-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read buffer are cleared before frame fully decoded in SerialFramed
#77
Comments
Simplest code example I could think of to reproduce the problem: #[cfg(test)]
mod tests {
use futures::StreamExt;
use std::time::Duration;
use tokio::{io::AsyncWriteExt, join, time::sleep};
use tokio_serial::{frame::SerialFramed, SerialStream};
use tokio_util::codec::LinesCodec;
// PASS
#[tokio::test]
async fn cobs_frames_async() {
let (mut master, slave) = SerialStream::pair().unwrap();
let mut slave_framed = SerialFramed::new(slave, LinesCodec::new());
let sender = async {
sleep(Duration::from_millis(100)).await;
master.write_all(b"Hello, ").await.unwrap();
master.write_all(b"world!\n").await.unwrap();
};
let receiver = async { slave_framed.next().await.unwrap().unwrap() };
let (_, data) = join!(sender, receiver);
assert_eq!(data, "Hello, world!");
}
// FAIL
#[tokio::test]
async fn cobs_frames_async_eof() {
let (mut master, slave) = SerialStream::pair().unwrap();
let mut slave_framed = SerialFramed::new(slave, LinesCodec::new());
let sender = async {
sleep(Duration::from_millis(100)).await;
master.write_all(b"Hello, ").await.unwrap();
sleep(Duration::from_millis(100)).await;
master.write_all(b"world!\n").await.unwrap();
};
let receiver = async { slave_framed.next().await.unwrap().unwrap() };
let (_, data) = join!(sender, receiver);
assert_eq!(data, "Hello, world!");
}
} |
And I'm not sure if it makes sense to use #[tokio::test]
async fn cobs_frames_length_delimited_async_eof() {
let (mut master, slave) = SerialStream::pair().unwrap();
let mut slave_framed = SerialFramed::new(slave, tokio_util::codec::LengthDelimitedCodec::default());
let data = [0x11, 0x22, 0x33, 0x44];
let sender = async {
sleep(Duration::from_millis(100)).await;
master.write_u32(data.len() as u32).await.unwrap();
master.write_all(&data[..1]).await.unwrap();
sleep(Duration::from_millis(100)).await;
master.write_all(&data[1..]).await.unwrap();
};
let receiver = async { slave_framed.next().await.unwrap().unwrap() };
let (_, received) = join!(sender, receiver);
assert_eq!(received, BytesMut::from(&data[..]));
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 bedecode_eof
even though the full frame has not been received. Thedecode
(and alsodecode_eof
) function returnOk(None)
unless a full frame has been received and in that case the read buffer is cleared code on:tokio-serial/src/frame.rs
Line 67 in 0fc9637
Looking at the documentation for the Decoder trait from
tokio_util
it states: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:
The text was updated successfully, but these errors were encountered: