Skip to content

Commit

Permalink
Renet: fix server.connected_clients
Browse files Browse the repository at this point in the history
The iterator was filtering by disconnected instead of connected. When adding a new connection to the server, set it always to connected.
  • Loading branch information
lucaspoffo committed Nov 7, 2023
1 parent 1b4c1c9 commit a7bced4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bevy_renet/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use bevy::prelude::{shape::Plane, *};
use bevy_renet::{
client_connected,
renet::{
transport::{ClientAuthentication, ServerAuthentication, ServerConfig},
ConnectionConfig, DefaultChannel, RenetClient, RenetServer, ServerEvent,
},
transport::{NetcodeClientPlugin, NetcodeServerPlugin},
RenetClientPlugin, RenetServerPlugin, client_connected,
RenetClientPlugin, RenetServerPlugin,
};
use renet::{
transport::{NetcodeClientTransport, NetcodeServerTransport, NetcodeTransportError},
Expand Down
2 changes: 1 addition & 1 deletion renet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod transport;

pub use channel::{ChannelConfig, DefaultChannel, SendType};
pub use error::{ChannelError, ClientNotFound, DisconnectReason};
pub use remote_connection::{ConnectionConfig, RenetConnectionStatus, NetworkInfo, RenetClient};
pub use remote_connection::{ConnectionConfig, NetworkInfo, RenetClient, RenetConnectionStatus};
pub use server::{RenetServer, ServerEvent};

pub use bytes::Bytes;
Expand Down
12 changes: 7 additions & 5 deletions renet/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;
use bytes::Bytes;

/// Connection and disconnection events in the server.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Event))]
pub enum ServerEvent {
ClientConnected { client_id: ClientId },
Expand Down Expand Up @@ -41,7 +41,9 @@ impl RenetServer {
return;
}

let connection = RenetClient::new_from_server(self.connection_config.clone());
let mut connection = RenetClient::new_from_server(self.connection_config.clone());
// Consider newly added connections as connected
connection.set_connected();
self.connections.insert(client_id, connection);
self.events.push_back(ServerEvent::ClientConnected { client_id })
}
Expand Down Expand Up @@ -205,7 +207,7 @@ impl RenetServer {

/// Return ids for all connected clients (iterator)
pub fn clients_id_iter(&self) -> impl Iterator<Item = ClientId> + '_ {
self.connections.iter().filter(|(_, c)| !c.is_disconnected()).map(|(id, _)| *id)
self.connections.iter().filter(|(_, c)| c.is_connected()).map(|(id, _)| *id)
}

/// Return ids for all connected clients
Expand All @@ -225,12 +227,12 @@ impl RenetServer {

/// Returns the current number of connected clients.
pub fn connected_clients(&self) -> usize {
self.connections.iter().filter(|(_, c)| c.is_disconnected()).count()
self.connections.iter().filter(|(_, c)| c.is_connected()).count()
}

pub fn is_connected(&self, client_id: ClientId) -> bool {
if let Some(connection) = self.connections.get(&client_id) {
return !connection.is_disconnected();
return connection.is_connected();
}

false
Expand Down
14 changes: 13 additions & 1 deletion renet/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytes::Bytes;
use renet::{ClientId, ConnectionConfig, DefaultChannel, RenetClient, RenetServer};
use renet::{ClientId, ConnectionConfig, DefaultChannel, DisconnectReason, RenetClient, RenetServer, ServerEvent};

pub fn init_log() {
let _ = env_logger::builder().is_test(true).try_init();
Expand All @@ -13,6 +13,8 @@ fn test_remote_connection_reliable_channel() {

let client_id = ClientId::from_raw(0);
server.add_connection(client_id);
assert_eq!(server.connected_clients(), 1);
assert_eq!(ServerEvent::ClientConnected { client_id }, server.get_event().unwrap());

for _ in 0..200 {
server.send_message(client_id, DefaultChannel::ReliableOrdered, Bytes::from("test"));
Expand Down Expand Up @@ -53,4 +55,14 @@ fn test_remote_connection_reliable_channel() {
}

assert_eq!(count, 10);

server.remove_connection(client_id);
assert_eq!(server.connected_clients(), 0);
assert_eq!(
ServerEvent::ClientDisconnected {
client_id,
reason: DisconnectReason::Transport
},
server.get_event().unwrap()
);
}

0 comments on commit a7bced4

Please sign in to comment.