From 184ac265eaef7ecfa1264ec757d3c3769a8fdaa1 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 6 Jul 2020 00:29:49 +0100 Subject: [PATCH] total reorg, does not work any more --- src/bin/server.rs | 9 ++++--- src/gamestate.rs | 14 ++++++++--- src/global.rs | 59 ++++++++++++++++++++++++++++++++------------- src/sse.rs | 61 +++++++++++++++++++++++++++-------------------- src/updates.rs | 18 +++----------- 5 files changed, 96 insertions(+), 65 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index 0da139ff..bcd0d126 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -152,16 +152,15 @@ fn api_grab(form : Json) -> impl response::Responder<'static> { id : vpiece, face : p.face, }; - let json = UpdatePayload::PieceUpdate(vpiece, p.mk_update(&pri)); - let json = serde_json::to_string(&json).expect("convert to json"); + let op = PieceUpdateOp::Modify(p.prep_piecestate(&pri)); let update = PreparedUpdate { gen, - u : PreparedPieceUpdate { + us : vec![ PreparedUpdateEntry::Piece { client, sameclient_cseq : form.cseq, piece : vpiece, - json, - }, + op, + }], }; let update = Arc::new(update); // split vie wthing would go here, see also update.piece diff --git a/src/gamestate.rs b/src/gamestate.rs index 928a0eef..0855700c 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -61,6 +61,13 @@ pub struct PieceRecord { pub gen_before_lastclient : Generation, } +#[derive(Debug,Serialize)] +pub struct PreparedPieceState { + pub pos : Pos, + pub held : Option, + pub svg : String, +} + impl PieceRecord { pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String { let pr = self; @@ -75,11 +82,12 @@ impl PieceRecord { defs } - pub fn mk_update(&self, pri : &PieceRenderInstructions) -> PieceUpdate { - PieceUpdate { + pub fn prep_piecestate(&self, pri : &PieceRenderInstructions) + -> PreparedPieceState { + PreparedPieceState { pos : self.pos, held : self.held, - svgs : self.make_defs(pri), + svg : self.make_defs(pri), // xxx want all piece's stuff in the same def } } diff --git a/src/global.rs b/src/global.rs index b90c371f..8f86452a 100644 --- a/src/global.rs +++ b/src/global.rs @@ -18,34 +18,61 @@ pub struct Client { pub player : PlayerId, } -impl Client { - pub fn transmit_update(&mut self, u : &Update) { - eprintln!("XXX not transmitting {:?}", &u); - } -} - #[derive(Debug)] pub struct PreparedUpdate { pub gen : Generation, - pub u : PreparedUpdatePayload, + pub us : Vec, } #[derive(Debug)] -pub enum PreparedUpdatePayload { - PreparedPieceUpdate { +pub enum PreparedUpdateEntry { + Piece { client : ClientId, sameclient_cseq : ClientSequence, piece : VisiblePieceId, - json : String, + op : PieceUpdateOp, + }, + Log { }, } -pub use PreparedUpdatePayload::*; - -impl PreparedUpdatePayload { - pub fn json_len(&self) -> usize { match self { - &PreparedPieceUpdate { ref json, .. } => json.len(), - } } +impl PreparedUpdateEntry { + pub fn json_len(&self) -> usize { + use PreparedUpdateEntry::*; + match self { + Piece { ref op, .. } => { + 50 + + op.new_state().map(|x| x.svg.len()).unwrap_or(0) + }, + Log { .. } => { + todo!("json length of log") + } + } + } +} +impl PreparedUpdate { + pub fn json_len(&self) -> usize { + self.us.iter().map(|u| 20 + u.json_len()).sum() + } } +#[derive(Debug,Serialize)] +pub enum PieceUpdateOp { + Delete, + Insert(NS), + Modify(NS), + Move(Pos), +} +impl PieceUpdateOp { + pub fn new_state(&self) -> Option<&NS> { + use PieceUpdateOp::*; + match self { + Delete => None, + Insert(ns) => Some(ns), + Modify(ns) => Some(ns), + Move(_) => None, + } + } +} + #[derive(Debug,Default)] pub struct PlayerUpdates { pub log : StableIndexVecDeque,sse::UpdateId>, diff --git a/src/sse.rs b/src/sse.rs index 49e2c815..af6d62b1 100644 --- a/src/sse.rs +++ b/src/sse.rs @@ -46,11 +46,18 @@ struct UpdateReader { ami : Arc>, } -#[derive(Serialize)] -struct RecordedConfirmation { - gen : Generation, - piece : VisiblePieceId, - cseq : ClientSequence, +#[derive(Debug,Serialize)] +enum TransmitUpdate<'u> { + Recorded { + piece : VisiblePieceId, + cseq : ClientSequence, + }, + Piece { + piece : VisiblePieceId, + op : &'u PieceUpdateOp, + }, + Log { + }, } #[derive(Error,Debug)] @@ -81,31 +88,33 @@ impl Read for UpdateReader { let next = match pu.log.get(self.to_send) { Some(next) => next, None => { break } }; - let next_len = UPDATE_MAX_FRAMING_SIZE + next.u.json_len(); + let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len(); if next_len > buf.len() { break } - match &next.u { - &PreparedPieceUpdate { - piece, client : uclient, sameclient_cseq : cseq, .. - } if uclient== self.client => { - write!(buf, "event: recorded\n\ - data: ")?; - serde_json::to_writer(&mut buf, &RecordedConfirmation { - gen : next.gen, - piece : piece, - cseq : cseq, - })?; - write!(buf, "\n\n")?; - }, - PreparedPieceUpdate { json, .. } => { - write!(buf, "id: {}\n\ - data: {}\n\n", - &self.to_send, - json)?; - }, + write!(buf, "data: [")?; + for u in &next.us { + let tu = match u { + &PreparedUpdateEntry::Piece + { piece, client, sameclient_cseq : cseq, .. } + if client== self.client => { + TransmitUpdate::Recorded { piece, cseq } + }, + &PreparedUpdateEntry::Piece { piece, ref op, .. } => { + TransmitUpdate::Piece { piece, op } + }, + PreparedUpdateEntry::Log { } => { + TransmitUpdate::Log { } + }, + }; + serde_json::to_writer(&mut buf, &tu)?; + write!(buf,",")?; } - self.to_send.try_increment().unwrap(); + serde_json::to_writer(&mut buf, &next.gen)?; + write!(buf, "]\n\ + id: {}\n\n", + &self.to_send)?; } + loop { let generated = orig_wanted - buf.len(); if generated > 0 { diff --git a/src/updates.rs b/src/updates.rs index 872cd8ec..0d1b6d8a 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -4,7 +4,7 @@ use crate::imports::*; #[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)] #[serde(transparent)] pub struct ClientSequence(u64); - +/* #[derive(Debug,Serialize)] pub struct Update { pub gen : Generation, @@ -12,18 +12,6 @@ pub struct Update { } #[derive(Debug,Serialize)] -pub struct PieceUpdate { - pub pos : Pos, - pub held : Option, - pub svgs : String, -} - -#[derive(Debug,Serialize)] -pub enum UpdatePayload { - NoUpdate, - ClientSequence(PieceId, ClientSequence), - PieceDelete(VisiblePieceId), - PieceInsert(VisiblePieceId, PieceUpdate), - PieceUpdate(VisiblePieceId, PieceUpdate), - PieceMove(VisiblePieceId, Pos), +pub enum PieceUpdate { } +*/ -- 2.30.2