pub struct BlockingFrameReader<R> { /* private fields */ }Expand description
Blocking frame reader. Reads length-prefixed frames from any Read source.
Uses BufReader to amortize read syscalls — a single recv fills the
buffer with many frames, so subsequent read_exact calls hit the
buffer instead of making kernel transitions. This is critical for
round-trip latency: without buffering, each frame requires 2 read
syscalls (4-byte length prefix + payload).
Generic over the reader type so it works with both std::net::TcpStream
and std::os::unix::net::UnixStream.
Implementations§
Source§impl<R: Read> BlockingFrameReader<R>
impl<R: Read> BlockingFrameReader<R>
pub fn new(reader: R) -> Self
Sourcepub fn read_frame(&mut self) -> Result<Option<&[u8]>>
pub fn read_frame(&mut self) -> Result<Option<&[u8]>>
Read the next complete frame into the internal buffer.
Returns a borrowed slice of the frame payload, or None on clean
disconnect. The slice is valid until the next read_frame() call.
A prefix declaring more than MAX_FRAME_SIZE is
io::ErrorKind::InvalidData wrapping a FrameTooLarge
(reachable through io::Error::get_ref); the stream has no frame
boundary left to resume from.
Sourcepub fn frame(&self) -> &[u8] ⓘ
pub fn frame(&self) -> &[u8] ⓘ
The frame the last successful read_frame
returned, for callers that decided what it was and now want it
back without holding the borrow across that decision. Empty
before the first read.