pub struct FrameBuffer { /* private fields */ }Expand description
Per-connection receive buffer.
The 50ms read_timeout on TCP sockets is used to poll multiple peers without
blocking forever, but it means a read_exact inside read_frame can be
interrupted mid-frame on very slow links (e.g. 3 KB/s). When that happens
the partial bytes that were already pulled from the kernel buffer are lost,
which shifts every subsequent frame by some number of bytes and breaks the
length-prefix framing entirely.
FrameBuffer fixes this by accumulating all received bytes in buf and
only returning a complete frame once enough bytes are present. Partial reads
due to timeout just leave bytes in the buffer for the next poll cycle.
Implementations§
Source§impl FrameBuffer
impl FrameBuffer
pub fn new() -> Self
Sourcepub fn try_read_frame(
&mut self,
stream: &mut impl Read,
) -> ZamResult<Option<Vec<u8>>>
pub fn try_read_frame( &mut self, stream: &mut impl Read, ) -> ZamResult<Option<Vec<u8>>>
Try to return one complete decoded frame.
Reads as many bytes as the stream offers (stopping on WouldBlock or
TimedOut), then checks whether the accumulated buffer contains a full
length-prefixed frame.
Returns:
Ok(Some(payload))– a complete, decompressed frame is ready.Ok(None)– not enough bytes yet; call again after the next read opportunity.Err(_)– a real I/O or protocol error occurred.