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>
impl<S: SyncState> NomadServer<S>
Sourcepub async fn bind<F>(
config: ServerConfig,
_state_factory: F,
) -> Result<(Self, Receiver<ServerEvent<S>>), ServerError>
pub async fn bind<F>( config: ServerConfig, _state_factory: F, ) -> Result<(Self, Receiver<ServerEvent<S>>), ServerError>
Bind to an address and start the server.
The state_factory is called for each new session to create the initial state.
Sourcepub fn local_addr(&self) -> SocketAddr
pub fn local_addr(&self) -> SocketAddr
Get the local address the server is bound to.
Sourcepub async fn session_count(&self) -> usize
pub async fn session_count(&self) -> usize
Get the number of active sessions.
Sourcepub async fn send_to(
&self,
session_id: ServerSessionId,
state: S,
) -> Result<(), ServerError>
pub async fn send_to( &self, session_id: ServerSessionId, state: S, ) -> Result<(), ServerError>
Send state to a specific session.
Sourcepub async fn broadcast(&self, state: S) -> Result<(), ServerError>
pub async fn broadcast(&self, state: S) -> Result<(), ServerError>
Broadcast state to all sessions.
Sourcepub fn session_sender(&self, session_id: ServerSessionId) -> SessionSender<S>
pub fn session_sender(&self, session_id: ServerSessionId) -> SessionSender<S>
Get a sender handle for a specific session.
Sourcepub async fn disconnect(
&self,
session_id: ServerSessionId,
) -> Result<(), ServerError>
pub async fn disconnect( &self, session_id: ServerSessionId, ) -> Result<(), ServerError>
Disconnect a specific session.
Sourcepub async fn shutdown(self) -> Result<(), ServerError>
pub async fn shutdown(self) -> Result<(), ServerError>
Gracefully shut down the server.
Sourcepub fn config(&self) -> &ServerConfig
pub fn config(&self) -> &ServerConfig
Get the server configuration.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more