Struct cobalt::Server
[−]
[src]
pub struct Server<S: Socket, R: RateLimiter, M: PacketModifier> { /* fields omitted */ }
Implementation of a multi-client low latency socket server.
Basic Usage
use cobalt::{ BinaryRateLimiter, Server, Config, NoopPacketModifier, MessageKind, UdpSocket }; // Create a new server that communicates over a udp socket let mut server = Server::<UdpSocket, BinaryRateLimiter, NoopPacketModifier>::new(Config::default()); // Make the server listen on port `1234` on all interfaces. server.listen("0.0.0.0:1234").expect("Failed to bind to socket."); // loop { // Accept incoming connections and fetch their events while let Ok(event) = server.accept_receive() { // Handle events (e.g. Connection, Messages, etc.) } // Send a message to all connected clients for (_, conn) in server.connections() { conn.send(MessageKind::Instant, b"Ping".to_vec()); } // Send all outgoing messages. // // Also auto delay the current thread to achieve the configured tick rate. server.send(true); // } // Shutdown the server (freeing its socket and closing all its connections) server.shutdown();
Methods
impl<S: Socket, R: RateLimiter, M: PacketModifier> Server<S, R, M>
[src]
fn new(config: Config) -> Server<S, R, M>
Creates a new server 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 local_addr(&self) -> Result<SocketAddr, Error>
Returns the local address that the client is sending from.
fn connection(&mut self,
id: &ConnectionID)
-> Result<&mut Connection<R, M>, Error>
id: &ConnectionID)
-> Result<&mut Connection<R, M>, Error>
Returns a mutable reference to the specified client connection.
fn connections(&mut self) -> &mut HashMap<ConnectionID, Connection<R, M>>
Returns a mutable reference to the servers client connections.
fn socket(&mut self) -> Result<&mut S, Error>
Returns a mutable reference to the server's underlying socket.
fn config(&self) -> Config
Returns the server's current configuration.
fn set_config(&mut self, config: Config)
Overrides the server's current configuration.
fn listen<A: ToSocketAddrs>(&mut self, addr: A) -> Result<(), Error>
Binds the server to listen the specified address.
fn accept_receive(&mut self) -> Result<ServerEvent, TryRecvError>
Accepts new incoming client connections from the server's underlying server and receives and returns messages from them.
fn send(&mut self, auto_tick: bool) -> Result<(), Error>
Sends all queued messages to the server's underlying client connections.
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 server's
configured send_rate
.
fn shutdown(&mut self) -> Result<(), Error>
Shuts down all of the server's client connections, clearing any state and freeing the server's socket.