aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/mailbox.rs
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2021-02-14 00:06:14 +0100
committerKatharina Fey <kookie@spacekookie.de>2021-02-14 00:06:14 +0100
commiteffbdeed66e8de8e769b8ac069926ad1a9110e62 (patch)
treee4522354e53266204aa9962caccde55d8c815092 /games/rstnode/rst-core/src/mailbox.rs
parent5dab336049dbc6817e9ff212998690f59f6bbfa8 (diff)
rstnode: refactoring server and client components into rst-coreHEADmaster
* Add an inbox/ outbox system to server components * Define a data flow from Request -> computation -> Update * Create simple handlers to call server or client code for requests
Diffstat (limited to 'games/rstnode/rst-core/src/mailbox.rs')
-rw-r--r--games/rstnode/rst-core/src/mailbox.rs78
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()
+ }
+}