refactor(blocking): let read_by_hint() optionally accumulate unmatched bytes

The caller can then decide what to do.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2024-08-14 19:17:02 +04:00 committed by Benoît Cortier
parent 98e7dbab99
commit 6f779406e6
2 changed files with 12 additions and 5 deletions

View file

@ -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");

View file

@ -87,14 +87,21 @@ where
}
/// Reads a frame using the provided PduHint.
pub fn read_by_hint(&mut self, hint: &dyn PduHint) -> io::Result<Bytes> {
pub fn read_by_hint(&mut self, hint: &dyn PduHint, mut unmatched: Option<&mut Vec<Bytes>>) -> io::Result<Bytes> {
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()?;