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
impl Client
Sourcepub fn new(bind_address: SocketAddr, time: f64) -> Result<Self, Error>
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.
Sourcepub fn new_dual(
bind_address_ipv4: SocketAddr,
bind_address_ipv6: SocketAddr,
time: f64,
) -> Result<Self, Error>
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.
Sourcepub fn connect(&mut self, connect_token: &[u8; 2048]) -> Result<(), Error>
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.
Sourcepub fn update(&mut self, time: f64)
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.
Sourcepub fn send_packet(&mut self, payload: &[u8]) -> Result<(), Error>
pub fn send_packet(&mut self, payload: &[u8]) -> Result<(), Error>
Sends a payload packet to the server. Does nothing unless the client is connected.
Sourcepub fn receive_packet(&mut self) -> Option<(Vec<u8>, u64)>
pub fn receive_packet(&mut self) -> Option<(Vec<u8>, u64)>
Pops the next payload packet received from the server, along with its sequence number.
Sourcepub fn disconnect(&mut self)
pub fn disconnect(&mut self)
Disconnects from the server, sending redundant disconnect packets so the server finds out quickly.
Sourcepub fn state(&self) -> ClientState
pub fn state(&self) -> ClientState
The current client state.
Sourcepub fn client_index(&self) -> usize
pub fn client_index(&self) -> usize
The slot this client occupies on the server, valid while connected.
Sourcepub fn max_clients(&self) -> usize
pub fn max_clients(&self) -> usize
The number of client slots on the server, valid while connected.
Sourcepub fn next_packet_sequence(&self) -> u64
pub fn next_packet_sequence(&self) -> u64
The sequence number of the next packet this client will send.
Sourcepub fn server_address(&self) -> Option<SocketAddr>
pub fn server_address(&self) -> Option<SocketAddr>
The server address the client is currently connecting or connected to.