pub struct RenetServer { /* private fields */ }
Implementations§
Source§impl RenetServer
impl RenetServer
pub fn new(connection_config: ConnectionConfig) -> Self
Sourcepub fn add_connection(&mut self, client_id: ClientId)
pub fn add_connection(&mut self, client_id: ClientId)
Adds a new connection to the server. If a connection already exits it does nothing.
Note: This should only be called by the transport layer.
Sourcepub fn get_event(&mut self) -> Option<ServerEvent>
pub fn get_event(&mut self) -> Option<ServerEvent>
Returns a server event if available
§Usage
while let Some(event) = server.get_event() {
match event {
ServerEvent::ClientConnected { client_id } => {
println!("Client {client_id} connected.")
}
ServerEvent::ClientDisconnected { client_id, reason } => {
println!("Client {client_id} disconnected: {reason}");
}
}
}
Sourcepub fn has_connections(&self) -> bool
pub fn has_connections(&self) -> bool
Returns whether or not the server has connections
Sourcepub fn disconnect_reason(&self, client_id: ClientId) -> Option<DisconnectReason>
pub fn disconnect_reason(&self, client_id: ClientId) -> Option<DisconnectReason>
Returns the disconnection reason for the client if its disconnected
Sourcepub fn rtt(&self, client_id: ClientId) -> f64
pub fn rtt(&self, client_id: ClientId) -> f64
Returns the round-time trip for the client or 0.0 if the client is not found
Sourcepub fn packet_loss(&self, client_id: ClientId) -> f64
pub fn packet_loss(&self, client_id: ClientId) -> f64
Returns the packet loss for the client or 0.0 if the client is not found
Sourcepub fn bytes_sent_per_sec(&self, client_id: ClientId) -> f64
pub fn bytes_sent_per_sec(&self, client_id: ClientId) -> f64
Returns the bytes sent per seconds for the client or 0.0 if the client is not found
Sourcepub fn bytes_received_per_sec(&self, client_id: ClientId) -> f64
pub fn bytes_received_per_sec(&self, client_id: ClientId) -> f64
Returns the bytes received per seconds for the client or 0.0 if the client is not found
Sourcepub fn network_info(
&self,
client_id: ClientId,
) -> Result<NetworkInfo, ClientNotFound>
pub fn network_info( &self, client_id: ClientId, ) -> Result<NetworkInfo, ClientNotFound>
Returns all network informations for the client
Sourcepub fn remove_connection(&mut self, client_id: ClientId)
pub fn remove_connection(&mut self, client_id: ClientId)
Removes a connection from the server, emits an disconnect server event. It does nothing if the client does not exits.
Note: This should only be called by the transport layer.
Sourcepub fn disconnect(&mut self, client_id: ClientId)
pub fn disconnect(&mut self, client_id: ClientId)
Disconnects a client, it does nothing if the client does not exist.
Sourcepub fn disconnect_all(&mut self)
pub fn disconnect_all(&mut self)
Disconnects all client.
Sourcepub fn broadcast_message<I: Into<u8>, B: Into<Bytes>>(
&mut self,
channel_id: I,
message: B,
)
pub fn broadcast_message<I: Into<u8>, B: Into<Bytes>>( &mut self, channel_id: I, message: B, )
Send a message to all clients over a channel.
Sourcepub fn broadcast_message_except<I: Into<u8>, B: Into<Bytes>>(
&mut self,
except_id: ClientId,
channel_id: I,
message: B,
)
pub fn broadcast_message_except<I: Into<u8>, B: Into<Bytes>>( &mut self, except_id: ClientId, channel_id: I, message: B, )
Send a message to all clients, except the specified one, over a channel.
Sourcepub fn channel_available_memory<I: Into<u8>>(
&self,
client_id: ClientId,
channel_id: I,
) -> usize
pub fn channel_available_memory<I: Into<u8>>( &self, client_id: ClientId, channel_id: I, ) -> usize
Returns the available memory in bytes of a channel for the given client. Returns 0 if the client is not found.
Sourcepub fn can_send_message<I: Into<u8>>(
&self,
client_id: ClientId,
channel_id: I,
size_bytes: usize,
) -> bool
pub fn can_send_message<I: Into<u8>>( &self, client_id: ClientId, channel_id: I, size_bytes: usize, ) -> bool
Checks if can send a message with the given size in bytes over a channel for the given client. Returns false if the client is not found.
Sourcepub fn send_message<I: Into<u8>, B: Into<Bytes>>(
&mut self,
client_id: ClientId,
channel_id: I,
message: B,
)
pub fn send_message<I: Into<u8>, B: Into<Bytes>>( &mut self, client_id: ClientId, channel_id: I, message: B, )
Send a message to a client over a channel.
Sourcepub fn receive_message<I: Into<u8>>(
&mut self,
client_id: ClientId,
channel_id: I,
) -> Option<Bytes>
pub fn receive_message<I: Into<u8>>( &mut self, client_id: ClientId, channel_id: I, ) -> Option<Bytes>
Receive a message from a client over a channel.
Sourcepub fn clients_id_iter(&self) -> impl Iterator<Item = ClientId> + '_
pub fn clients_id_iter(&self) -> impl Iterator<Item = ClientId> + '_
Return ids for all connected clients (iterator)
Sourcepub fn clients_id(&self) -> Vec<ClientId> ⓘ
pub fn clients_id(&self) -> Vec<ClientId> ⓘ
Return ids for all connected clients
Sourcepub fn disconnections_id_iter(&self) -> impl Iterator<Item = ClientId> + '_
pub fn disconnections_id_iter(&self) -> impl Iterator<Item = ClientId> + '_
Return ids for all disconnected clients (iterator)
Sourcepub fn disconnections_id(&self) -> Vec<ClientId> ⓘ
pub fn disconnections_id(&self) -> Vec<ClientId> ⓘ
Return ids for all disconnected clients
Sourcepub fn connected_clients(&self) -> usize
pub fn connected_clients(&self) -> usize
Returns the current number of connected clients.
pub fn is_connected(&self, client_id: ClientId) -> bool
Sourcepub fn update(&mut self, duration: Duration)
pub fn update(&mut self, duration: Duration)
Advances the server by the duration. Should be called every tick
Sourcepub fn get_packets_to_send(
&mut self,
client_id: ClientId,
) -> Result<Vec<Vec<u8>>, ClientNotFound>
pub fn get_packets_to_send( &mut self, client_id: ClientId, ) -> Result<Vec<Vec<u8>>, ClientNotFound>
Returns a list of packets to be sent to the client.
Note: This should only be called by the transport layer.
Sourcepub fn process_packet_from(
&mut self,
payload: &[u8],
client_id: ClientId,
) -> Result<(), ClientNotFound>
pub fn process_packet_from( &mut self, payload: &[u8], client_id: ClientId, ) -> Result<(), ClientNotFound>
Process a packet received from the client.
Note: This should only be called by the transport layer.
Sourcepub fn new_local_client(&mut self, client_id: ClientId) -> RenetClient
pub fn new_local_client(&mut self, client_id: ClientId) -> RenetClient
Creates a local RenetClient, use this for testing.
Use Self::process_local_client
to update the local connection.
Sourcepub fn disconnect_local_client(
&mut self,
client_id: ClientId,
client: &mut RenetClient,
)
pub fn disconnect_local_client( &mut self, client_id: ClientId, client: &mut RenetClient, )
Disconnects a local RenetClient, created with Self::new_local_client
.
Sourcepub fn process_local_client(
&mut self,
client_id: ClientId,
client: &mut RenetClient,
) -> Result<(), ClientNotFound>
pub fn process_local_client( &mut self, client_id: ClientId, client: &mut RenetClient, ) -> Result<(), ClientNotFound>
Given a local RenetClient, receive and send packets to/from it.
Use this to update local client created from Self::new_local_client
.