Skip to content

Commit

Permalink
Netcode: add client_id in public ConnectToken
Browse files Browse the repository at this point in the history
This makes easier to acquire the client id when receiving a connect token. Before we need to always give the client id when creating the client, now we get it from the token.
lucaspoffo committed Aug 2, 2022
1 parent c68e340 commit b2affb5
Showing 9 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bevy_renet/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ fn new_renet_client() -> RenetClient {
server_addr,
user_data: None,
};
RenetClient::new(current_time, socket, client_id, connection_config, authentication).unwrap()
RenetClient::new(current_time, socket, connection_config, authentication).unwrap()
}

fn new_renet_server() -> RenetServer {
1 change: 0 additions & 1 deletion bevy_renet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -176,7 +176,6 @@ mod tests {
RenetClient::new(
current_time,
UdpSocket::bind((ip, 0))?,
client_id,
RenetConnectionConfig::default(),
authentication,
)
2 changes: 1 addition & 1 deletion demo_bevy/src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ fn new_renet_client() -> RenetClient {
user_data: None,
};

RenetClient::new(current_time, socket, client_id, connection_config, authentication).unwrap()
RenetClient::new(current_time, socket, connection_config, authentication).unwrap()
}

fn main() {
2 changes: 1 addition & 1 deletion renet/examples/echo.rs
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ fn client(server_addr: SocketAddr, username: Username) {
user_data: Some(username.to_netcode_user_data()),
protocol_id: PROTOCOL_ID,
};
let mut client = RenetClient::new(current_time, socket, client_id, connection_config, authentication).unwrap();
let mut client = RenetClient::new(current_time, socket, connection_config, authentication).unwrap();
let stdin_channel = spawn_stdin_channel();

let mut last_updated = Instant::now();
4 changes: 1 addition & 3 deletions renet/src/client.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,6 @@ impl RenetClient {
pub fn new(
current_time: Duration,
socket: UdpSocket,
client_id: u64,
config: RenetConnectionConfig,
authentication: ClientAuthentication,
) -> Result<Self, RenetError> {
@@ -70,7 +69,7 @@ impl RenetClient {
ClientAuthentication::Secure { connect_token } => connect_token,
};

let netcode_client = NetcodeClient::new(current_time, client_id, connect_token);
let netcode_client = NetcodeClient::new(current_time, connect_token);
let client_packet_info = ClientPacketInfo::new(config.bandwidth_smoothing_factor);

Ok(Self {
@@ -91,7 +90,6 @@ impl RenetClient {
Self::new(
Duration::ZERO,
socket,
0,
Default::default(),
ClientAuthentication::Unsecure {
client_id: 0,
6 changes: 3 additions & 3 deletions renetcode/examples/echo.rs
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ fn main() {
private_key,
)
.unwrap();
client(client_id, connect_token);
client(connect_token);
}
"server" => {
let server_addr: SocketAddr = format!("127.0.0.1:{}", args[2]).parse().unwrap();
@@ -158,11 +158,11 @@ fn server(addr: SocketAddr, private_key: [u8; NETCODE_KEY_BYTES]) {
}
}

fn client(client_id: u64, connect_token: ConnectToken) {
fn client(connect_token: ConnectToken) {
let udp_socket = UdpSocket::bind("127.0.0.1:0").unwrap();
udp_socket.set_nonblocking(true).unwrap();
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let mut client = NetcodeClient::new(now, client_id, connect_token);
let mut client = NetcodeClient::new(now, connect_token);
let stdin_channel = spawn_stdin_channel();
let mut buffer = [0u8; NETCODE_MAX_PACKET_BYTES];

6 changes: 3 additions & 3 deletions renetcode/src/client.rs
Original file line number Diff line number Diff line change
@@ -67,12 +67,12 @@ impl fmt::Display for DisconnectReason {
}

impl NetcodeClient {
pub fn new(current_time: Duration, client_id: ClientID, connect_token: ConnectToken) -> Self {
pub fn new(current_time: Duration, connect_token: ConnectToken) -> Self {
let server_addr = connect_token.server_addresses[0].expect("cannot create or deserialize a ConnectToken without a server address");

Self {
sequence: 0,
client_id,
client_id: connect_token.client_id,
server_addr,
server_addr_index: 0,
challenge_token_sequence: 0,
@@ -338,7 +338,7 @@ mod tests {
.unwrap();
let server_key = connect_token.server_to_client_key;
let client_key = connect_token.client_to_server_key;
let mut client = NetcodeClient::new(Duration::ZERO, client_id, connect_token);
let mut client = NetcodeClient::new(Duration::ZERO, connect_token);
let (packet_buffer, _) = client.update(Duration::ZERO).unwrap();

let (r_sequence, packet) = Packet::decode(packet_buffer, protocol_id, None, None).unwrap();
7 changes: 6 additions & 1 deletion renetcode/src/server.rs
Original file line number Diff line number Diff line change
@@ -486,6 +486,11 @@ impl NetcodeServer {
self.max_clients
}

/// Returns the maximum number of clients that can be connected.
pub fn connected_clients(&self) -> usize {
self.clients.iter().filter(|slot| slot.is_some()).count()
}

/// Advance the server current time, and remove any pending connections that have expired.
pub fn update(&mut self, duration: Duration) {
self.current_time += duration;
@@ -669,7 +674,7 @@ mod tests {
TEST_KEY,
)
.unwrap();
let mut client = NetcodeClient::new(Duration::ZERO, client_id, connect_token);
let mut client = NetcodeClient::new(Duration::ZERO, connect_token);
let (client_packet, _) = client.update(Duration::ZERO).unwrap();

let result = server.process_packet(client_addr, client_packet);
8 changes: 8 additions & 0 deletions renetcode/src/token.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,10 @@ use chacha20poly1305::aead::Error as CryptoError;
/// system or from a call to a REST API as an example.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectToken {
// NOTE: On the netcode standard the client id is not available in the public part of the
// ConnectToken. But having it acessible here makes it easier to consume the token, and the
// server still uses the client_id from the private part.
pub(crate) client_id: u64,
pub(crate) version_info: [u8; 13],
pub(crate) protocol_id: u64,
pub(crate) create_timestamp: u64,
@@ -100,6 +104,7 @@ impl ConnectToken {
private_connect_token.encode(&mut private_data, protocol_id, expire_timestamp, &xnonce, private_key)?;

Ok(Self {
client_id,
version_info: *NETCODE_VERSION_INFO,
protocol_id,
private_data,
@@ -114,6 +119,7 @@ impl ConnectToken {
}

pub fn write(&self, writer: &mut impl io::Write) -> Result<(), io::Error> {
writer.write_all(&self.client_id.to_le_bytes())?;
writer.write_all(&self.version_info)?;
writer.write_all(&self.protocol_id.to_le_bytes())?;
writer.write_all(&self.create_timestamp.to_le_bytes())?;
@@ -129,6 +135,7 @@ impl ConnectToken {
}

pub fn read(src: &mut impl io::Read) -> Result<Self, NetcodeError> {
let client_id = read_u64(src)?;
let version_info: [u8; 13] = read_bytes(src)?;
if &version_info != NETCODE_VERSION_INFO {
return Err(NetcodeError::InvalidVersion);
@@ -146,6 +153,7 @@ impl ConnectToken {
let server_to_client_key: [u8; NETCODE_KEY_BYTES] = read_bytes(src)?;

Ok(Self {
client_id,
version_info,
protocol_id,
create_timestamp,

0 comments on commit b2affb5

Please sign in to comment.