NomadServer

Struct NomadServer 

Source
pub struct NomadServer<S: SyncState> { /* private fields */ }
Available on crate feature server only.
Expand description

A NOMAD protocol server.

Generic over state type S which must implement SyncState.

§Example

use nomad_protocol::server::{NomadServer, NomadServerBuilder};

let config = NomadServerBuilder::new()
    .bind_addr("0.0.0.0:19999".parse()?)
    .private_key(server_privkey)
    .build();

let (server, mut events) = NomadServer::<MyState>::bind(config, initial_state_factory).await?;

// Handle events
while let Some(event) = events.recv().await {
    match event {
        ServerEvent::ClientConnected { session_id, .. } => {
            println!("Client connected: {:?}", session_id);
        }
        ServerEvent::StateUpdated { session_id, state } => {
            // Process client state, send response
            server.send_to(session_id, response_state).await?;
        }
        ServerEvent::ClientDisconnected { session_id } => {
            println!("Client disconnected: {:?}", session_id);
        }
    }
}

Implementations§

Source§

impl<S: SyncState> NomadServer<S>

Source

pub async fn bind<F>( config: ServerConfig, _state_factory: F, ) -> Result<(Self, Receiver<ServerEvent<S>>), ServerError>
where F: Fn() -> S + Send + Sync + 'static,

Bind to an address and start the server.

The state_factory is called for each new session to create the initial state.

Source

pub fn local_addr(&self) -> SocketAddr

Get the local address the server is bound to.

Source

pub async fn session_count(&self) -> usize

Get the number of active sessions.

Source

pub async fn send_to( &self, session_id: ServerSessionId, state: S, ) -> Result<(), ServerError>

Send state to a specific session.

Source

pub async fn broadcast(&self, state: S) -> Result<(), ServerError>

Broadcast state to all sessions.

Source

pub fn session_sender(&self, session_id: ServerSessionId) -> SessionSender<S>

Get a sender handle for a specific session.

Source

pub async fn disconnect( &self, session_id: ServerSessionId, ) -> Result<(), ServerError>

Disconnect a specific session.

Source

pub async fn shutdown(self) -> Result<(), ServerError>

Gracefully shut down the server.

Source

pub fn config(&self) -> &ServerConfig

Get the server configuration.

Trait Implementations§

Source§

impl<S: SyncState> Drop for NomadServer<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<S> Freeze for NomadServer<S>

§

impl<S> !RefUnwindSafe for NomadServer<S>

§

impl<S> Send for NomadServer<S>

§

impl<S> Sync for NomadServer<S>

§

impl<S> Unpin for NomadServer<S>

§

impl<S> !UnwindSafe for NomadServer<S>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V