chiark / gitweb /
wip sse
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 3 Jul 2020 21:34:39 +0000 (22:34 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 3 Jul 2020 21:34:39 +0000 (22:34 +0100)
src/bin/server.rs
src/global.rs

index 65557f21f64f242a116d7c93411aa786c7646ec7..78869bb29176817da86c6b5fd66a8c452c7df1ac 100644 (file)
@@ -209,22 +209,63 @@ enum XUpdate {
 }
 
 const UPDATE_READER_SIZE : usize = 1024*32;
-const UPDATE_MAX_MSG_SIZE : usize = 1024;
+const UPDATE_MAX_FRAMING_SIZE : usize = 200;
+const UPDATE_KEEPALIVE : Duration = Duration::from_seconds(14);
 
 #[derive(Debug)]
 struct UpdateReader {
   playerid : PlayerId,
   client : ClientId,
-  last_sent : Counter, // xxx race for setting this initially
+  to_send : UpdateCounter, // xxx race for setting this initially
   ami : Arc<Mutex<Instance>>>,
 }
+
 impl Read for UpdateReader {
-  fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+  fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
     let amig = self.ami.lock()?;
+    let orig_wanted = buf.len();
+
+    let pu = &mut amig.updates.get(playerid)
+      .ok_or_else(|| io::Error::new
+                  (ErrorKind::Other, anyhow!("player gonee")))?;
+    loop {
+      let next = match pu.log.get(self.to_send) {
+        Some(next) => next,  None => { break }
+      };
+      let next_len = UPDATE_MAX_FRAMING_SIZE + next.json.len();
+      if next_len > buf.len() { break }
+
+      if next.client == self.client {
+        write!(buf, r#"
+event: recorded
+data: {{ gen: {}, piece: {}, cseq:{} }}
+"#,
+               &self.gen, &next.piece, &next.client_seq);
+      } else {
+        write!(buf, r#"
+id: {}
+data: {}
+"#,
+               &self.to_send,
+               &next.json);
+      }
+    }
+    loop {
+      let generated = orig_wanted - buf.len();
+      if generated > 0 { return generated }
+
+      (amig,_) = cv.wait_timeout(amig, UPDATE_KEEPALIVE)?;
+      write!(buf,r#"
+: keepalive
+"#);
+    }
+  }
+}
+
+    /*
     loop {
+                    e
       let send_from = (||{
-        let updates = &amig.updates.get(playerid)
-          .ok_or_else(|| anyhow!("player gone"))?
         let l = self.updates.len();
         let last_probe = match updates.last() {
           None => return None,
@@ -255,8 +296,6 @@ impl Read for UpdateReader {
     loop {
          implement this! 
     }
-                    e
-    /*
     for (tclient, tcl) in &mut g.clients {
       if tclient == client {
         tcl.transmit_update(&Update {
index 6578a01956a94b5b8a7104a0f927fcc204f9d67b..423d26d4677890d5cc5bb9a33a4c1c0fa9429a3e 100644 (file)
@@ -28,11 +28,11 @@ pub struct PreparedUpdate {
   client : ClientId,
   piece : PieceId;
   client_seq : ClientSequence,
-  json : Arc<String>,
+  json : String,
 }
 
 pub struct PlayerUpdates {
-  pub log : VecDeque<PreparedUpdate>,
+  pub log : StableIndexVecDeque<UpdateCounter,PreparedUpdate>,
   pub cv : Condvar,
 }