chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 3 Jul 2020 22:51:27 +0000 (23:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 3 Jul 2020 22:58:46 +0000 (23:58 +0100)
Cargo.lock.example
Cargo.toml
src/bin/server.rs
src/global.rs
src/imports.rs
src/lib.rs
src/sse.rs [new file with mode: 0644]

index bcf5394fca517a08118708ea4724da93b9d76187..02f92c83363b65b10c21dd72eb94e8b4646648ba 100644 (file)
@@ -361,6 +361,7 @@ dependencies = [
  "fehler",
  "index_vec",
  "lazy_static",
+ "num-traits",
  "rand",
  "rocket",
  "rocket_contrib",
index 23b22596c448fa95d61b95dc8ec102689678be29..4df0ccce700f76fe5a7598a1cd9b1960fe321663 100644 (file)
@@ -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"] }
 
index 78869bb29176817da86c6b5fd66a8c452c7df1ac..958120e926607607686aeb6640e4030a8a4cc957 100644 (file)
@@ -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<Mutex<Instance>>>,
+  ami : Arc<Mutex<Instance>>,
 }
 
 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)]
index 423d26d4677890d5cc5bb9a33a4c1c0fa9429a3e..3ecdf43f41c3202555eab30157152001e9868ae1 100644 (file)
@@ -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<UpdateCounter,PreparedUpdate>,
+  pub log : StableIndexVecDeque<PreparedUpdate,UpdateCounter>,
   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
     );
index 92c6944794848a6169d47e75dd124eb23691cb52..a949f8f196238c110af1549651c67a36793cc5d9 100644 (file)
@@ -37,11 +37,14 @@ pub use slotmap::dense::DenseSlotMap;
 pub type SecondarySlotMap<K,V> = slotmap::secondary::SecondaryMap<K,V>;
 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;
index 57f7e6f054d367294a37a87c3f60b57ef74c3fa2..f8bd1e1aa57c55f44356e79d82455b8455469cb6 100644 (file)
@@ -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 (file)
index 0000000..c090c3e
--- /dev/null
@@ -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<usize> {
+    self.0.index_input(input.0)
+  }
+  fn index_output(&self, inner: usize) -> Option<Self> {
+    self.0.index_output(inner).map(|v| UpdateCounter(v))
+  }
+  fn zero() -> Self { UpdateCounter(0) }
+}