Skip to content

Commit

Permalink
Renet: add functions to create/update local clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspoffo committed Nov 11, 2024
1 parent a69ef2b commit 5426d72
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions renet/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,29 @@ impl RenetServer {
None => Err(ClientNotFound),
}
}

/// Creates a local [RenetClient], use this for testing.
/// Use [`Self::process_local_client`] to update the local connection.
pub fn new_local_client(&mut self, client_id: ClientId) -> RenetClient {
let mut client = RenetClient::new_from_server(self.connection_config.clone());
client.set_connected();

self.add_connection(client_id);

client
}

/// Given a local [RenetClient], receive and send packets to/from it.
/// Use this to update local client created from [`Self::new_local_client`].
pub fn process_local_client(&mut self, client_id: ClientId, client: &mut RenetClient) -> Result<(), ClientNotFound> {
for packet in self.get_packets_to_send(client_id)? {
client.process_packet(&packet);
}

for packet in client.get_packets_to_send() {
self.process_packet_from(&packet, client_id)?
}

Ok(())
}
}
20 changes: 20 additions & 0 deletions renet/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ fn test_remote_connection_reliable_channel() {
server.get_event().unwrap()
);
}

#[test]
fn test_local_client() {
init_log();
let mut server = RenetServer::new(ConnectionConfig::default());

let client_id: ClientId = 0;
let mut client = server.new_local_client(client_id);

server.send_message(client_id, DefaultChannel::ReliableOrdered, Bytes::from("test server"));
client.send_message(DefaultChannel::ReliableOrdered, Bytes::from("test client"));

server.process_local_client(client_id, &mut client).unwrap();

let server_message = server.receive_message(client_id, DefaultChannel::ReliableOrdered).unwrap();
assert_eq!(server_message, "test client");

let client_message = client.receive_message(DefaultChannel::ReliableOrdered).unwrap();
assert_eq!(client_message, "test server");
}

0 comments on commit 5426d72

Please sign in to comment.