client renet 0.11

This commit is contained in:
Henri Bourcereau 2023-10-30 14:25:05 +01:00
parent 1ae1eceda8
commit 2f5f051957
4 changed files with 35 additions and 37 deletions

View file

@ -1,12 +1,10 @@
use std::{net::UdpSocket, time::SystemTime};
use bevy::prelude::*;
use bevy_renet::RenetClientPlugin;
use std::{net::UdpSocket, time::SystemTime};
use renet::{
transport::{
ClientAuthentication, NETCODE_USER_DATA_BYTES,
},
RenetClient, ConnectionConfig,
transport::{ClientAuthentication, NetcodeClientTransport, NETCODE_USER_DATA_BYTES},
ConnectionConfig, RenetClient,
};
// This id needs to be the same as the server is using
@ -17,28 +15,31 @@ fn main() {
let args = std::env::args().collect::<Vec<String>>();
let username = &args[1];
let client = RenetClient::new(ConnectionConfig::default());
App::new()
.insert_resource(WindowDescriptor {
// Adding the username to the window title makes debugging a whole lot easier.
title: format!("TricTrac <{}>", username),
width: 480.0,
height: 540.0,
..default()
})
// Lets add a nice dark grey background color
.insert_resource(ClearColor(Color::hex("282828").unwrap()))
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
// Adding the username to the window title makes debugging a whole lot easier.
title: format!("TricTrac <{}>", username),
resolution: (480.0, 540.0).into(),
..default()
}),
..default()
}))
// Renet setup
.add_plugin(RenetClientPlugin)
.insert_resource(new_renet_client(&username).unwrap())
.add_system(handle_renet_error)
.add_plugins(RenetClientPlugin)
.insert_resource(client)
.insert_resource(new_renet_transport(&username).unwrap())
// .add_system(handle_renet_error)
.run();
}
////////// RENET NETWORKING //////////
// Creates a RenetClient thats already connected to a server.
// Returns an Err if connection fails
fn new_renet_client(username: &String) -> anyhow::Result<RenetClient> {
fn new_renet_transport(username: &String) -> anyhow::Result<NetcodeClientTransport> {
let server_addr = "127.0.0.1:5000".parse()?;
let socket = UdpSocket::bind("127.0.0.1:0")?;
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
@ -52,26 +53,21 @@ fn new_renet_client(username: &String) -> anyhow::Result<RenetClient> {
user_data[0..8].copy_from_slice(&(username.len() as u64).to_le_bytes());
user_data[8..username.len() + 8].copy_from_slice(username.as_bytes());
let client = RenetClient::new(
current_time,
socket,
let authentication = ClientAuthentication::Unsecure {
server_addr,
client_id,
RenetConnectionConfig::default(),
ClientAuthentication::Unsecure {
client_id,
protocol_id: PROTOCOL_ID,
server_addr,
user_data: Some(user_data),
},
)?;
user_data: Some(user_data),
protocol_id: PROTOCOL_ID,
};
let transport = NetcodeClientTransport::new(current_time, authentication, socket).unwrap();
Ok(client)
Ok(transport)
}
// If there's any network error we just panic 🤷‍♂️
// Ie. Client has lost connection to server, if internet is gone or server shut down etc.
fn handle_renet_error(mut renet_error: EventReader<RenetError>) {
for err in renet_error.iter() {
panic!("{}", err);
}
}
// fn handle_renet_error(mut renet_error: EventReader<RenetError>) {
// for err in renet_error.iter() {
// panic!("{}", err);
// }
// }