Struct cobalt::Client
[−]
[src]
pub struct Client<S: Socket, R: RateLimiter, M: PacketModifier> { /* fields omitted */ }
Implementation of a low latency socket client.
Basic Usage
use cobalt::{ BinaryRateLimiter, Client, Config, NoopPacketModifier, MessageKind, UdpSocket }; // Create a new client that communicates over a udp socket let mut client = Client::<UdpSocket, BinaryRateLimiter, NoopPacketModifier>::new(Config::default()); // Initiate a connection to the server client.connect("127.0.0.1:1234").expect("Failed to bind to socket"); // loop { // Fetch connection events while let Ok(event) = client.receive() { // Handle events (e.g. Connection, Messages, etc.) } // Schedule a message to the send to the server if let Ok(connection) = client.connection() { connection.send(MessageKind::Instant, b"Ping".to_vec()); } // Send all outgoing messages. // // Also auto delay the current thread to achieve the configured tick rate. client.send(true); // } // Disconnect the client, closing its connection and unbinding its socket client.disconnect();
Methods
impl<S: Socket, R: RateLimiter, M: PacketModifier> Client<S, R, M>
[src]
fn new(config: Config) -> Client<S, R, M>
Creates a new client with the given configuration.
fn bytes_sent(&self) -> u32
Returns the number of bytes sent over the last second.
fn bytes_received(&self) -> u32
Returns the number of bytes received over the last second.
fn peer_addr(&self) -> Result<SocketAddr, Error>
Returns the address of the server the client is currently connected to.
fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local address that the client is sending from.
fn connection(&mut self) -> Result<&mut Connection<R, M>, Error>
Returns a mutable reference to underlying connection to the server.
fn socket(&mut self) -> Result<&mut S, Error>
Returns a mutable reference to the client's underlying socket.
fn config(&self) -> Config
Returns the client's current configuration.
fn set_config(&mut self, config: Config)
Overrides the client's current configuration.
fn connect<A: ToSocketAddrs>(&mut self, addr: A) -> Result<(), Error>
Establishes a connection with the server at the specified address.
fn receive(&mut self) -> Result<ClientEvent, TryRecvError>
Receives the next incoming message from the client's underlying connection.
fn send(&mut self, auto_tick: bool) -> Result<(), Error>
Sends all queued messages over the client's underlying connection.
If auto_tick
is specified as true
this method will block the
current thread for the amount of time which is required to limit the
number of calls per second (when called inside a loop) to the client's
configured send_rate
.
fn reset(&mut self) -> Result<(), Error>
Resets the client, clearing all pending events and dropping any
connection to the server, returning it into the Connecting
state.
This can be used to re-try a connection attempt if a previous one has failed.
fn disconnect(&mut self) -> Result<(), Error>
Drops the client's connection to the server, freeing the socket and clearing any state.