Skip to content

Read buffer are cleared before frame fully decoded in SerialFramed #77

@LHelge

Description

@LHelge

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:

pin.rd.clear();

Looking at the documentation for the Decoder trait from tokio_util it states:

It is guaranteed that, from one call to decode to 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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions