pub struct Server { /* private fields */ }Expand description
Implementations§
Source§impl Server
impl Server
Sourcepub fn new(
public_address: SocketAddr,
protocol_id: u64,
private_key: &Key,
time: f64,
) -> Result<Self, Error>
pub fn new( public_address: SocketAddr, protocol_id: u64, private_key: &Key, time: f64, ) -> Result<Self, Error>
Creates a server that clients reach at public_address. The server binds a
socket on that port on all interfaces of the same address family.
protocol_id is a 64-bit value unique to this game or application, and
private_key is shared between the web backend and the dedicated servers.
A public address with port 0 binds an ephemeral port and advertises it as the public port, which is convenient for tests running on localhost.
Sourcepub fn start(&mut self, max_clients: usize) -> Result<(), Error>
pub fn start(&mut self, max_clients: usize) -> Result<(), Error>
Starts the server with max_clients client slots, in [1, MAX_CLIENTS].
If the server is already running it is stopped first, disconnecting everyone.
Sourcepub fn update(&mut self, time: f64)
pub fn update(&mut self, time: f64)
Advances the server to the current time: receives and processes packets, sends keep-alives, and times out unresponsive clients.
Sourcepub fn next_event(&mut self) -> Option<ServerEvent>
pub fn next_event(&mut self) -> Option<ServerEvent>
Pops the next connect or disconnect event.
Sourcepub fn send_packet(
&mut self,
client_index: usize,
payload: &[u8],
) -> Result<(), Error>
pub fn send_packet( &mut self, client_index: usize, payload: &[u8], ) -> Result<(), Error>
Sends a payload packet to the client in the given slot. Does nothing unless that client is connected.
Sourcepub fn receive_packet(&mut self, client_index: usize) -> Option<(Vec<u8>, u64)>
pub fn receive_packet(&mut self, client_index: usize) -> Option<(Vec<u8>, u64)>
Pops the next payload packet received from the client in the given slot, along with its sequence number.
Sourcepub fn disconnect_client(&mut self, client_index: usize)
pub fn disconnect_client(&mut self, client_index: usize)
Disconnects the client in the given slot, sending redundant disconnect packets so it finds out quickly.
Sourcepub fn disconnect_all_clients(&mut self)
pub fn disconnect_all_clients(&mut self)
Disconnects all connected clients.
Sourcepub fn max_clients(&self) -> usize
pub fn max_clients(&self) -> usize
The number of client slots, or 0 when the server is not running.
Sourcepub fn num_connected_clients(&self) -> usize
pub fn num_connected_clients(&self) -> usize
The number of connected clients.
Sourcepub fn client_connected(&self, client_index: usize) -> bool
pub fn client_connected(&self, client_index: usize) -> bool
Whether a client is connected in the given slot.
Sourcepub fn client_id(&self, client_index: usize) -> u64
pub fn client_id(&self, client_index: usize) -> u64
The client id of the client in the given slot, or 0 if none is connected.
Sourcepub fn client_address(&self, client_index: usize) -> Option<SocketAddr>
pub fn client_address(&self, client_index: usize) -> Option<SocketAddr>
The address of the client in the given slot.
Sourcepub fn client_user_data(&self, client_index: usize) -> Option<&UserData>
pub fn client_user_data(&self, client_index: usize) -> Option<&UserData>
The user data carried in the connect token of the client in the given slot.
Sourcepub fn next_packet_sequence(&self, client_index: usize) -> u64
pub fn next_packet_sequence(&self, client_index: usize) -> u64
The sequence number of the next packet the server will send to the client in the given slot.
Sourcepub fn address(&self) -> SocketAddr
pub fn address(&self) -> SocketAddr
The public address clients connect to.