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).
All parse state lives in the reader, never in a future. An I/O driver can
therefore read in a single cancellation-safe step (e.g. one
AsyncReadExt::read in a select!) and feed whatever it got: a cancelled
read consumes nothing, 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 with_capacity(capacity: usize) -> Self
Available on crate feature alloc only.
pub fn with_capacity(capacity: usize) -> Self
alloc only.Creates an empty heap-backed reader preallocating capacity bytes.
§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 given byte storage 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 feed(&mut self, chunk: &[u8]) -> Result<(), CapacityExceeded>
pub fn feed(&mut self, chunk: &[u8]) -> Result<(), CapacityExceeded>
Appends freshly read bytes.
§Errors
Returns CapacityExceeded if the underlying ByteBuffer is
fixed-capacity and full (never for GrowBuffer).
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<(FixedHeader, usize)>, DecodeError>
pub fn peek(&self) -> Result<Option<(FixedHeader, usize)>, DecodeError>
Inspects the next packet without consuming it.
Returns the fixed header and the total on-wire length (fixed header +
body) 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,
header: FixedHeader,
total: usize,
) -> Result<Packet<'_>, DecodeError>
pub fn decode( &self, header: FixedHeader, total: usize, ) -> Result<Packet<'_>, DecodeError>
Decodes the packet reported by peek.
header and total must come from the same peek call on an
unchanged buffer.
§Errors
Returns DecodeError if the body is malformed.
pub fn body(&self, header: FixedHeader, total: usize) -> &[u8]
pub fn body(&self, header: FixedHeader, total: usize) -> &[u8]
Returns the body (variable header + payload) of the packet reported by
peek, so an I/O driver can ship it across a task
boundary before consuming it. header and total must come from the
same peek call on an unchanged buffer.
Trait Implementations§
§impl Default for PacketReader<GrowBuffer>
Available on crate feature alloc only.
impl Default for PacketReader<GrowBuffer>
alloc only.