-
Notifications
You must be signed in to change notification settings - Fork 75
Description
I'm trying to implement a codec for COBS to use together with a SerialFramed for sending and receiving postcard serialized objects to a microcontroller.
My issue is receiving frames that are not complete on each call to decode. The bytes arrive at a low enough pace for each decode call to be decode_eof even though the full frame has not been received. The decode (and also decode_eof) function return Ok(None) unless a full frame has been received and in that case the read buffer is cleared code on:
Line 67 in 0fc9637
| pin.rd.clear(); |
Looking at the documentation for the Decoder trait from tokio_util it states:
It is guaranteed that, from one call to
decodeto another, the provided buffer will contain the exact same data as before, except that if more data has arrived through the IO resource, that data will have been appended to the buffer.
Is this intended behaviour? My code works as expected if I comment the line referenced above.
A very simplifide pseudocode version of my decoder looks like this:
pub struct CobsCodec;
impl Decoder for CobsCodec {
type Error = CobsError;
type Item = Vec<u8>;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match decode_frame(&mut src) {
Success(frame, len) => {
src.advance(len);
Ok(Some(frame))
},
Incomplete() => Ok(None),
Corrupt() => Err(CobsError::Corrupted)
}
}
fn decode_eof(
&mut self,
buf: &mut BytesMut,
) -> Result<Option<Self::Item>, Self::Error> {
// Needs to be overridden since default implementation errors on frames crossing
// eof boundaries unsuitable for reading from resumable I/O
self.decode(buf)
}
}