Struct PacketReader
pub struct PacketReader<B> {
buf: B,
max_packet_size: Option<NonZeroU32>,
}Expand description
A buffering, sans-io packet framer.
Bytes are appended with feed in whatever chunks the
transport provides. Complete packets are then pulled out with
peek + decode +
consume.
The byte storage is supplied as a ByteBuffer, so the framer itself is
no-alloc: pair it with ArrayBuffer on no_std
targets or GrowBuffer when alloc is available (the
default).
Parse state is stored in the reader, never in a future. This lets an I/O
driver read in one cancellation-safe step (e.g. one
AsyncReadExt::read in a select!) and feed whatever it got. A cancelled
read does not consume any bytes, and bytes already fed are never lost. This
is the property a per-call read_exact loop lacks, and it also lets the
driver read in large chunks instead of one syscall per byte.
Fields§
§buf: B§max_packet_size: Option<NonZeroU32>Implementations§
§impl PacketReader<GrowBuffer>
impl PacketReader<GrowBuffer>
pub const fn new() -> Self
Available on crate feature alloc only.
pub const fn new() -> Self
alloc only.Creates an empty heap-backed reader with no packet-size limit.
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>
Available on crate feature alloc only.
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>
alloc only.Creates an empty heap-backed reader preallocating capacity bytes.
§Errors
Returns TryReserveError if the allocation fails.
§impl<B: ByteBuffer> PacketReader<B>
impl<B: ByteBuffer> PacketReader<B>
pub const fn from_buffer(buf: B) -> Self
pub const fn from_buffer(buf: B) -> Self
Creates a reader over the byte storage buf with no packet-size limit.
pub const fn set_max_packet_size(&mut self, max: NonZeroU32)
pub const fn set_max_packet_size(&mut self, max: NonZeroU32)
Rejects any packet whose total on-wire size exceeds max bytes.
peek returns DecodeError::TooLong as soon as the
announced size is known, before the body is buffered, so an oversized
declaration cannot force an unbounded allocation (§3.1.2.11.4).
pub fn buffered_len(&self) -> usize
pub fn buffered_len(&self) -> usize
Number of buffered bytes not yet consumed.
pub fn clear(&mut self)
pub fn clear(&mut self)
Drops all buffered bytes; call on connection loss to discard a stale partial packet before reusing the reader.
fn buffered(&self) -> &[u8] ⓘ
pub fn peek(&self) -> Result<Option<Frame>, DecodeError>
pub fn peek(&self) -> Result<Option<Frame>, DecodeError>
Inspects the next packet without consuming it.
Returns the Frame once a whole packet is buffered, or None if more
bytes are needed.
§Errors
Returns DecodeError if the fixed header is malformed, or
DecodeError::TooLong if the announced size exceeds the configured
set_max_packet_size.
pub fn decode(&self, frame: &Frame) -> Result<Packet<'_>, DecodeError>
pub fn decode(&self, frame: &Frame) -> Result<Packet<'_>, DecodeError>
Decodes the packet in frame.
frame must come from the latest peek, with nothing
consumed or cleared since. A stale frame may panic or point at the wrong
bytes.
§Errors
Returns DecodeError if the body is malformed.
pub fn body(&self, frame: &Frame) -> &[u8] ⓘ
pub fn body(&self, frame: &Frame) -> &[u8] ⓘ
Returns the body (variable header + payload) of the packet in frame,
so an I/O driver can send it across a task boundary before consuming
it.
frame must come from the latest peek, with nothing
consumed or cleared since. A stale frame may panic or point at the wrong
bytes.
Trait Implementations§
§impl<B: Debug> Debug for PacketReader<B>
impl<B: Debug> Debug for PacketReader<B>
§impl Default for PacketReader<GrowBuffer>
Available on crate feature alloc only.
impl Default for PacketReader<GrowBuffer>
alloc only.