From: Ian Jackson Date: Sat, 1 May 2021 15:35:47 +0000 (+0100) Subject: move mgmtchannel read/write to methods on Framed X-Git-Tag: otter-0.6.0~490 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f6626a4ea7da34d276fb39226c07769405b6cc1f;p=otter.git move mgmtchannel read/write to methods on Framed This will allow us to borrow the read and write halves of a channel separately, for doing bulk upload/download. Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 45f56c9c..e8636f4d 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -1153,7 +1153,7 @@ impl CommandStream<'_> { pub fn mainloop(mut self) { loop { use MgmtChannelReadError::*; - let resp = match self.chan.read::() { + let resp = match self.chan.read.read::() { Ok(cmd) => { let mut cmd_s = log_enabled!(log::Level::Info) .as_some_from(|| format!("{:?}", &cmd)) @@ -1180,7 +1180,7 @@ impl CommandStream<'_> { Err(IO(e)) => Err(e).context("read command stream")?, Err(Parse(s)) => MgmtResponse::Error { error: ME::ParseFailed(s) }, }; - self.chan.write(&resp).context("swrite command stream")?; + self.chan.write.write(&resp).context("swrite command stream")?; } } } diff --git a/src/mgmtchannel.rs b/src/mgmtchannel.rs index 2b763ca7..cf54988b 100644 --- a/src/mgmtchannel.rs +++ b/src/mgmtchannel.rs @@ -35,8 +35,8 @@ impl From for MgmtChannelWriteError { } pub struct MgmtChannel { - read: FrameReader>>, - write: FrameWriter>>, + pub read: FrameReader>>, + pub write: FrameWriter>>, } impl Debug for MgmtChannel{ @@ -68,49 +68,11 @@ impl MgmtChannel { MgmtChannel { read, write } } - #[throws(MgmtChannelReadError)] - pub fn read_withbulk<'c,T>(&'c mut self) -> (T, ReadFrame) - where T: DeserializeOwned + Debug - { - use MgmtChannelReadError::*; - let mut f = self.read.new_frame()?.ok_or(MgmtChannelReadError::EOF)?; - let r = rmp_serde::decode::from_read(&mut f); - let v = r.map_err(|e| Parse(format!("{}", &e)))?; - trace!("read OK {:?}", &v); - (v, f) - } - - #[throws(MgmtChannelReadError)] - pub fn read(&mut self) -> T - where T: DeserializeOwned + Debug - { - self.read_withbulk()?.0 - } - - #[throws(MgmtChannelWriteError)] - pub fn write_withbulk<'c,T>(&mut self, val: &T) - -> WriteFrame - where T: Serialize + Debug - { - let mut f = self.write.new_frame()?; - rmp_serde::encode::write_named(&mut f, val)?; - trace!("writing {:?}", val); - f - } - - #[throws(MgmtChannelWriteError)] - pub fn write(&mut self, val: &T) - where T: Serialize + Debug - { - let f = self.write_withbulk(val)?; - f.finish()?; - } - #[throws(AE)] pub fn cmd(&mut self, cmd: &MgmtCommand) -> MgmtResponse { use MgmtResponse::*; - self.write(&cmd).context("send command")?; - let resp = self.read().context("read response")?; + self.write.write(&cmd).context("send command")?; + let resp = self.read.read().context("read response")?; match &resp { Fine | AccountsList{..} | GamesList{..} | LibraryItems(_) => { }, AlterGame { error: None, .. } => { }, diff --git a/src/packetframe.rs b/src/packetframe.rs index c7c6d633..2fa958ee 100644 --- a/src/packetframe.rs +++ b/src/packetframe.rs @@ -263,7 +263,26 @@ impl FrameReader { Err(RHE::IO(e)) => throw!(e), } } - } + } + + #[throws(MgmtChannelReadError)] + pub fn read_withbulk<'c,T>(&'c mut self) -> (T, ReadFrame) + where T: DeserializeOwned + Debug + { + use MgmtChannelReadError::*; + let mut f = self.new_frame()?.ok_or(MgmtChannelReadError::EOF)?; + let r = rmp_serde::decode::from_read(&mut f); + let v = r.map_err(|e| Parse(format!("{}", &e)))?; + trace!("read OK {:?}", &v); + (v, f) + } + + #[throws(MgmtChannelReadError)] + pub fn read(&mut self) -> T + where T: DeserializeOwned + Debug + { + self.read_withbulk()?.0 + } } trait ReadOutput { @@ -324,6 +343,25 @@ impl FrameWriter { self.inner.flush()?; } } + + #[throws(MgmtChannelWriteError)] + pub fn write_withbulk<'c,T>(&'c mut self, val: &T) + -> WriteFrame + where T: Serialize + Debug + { + let mut f = self.new_frame()?; + rmp_serde::encode::write_named(&mut f, val)?; + trace!("writing {:?}", val); + f + } + + #[throws(MgmtChannelWriteError)] + pub fn write(&mut self, val: &T) + where T: Serialize + Debug + { + let f = self.write_withbulk(val)?; + f.finish()?; + } } impl<'w,W:Write> WriteFrame<'w,W> {