Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Expand description

A netcode dedicated server.

Create it with the public address clients connect to, start it with a number of client slots, then drive it by calling update once per frame with the current time. All methods must be called from one thread; the server performs no internal synchronization.

Implementations§

Source§

impl Server

Source

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.

Source

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.

Source

pub fn stop(&mut self)

Stops the server, disconnecting all clients.

Source

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.

Source

pub fn next_event(&mut self) -> Option<ServerEvent>

Pops the next connect or disconnect event.

Source

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.

Source

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.

Source

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.

Source

pub fn disconnect_all_clients(&mut self)

Disconnects all connected clients.

Source

pub fn running(&self) -> bool

Whether the server is running.

Source

pub fn max_clients(&self) -> usize

The number of client slots, or 0 when the server is not running.

Source

pub fn num_connected_clients(&self) -> usize

The number of connected clients.

Source

pub fn client_connected(&self, client_index: usize) -> bool

Whether a client is connected in the given slot.

Source

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.

Source

pub fn client_address(&self, client_index: usize) -> Option<SocketAddr>

The address of the client in the given slot.

Source

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.

Source

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.

Source

pub fn port(&self) -> u16

The port the server socket is bound to.

Source

pub fn address(&self) -> SocketAddr

The public address clients connect to.

Trait Implementations§

Source§

impl Drop for Server

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.