pub struct RUMServer { /* private fields */ }
Expand description
This is the Server primitive that listens for incoming connections and manages “low-level” messages.
This struct tracks accepting new clients via RUMServer::handle_accept, incoming messages via RUMServer::handle_receive and message dispatchs via RUMServer::handle_send.
All key methods are async and shall be run exclusively in the async context. We provide a set of tools that allow you to interact with this struct from sync code. One such tool is RUMServerHandle.
The RUMServer::run method orchestrate a series of steps that allows starting server management. The result is that the server will check for connections and messages autonomously. You want to call this method in a non blocking manner from the sync context, so that the server can handle the transactions in the background
Implementations§
Source§impl RUMServer
impl RUMServer
Sourcepub async fn new(ip: &str, port: u16) -> RUMResult<RUMServer>
pub async fn new(ip: &str, port: u16) -> RUMResult<RUMServer>
Constructs a server and binds the port
on interface denoted by ip
. The server
management is not started until you invoke RUMServer::run.
Sourcepub async fn run(ctx: SafeServer) -> RUMResult<()>
pub async fn run(ctx: SafeServer) -> RUMResult<()>
Main, juicy server management logic. Call this method to kick start a series of autonomous checks. Message handling and connection handling are taken care autonamtically.
Await this method if you wish to block the context thread indefinitely. This method has a never ending loop looking for when the server has been signalled to shut down.
ctx
here refers to an instance of the server wrapped by SafeServer. This was done
to be able to make the management logic work autonomously across threads. We call the
RWLock’s read() method every pass of the loop and await on it. This allows the runtime
to progress the state of other futures and allow sync code to interact with the server
state. In most situations, this step should yield a no-op.
Sourcepub async fn stop_server(ctx: &SafeServer) -> RUMResult<RUMString>
pub async fn stop_server(ctx: &SafeServer) -> RUMResult<RUMString>
This method signals the server to stop.
Then, this method waits for run to cleanup before exiting. Meaning, this method’s exit is enough to signal everything went through smoothly.
Sourcepub async fn handle_accept(
listener: SafeListener,
clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>,
tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
) -> RUMResult<()>
pub async fn handle_accept( listener: SafeListener, clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, ) -> RUMResult<()>
Contains basic logic for listening for incoming connections.
Sourcepub async fn handle_send(
clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>,
tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
) -> RUMResult<()>
pub async fn handle_send( clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, ) -> RUMResult<()>
Contains logic for sending messages queued for a client to it. tx_out
is a reference
of [SafeMappedQueues] which is a hash map of [SafeQueue
Sourcepub async fn handle_receive(
clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>,
tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
) -> RUMResult<()>
pub async fn handle_receive( clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, ) -> RUMResult<()>
Contains the logic for handling receiving messages from clients. Incoming messages are all placed into a queue that the “outside” world can interact with.
Sourcepub async fn handle_client_gc(
clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>,
tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>,
) -> RUMResult<()>
pub async fn handle_client_gc( clients: Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, tx_in: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, tx_out: Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, ) -> RUMResult<()>
Contains the logic for handling removal of clients from the server if they disconnected.
pub async fn register_queue( tx_queues: &Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, client: &RUMString, )
pub async fn push_queue( tx_queues: &Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, client: &RUMString, msg: RUMNetMessage, ) -> RUMResult<()>
pub async fn pop_queue( tx_queues: &Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, client: &RUMString, ) -> Option<Vec<RUMNetMessage>>
pub async fn is_queue_empty( tx_queues: &Arc<AsyncMutex<HashMap<RUMString, Arc<AsyncMutex<VecDeque<RUMNetMessage>>>>>>, client: &RUMString, ) -> bool
pub async fn send(client: &SafeClient, msg: &RUMNetMessage) -> RUMResult<()>
pub async fn receive(client: &SafeClient) -> RUMResult<RUMNetMessage>
pub async fn disconnect(client: &SafeClient)
pub async fn get_client( clients: &Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, client: &RUMString, ) -> RUMResult<SafeClient>
Sourcepub async fn get_client_ids(
clients: &Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>,
) -> ClientIDList
pub async fn get_client_ids( clients: &Arc<AsyncRwLock<HashMap<RUMString, SafeClient>>>, ) -> ClientIDList
Return client id list.
pub async fn get_client_id(client: &SafeClient) -> RUMString
pub async fn get_client_readiness( client: &SafeClient, socket_readiness_type: &SOCKET_READINESS_TYPE, ) -> bool
Sourcepub async fn get_clients(&self) -> ClientList
pub async fn get_clients(&self) -> ClientList
Return list of clients.
Sourcepub async fn push_message(
&mut self,
client_id: &RUMString,
msg: RUMNetMessage,
) -> RUMResult<()>
pub async fn push_message( &mut self, client_id: &RUMString, msg: RUMNetMessage, ) -> RUMResult<()>
Queues a message onto the server to send to client.
Sourcepub async fn pop_message(
&mut self,
client_id: &RUMString,
) -> Option<RUMNetMessage>
pub async fn pop_message( &mut self, client_id: &RUMString, ) -> Option<RUMNetMessage>
Obtain a message, if available, from the incoming queue.
Sourcepub async fn wait_incoming(&mut self, client_id: &RUMString) -> RUMResult<bool>
pub async fn wait_incoming(&mut self, client_id: &RUMString) -> RUMResult<bool>
Obtain a message, if available, from the incoming queue.
Sourcepub async fn get_address_info(&self) -> Option<RUMString>
pub async fn get_address_info(&self) -> Option<RUMString>
Get the Address:Port info for this socket.
Sourcepub async fn gc_clients(&mut self) -> RUMResult<()>
pub async fn gc_clients(&mut self) -> RUMResult<()>
Attempts to clear clients that have been marked as disconnected.