diff options
Diffstat (limited to 'games/rstnode/rst-core/src/mailbox.rs')
-rw-r--r-- | games/rstnode/rst-core/src/mailbox.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/mailbox.rs b/games/rstnode/rst-core/src/mailbox.rs new file mode 100644 index 000000000000..c7e4512dbeae --- /dev/null +++ b/games/rstnode/rst-core/src/mailbox.rs @@ -0,0 +1,78 @@ +use crate::wire::{ + game::{Action, Update}, + Lobby, +}; + +use async_std::sync::RwLock; +use std::collections::VecDeque; + +pub enum ClientUpdate { + /// Change to the lobby state + Lobby(Lobby), + /// A game simulation update + GameUpdate(Update), +} + +impl<'l> From<&'l Lobby> for ClientUpdate { + fn from(l: &'l Lobby) -> Self { + ClientUpdate::Lobby(l.clone()) + } +} + +impl<'l> From<&'l Update> for ClientUpdate { + fn from(u: &'l Update) -> Self { + ClientUpdate::GameUpdate(u.clone()) + } +} + +/// A message out buffer that can be attached to any server entity +pub struct Outbox { + queue: RwLock<VecDeque<ClientUpdate>>, +} + +impl Outbox { + pub fn new() -> Self { + Self { + queue: Default::default(), + } + } + + /// Queue a new item to send out + pub async fn queue(&self, update: impl Into<ClientUpdate>) { + let mut q = self.queue.write().await; + q.push_back(update.into()); + } + + /// Run a closure for all queued items + pub async fn run_for<F: Fn(&ClientUpdate)>(&self, handle: F) { + let q = self.queue.read().await; + q.iter().for_each(|item| handle(item)); + } + + /// Clear the outbox for the next update interval + pub async fn clear(&self) { + self.queue.write().await.clear(); + } +} + +pub struct Inbox { + queue: RwLock<VecDeque<Action>>, +} + +impl Inbox { + pub fn new() -> Self { + Self { + queue: Default::default(), + } + } + + /// Queue a new item to send out + pub async fn queue(&self, update: impl Into<Action>) { + let mut q = self.queue.write().await; + q.push_back(update.into()); + } + + pub async fn pop(&self) -> Option<Action> { + self.queue.write().await.pop_front() + } +} |