From: Ian Jackson Date: Fri, 3 Jul 2020 22:51:27 +0000 (+0100) Subject: wip X-Git-Tag: otter-0.2.0~1491 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=32942bee9f32d7df37701be3093dcdfa270fee1e;p=otter.git wip --- diff --git a/Cargo.lock.example b/Cargo.lock.example index bcf5394f..02f92c83 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -361,6 +361,7 @@ dependencies = [ "fehler", "index_vec", "lazy_static", + "num-traits", "rand", "rocket", "rocket_contrib", diff --git a/Cargo.toml b/Cargo.toml index 23b22596..4df0ccce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ fehler = "1" index_vec = "0.1.1" +num-traits = "0.2" + rocket = "0.4" rocket_contrib = { version = "0.4", default-features=false, features=["tera_templates","helmet","json"] } diff --git a/src/bin/server.rs b/src/bin/server.rs index 78869bb2..958120e9 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -214,10 +214,10 @@ const UPDATE_KEEPALIVE : Duration = Duration::from_seconds(14); #[derive(Debug)] struct UpdateReader { - playerid : PlayerId, + player : PlayerId, client : ClientId, to_send : UpdateCounter, // xxx race for setting this initially - ami : Arc>>, + ami : Arc>, } impl Read for UpdateReader { @@ -225,9 +225,9 @@ impl Read for UpdateReader { let amig = self.ami.lock()?; let orig_wanted = buf.len(); - let pu = &mut amig.updates.get(playerid) + let pu = &mut amig.updates.get(self.player) .ok_or_else(|| io::Error::new - (ErrorKind::Other, anyhow!("player gonee")))?; + (io::ErrorKind::Other, anyhow!("player gonee")))?; loop { let next = match pu.log.get(self.to_send) { Some(next) => next, None => { break } @@ -254,7 +254,7 @@ data: {} let generated = orig_wanted - buf.len(); if generated > 0 { return generated } - (amig,_) = cv.wait_timeout(amig, UPDATE_KEEPALIVE)?; + amig = self.cv.wait_timeout(amig, UPDATE_KEEPALIVE)?.0; write!(buf,r#" : keepalive "#); @@ -307,7 +307,7 @@ data: {} } } */ - +/* thread::sleep(Duration::from_millis(500)); let message = XUpdate::TestCounter { value : self.next }; @@ -318,7 +318,7 @@ data: {} buf[0..data.len()].copy_from_slice(data.as_bytes()); Ok(buf.len()) } -} +}*/ /* #[derive(Deserialize)] diff --git a/src/global.rs b/src/global.rs index 423d26d4..3ecdf43f 100644 --- a/src/global.rs +++ b/src/global.rs @@ -26,13 +26,13 @@ impl Client { pub struct PreparedUpdate { gen : Counter, client : ClientId, - piece : PieceId; + piece : PieceId, client_seq : ClientSequence, json : String, } pub struct PlayerUpdates { - pub log : StableIndexVecDeque, + pub log : StableIndexVecDeque, pub cv : Condvar, } @@ -126,7 +126,7 @@ pub fn xxx_global_setup() { nick : nick.to_string(), }; let player = ig.gs.players.insert(np); - let ia = InstanceAccessDetails { g : g.clone(), ident : player }; + let ia = InstanceAccessDetails { g : amu.clone(), ident : player }; GLOBAL.players.write().unwrap().insert( RawToken(token.to_string()), ia ); diff --git a/src/imports.rs b/src/imports.rs index 92c69447..a949f8f1 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -37,11 +37,14 @@ pub use slotmap::dense::DenseSlotMap; pub type SecondarySlotMap = slotmap::secondary::SecondaryMap; pub use index_vec::{define_index_type,index_vec,IndexVec}; +pub use vecdeque_stableix::StableIndexVecDeque; + pub use crate::global::*; pub use crate::gamestate::*; pub use crate::pieces::*; pub use crate::keydata::*; pub use crate::updates::*; +pub use crate::sse::*; pub type E = anyhow::Error; pub type AE = anyhow::Error; diff --git a/src/lib.rs b/src/lib.rs index 57f7e6f0..f8bd1e1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,4 @@ pub mod pieces; pub mod gamestate; pub mod keydata; pub mod updates; +pub mod sse; diff --git a/src/sse.rs b/src/sse.rs new file mode 100644 index 00000000..c090c3e4 --- /dev/null +++ b/src/sse.rs @@ -0,0 +1,27 @@ + +use crate::imports::*; + +#[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd)] +#[derive(Serialize,Deserialize)] +#[serde(transparent)] +pub struct UpdateCounter (i64); + +use vecdeque_stableix::StableIndexOffset; +use std::ops::Neg; + +impl Neg for UpdateCounter { + type Output = Self; + fn neg(self) -> Self { UpdateCounter(-self.0) } +} + +impl StableIndexOffset for UpdateCounter { + 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(|v| UpdateCounter(v)) + } + fn zero() -> Self { UpdateCounter(0) } +}