Skip to main content

Client

Struct Client 

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

A netcode client.

Drive it by calling update once per frame with the current time. All methods must be called from one thread; the client performs no internal synchronization.

Implementations§

Source§

impl Client

Source

pub fn new(bind_address: SocketAddr, time: f64) -> Result<Self, Error>

Creates a client with a socket bound to bind_address. Bind to port 0 to let the operating system pick a port.

The client can only reach server addresses in the same address family as the bind address; use new_dual to support connect tokens that mix IPv4 and IPv6 server addresses.

Source

pub fn new_dual( bind_address_ipv4: SocketAddr, bind_address_ipv6: SocketAddr, time: f64, ) -> Result<Self, Error>

Creates a client with one IPv4 socket and one IPv6 socket, so it can connect to server addresses of either family.

Source

pub fn connect(&mut self, connect_token: &[u8; 2048]) -> Result<(), Error>

Starts connecting to the servers listed in the connect token, trying each address in order until one accepts.

Track progress by polling state after each update. On an invalid token this returns an error and the client transitions to ClientState::InvalidConnectToken.

Source

pub fn update(&mut self, time: f64)

Advances the client to the current time: receives and processes packets, sends queued packets and keep-alives, and applies timeouts.

Source

pub fn send_packet(&mut self, payload: &[u8]) -> Result<(), Error>

Sends a payload packet to the server. Does nothing unless the client is connected.

Source

pub fn receive_packet(&mut self) -> Option<(Vec<u8>, u64)>

Pops the next payload packet received from the server, along with its sequence number.

Source

pub fn disconnect(&mut self)

Disconnects from the server, sending redundant disconnect packets so the server finds out quickly.

Source

pub fn state(&self) -> ClientState

The current client state.

Source

pub fn client_index(&self) -> usize

The slot this client occupies on the server, valid while connected.

Source

pub fn max_clients(&self) -> usize

The number of client slots on the server, valid while connected.

Source

pub fn next_packet_sequence(&self) -> u64

The sequence number of the next packet this client will send.

Source

pub fn port(&self) -> u16

The local port the client is bound to.

Source

pub fn server_address(&self) -> Option<SocketAddr>

The server address the client is currently connecting or connected to.

Trait Implementations§

Source§

impl Drop for Client

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.