From 191ca1796a021c3183dc1465e7a3efcc72d833d9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 11 Jul 2021 11:56:37 +0100 Subject: [PATCH] sse: Move to daemon We might perhaps be going to want this to use some macros from our web framework. (Commit originally intended for Rocket 0.5 branch.) Signed-off-by: Ian Jackson --- daemon/main.rs | 1 + {src => daemon}/sse.rs | 40 +--------------------------------------- src/lib.rs | 1 - src/prelude.rs | 2 +- src/updates.rs | 39 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 41 insertions(+), 42 deletions(-) rename {src => daemon}/sse.rs (87%) diff --git a/daemon/main.rs b/daemon/main.rs index 8d8da1bf..2dcf5757 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -11,6 +11,7 @@ pub mod imports; pub mod api; pub mod cmdlistener; pub mod session; +pub mod sse; pub use rocket::http::Status; pub use rocket::http::{ContentType, RawStr}; diff --git a/src/sse.rs b/daemon/sse.rs similarity index 87% rename from src/sse.rs rename to daemon/sse.rs index 8f33e5a2..4e1dc387 100644 --- a/src/sse.rs +++ b/daemon/sse.rs @@ -5,18 +5,10 @@ #![allow(clippy::while_let_loop)] #![allow(clippy::blocks_in_if_conditions)] -use crate::prelude::*; - -use vecdeque_stableix::Offset as StableIndexOffset; -use std::ops::Neg; +use otter::prelude::*; // ---------- basic definitions ---------- -#[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd)] -#[derive(Serialize,Deserialize)] -#[serde(transparent)] -pub struct UpdateId(i64); - const UPDATE_READER_SIZE: usize = 1024*32; const UPDATE_MAX_FRAMING_SIZE: usize = 200; const UPDATE_KEEPALIVE: Duration = Duration::from_secs(14); @@ -224,36 +216,6 @@ impl Read for UpdateReader { } } -// ---------- support implementation ---------- - -impl Neg for UpdateId { - type Output = Self; - fn neg(self) -> Self { UpdateId(-self.0) } -} - -impl Display for UpdateId { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - Display::fmt(&self.0, f) - } -} - -impl Bounded for UpdateId { - fn max_value() -> Self { UpdateId(Bounded::max_value()) } - fn min_value() -> Self { UpdateId(Bounded::min_value()) } -} - -impl StableIndexOffset for UpdateId { - fn try_increment(&mut self) -> Option<()> { self.0.try_increment() } - fn try_decrement(&mut self) -> Option<()> { self.0.try_decrement() } - fn index_input(&self, input: Self) -> Option { - self.0.index_input(input.0) - } - fn index_output(&self, inner: usize) -> Option { - self.0.index_output(inner).map(UpdateId) - } - fn zero() -> Self { UpdateId(0) } -} - // ---------- entrypoint for dribbling the http response ---------- #[throws(Fatal)] diff --git a/src/lib.rs b/src/lib.rs index fbeacecb..232001c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,6 @@ pub mod pieces; pub mod progress; pub mod shapelib; pub mod spec; -pub mod sse; pub mod termprogress; pub mod timedfd; pub mod tz; diff --git a/src/prelude.rs b/src/prelude.rs index 1db1ef26..94bbb63f 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -104,6 +104,7 @@ pub use thiserror::Error; pub use unicase::UniCase; pub use url::Url; pub use vecdeque_stableix::Deque as StableIndexVecDeque; +pub use vecdeque_stableix::Offset as StableIndexOffset; pub use void::{unreachable, Void, ResultVoidExt, ResultVoidErrExt}; pub use crate::imports::zipfile::{self, read::ZipFile, result::ZipError}; @@ -162,7 +163,6 @@ pub use crate::shapelib::{LibraryLoadError}; pub use crate::slotmap_slot_idx::*; pub use crate::spec::*; pub use crate::spec::piece_specs::{FaceColourSpecs, SimpleCommon}; -pub use crate::sse; pub use crate::toml_de; pub use crate::timedfd::*; pub use crate::termprogress; diff --git a/src/updates.rs b/src/updates.rs index 69a3ff07..daa2af62 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -6,6 +6,8 @@ use crate::prelude::*; +use std::ops::Neg; + #[path="movehist.rs"] pub mod movehist; #[allow(non_camel_case_types)] type TUE_P<'u> = TransmitUpdateEntry_Piece<'u>; @@ -14,6 +16,11 @@ use crate::prelude::*; // ---------- newtypes, type aliases, basic definitions ---------- +#[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd)] +#[derive(Serialize,Deserialize)] +#[serde(transparent)] +pub struct UpdateId(i64); + pub type RawClientSequence = u64; #[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)] @@ -37,7 +44,7 @@ pub struct ExecuteGameChangeUpdates { // ---------- prepared updates, queued in memory ---------- pub type PlayerUpdatesLog = - StableIndexVecDeque,sse::UpdateId>; + StableIndexVecDeque,UpdateId>; #[derive(Debug)] pub struct PlayerUpdates { @@ -337,6 +344,36 @@ pub fn log_did_to_piece(ioccults: &IOccults, goccults: &GameOccults, log_did_to_piece_whoby(ioccults,goccults,by_gpl,gpc,ipc,did)?.0 } +// ---------- support implementation ---------- + +impl Neg for UpdateId { + type Output = Self; + fn neg(self) -> Self { UpdateId(-self.0) } +} + +impl Display for UpdateId { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + Display::fmt(&self.0, f) + } +} + +impl Bounded for UpdateId { + fn max_value() -> Self { UpdateId(Bounded::max_value()) } + fn min_value() -> Self { UpdateId(Bounded::min_value()) } +} + +impl StableIndexOffset for UpdateId { + fn try_increment(&mut self) -> Option<()> { self.0.try_increment() } + fn try_decrement(&mut self) -> Option<()> { self.0.try_decrement() } + fn index_input(&self, input: Self) -> Option { + self.0.index_input(input.0) + } + fn index_output(&self, inner: usize) -> Option { + self.0.index_output(inner).map(UpdateId) + } + fn zero() -> Self { UpdateId(0) } +} + // ---------- prepared updates, queued in memory ---------- pub struct PlayerUpdatesBuildContext { -- 2.30.2