From 6f779406e67b4c729943fd043f5726b140d6e640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 14 Aug 2024 19:17:02 +0400 Subject: [PATCH] refactor(blocking): let read_by_hint() optionally accumulate unmatched bytes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The caller can then decide what to do. Signed-off-by: Marc-André Lureau --- crates/ironrdp-blocking/src/connector.rs | 4 ++-- crates/ironrdp-blocking/src/framed.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/ironrdp-blocking/src/connector.rs b/crates/ironrdp-blocking/src/connector.rs index b5951513..9f782335 100644 --- a/crates/ironrdp-blocking/src/connector.rs +++ b/crates/ironrdp-blocking/src/connector.rs @@ -167,7 +167,7 @@ where ); let pdu = framed - .read_by_hint(next_pdu_hint) + .read_by_hint(next_pdu_hint, None) .map_err(|e| ironrdp_connector::custom_err!("read frame by hint", e))?; trace!(length = pdu.len(), "PDU received"); @@ -202,7 +202,7 @@ where ); let pdu = framed - .read_by_hint(next_pdu_hint) + .read_by_hint(next_pdu_hint, None) .map_err(|e| ironrdp_connector::custom_err!("read frame by hint", e))?; trace!(length = pdu.len(), "PDU received"); diff --git a/crates/ironrdp-blocking/src/framed.rs b/crates/ironrdp-blocking/src/framed.rs index c46109bb..bcc2242d 100644 --- a/crates/ironrdp-blocking/src/framed.rs +++ b/crates/ironrdp-blocking/src/framed.rs @@ -87,14 +87,21 @@ where } /// Reads a frame using the provided PduHint. - pub fn read_by_hint(&mut self, hint: &dyn PduHint) -> io::Result { + pub fn read_by_hint(&mut self, hint: &dyn PduHint, mut unmatched: Option<&mut Vec>) -> io::Result { loop { match hint .find_size(self.peek()) .map_err(|e| io::Error::new(io::ErrorKind::Other, e))? { - Some((_matched, length)) => { - return Ok(self.read_exact(length)?.freeze()); + Some((matched, length)) => { + let bytes = self.read_exact(length)?.freeze(); + if matched { + return Ok(bytes); + } else if let Some(ref mut unmatched) = unmatched { + unmatched.push(bytes); + } else { + warn!("Received and lost an unexpected PDU"); + } } None => { let len = self.read()?;