From: Ian Jackson Date: Fri, 3 Jul 2020 21:34:39 +0000 (+0100) Subject: wip sse X-Git-Tag: otter-0.2.0~1492 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=ded65f9547823ff6fef91d838bf75deb3588c856;p=otter.git wip sse --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 65557f21..78869bb2 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -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>>, } + impl Read for UpdateReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { + fn read(&mut self, mut buf: &mut [u8]) -> io::Result { 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 { diff --git a/src/global.rs b/src/global.rs index 6578a019..423d26d4 100644 --- a/src/global.rs +++ b/src/global.rs @@ -28,11 +28,11 @@ pub struct PreparedUpdate { client : ClientId, piece : PieceId; client_seq : ClientSequence, - json : Arc, + json : String, } pub struct PlayerUpdates { - pub log : VecDeque, + pub log : StableIndexVecDeque, pub cv : Condvar, }