Struct ServerConfig

Source
pub struct ServerConfig<Ctx> { /* private fields */ }
Expand description

Configuration for a server.

  • num_disconnect_packets - The number of redundant disconnect packets that will be sent to a client when the server is disconnecting it.
  • keep_alive_send_rate - The rate at which keep-alive packets will be sent to clients.
  • on_connect - A callback that will be called when a client is connected to the server.
  • on_disconnect - A callback that will be called when a client is disconnected from the server.

§Example

use std::sync::{Arc, Mutex};
use netcode::{Server, ServerConfig};

let thread_safe_counter = Arc::new(Mutex::new(0));
let cfg = ServerConfig::with_context(thread_safe_counter).on_connect(|idx, ctx| {
    let mut counter = ctx.lock().unwrap();
    *counter += 1;
    println!("client {} connected, counter: {idx}", counter);
});
let server = Server::with_config(addr, protocol_id, private_key, cfg).unwrap();

Implementations§

Source§

impl<Ctx> ServerConfig<Ctx>

Source

pub fn new() -> ServerConfig<()>

Create a new, default server configuration with no context.

Source

pub fn with_context(ctx: Ctx) -> Self

Create a new server configuration with context that will be passed to the callbacks.

Source

pub fn num_disconnect_packets(self, num: usize) -> Self

Set the number of redundant disconnect packets that will be sent to a client when the server is disconnecting it.
The default is 10 packets.

Source

pub fn keep_alive_send_rate(self, rate_seconds: f64) -> Self

Set the rate (in seconds) at which keep-alive packets will be sent to clients.
The default is 10 packets per second. (0.1 seconds)

Source

pub fn on_connect<F>(self, cb: F) -> Self
where F: FnMut(ClientIndex, &mut Ctx) + Send + Sync + 'static,

Provide a callback that will be called when a client is connected to the server.
The callback will be called with the client index and the context that was provided (provide a None context if you don’t need one).

See ServerConfig for an example.

Source

pub fn on_disconnect<F>(self, cb: F) -> Self
where F: FnMut(ClientIndex, &mut Ctx) + Send + Sync + 'static,

Provide a callback that will be called when a client is disconnected from the server.
The callback will be called with the client index and the context that was provided (provide a None context if you don’t need one).

See ServerConfig for an example.

Trait Implementations§

Source§

impl Default for ServerConfig<()>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Ctx> Freeze for ServerConfig<Ctx>
where Ctx: Freeze,

§

impl<Ctx> !RefUnwindSafe for ServerConfig<Ctx>

§

impl<Ctx> Send for ServerConfig<Ctx>
where Ctx: Send,

§

impl<Ctx> Sync for ServerConfig<Ctx>
where Ctx: Sync,

§

impl<Ctx> Unpin for ServerConfig<Ctx>
where Ctx: Unpin,

§

impl<Ctx> !UnwindSafe for ServerConfig<Ctx>

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.