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]

Creates a new server with the given configuration.

Returns the number of bytes sent over the last second.

Returns the number of bytes received over the last second.

Returns the local address that the client is sending from.

Returns a mutable reference to the specified client connection.

Returns a mutable reference to the servers client connections.

Returns a mutable reference to the server's underlying socket.

Returns the server's current configuration.

Overrides the server's current configuration.

Binds the server to listen the specified address.

Accepts new incoming client connections from the server's underlying server and receives and returns messages from them.

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.

Shuts down all of the server's client connections, clearing any state and freeing the server's socket.

Trait Implementations

impl<S: Debug + Socket, R: Debug + RateLimiter, M: Debug + PacketModifier> Debug for Server<S, R, M>
[src]

Formats the value using the given formatter.