pub struct FrameReader { /* private fields */ }Expand description
WebSocket frame reader — parses wire bytes into Messages.
Handles wire frame parsing, fragment assembly, control frame
interleaving, masking, and UTF-8 validation. The user sees complete
Message values — never raw frames or continuations.
§Usage
use nexus_net::ws::{FrameReader, Role, Message};
let mut reader = FrameReader::builder()
.role(Role::Client)
.buffer_capacity(65_536)
.build();
// Feed wire bytes
reader.read(&[0x81, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]).unwrap();
// Parse messages
match reader.next().unwrap().unwrap() {
Message::Text(s) => assert_eq!(s, "Hello"),
_ => panic!("expected text"),
}Implementations§
Source§impl FrameReader
impl FrameReader
Sourcepub fn builder() -> FrameReaderBuilder
pub fn builder() -> FrameReaderBuilder
Create a builder.
Sourcepub fn read_from<R>(&mut self, src: &mut R) -> Result<usize, Error>where
R: Read,
pub fn read_from<R>(&mut self, src: &mut R) -> Result<usize, Error>where
R: Read,
Read bytes from a source directly into the internal buffer.
Convenience for spare() + filled(). Returns bytes read,
or 0 on EOF. Returns Err if the buffer is full after compaction
(indicates the buffer is undersized for the current message).
let n = reader.read_from(&mut socket)?;Sourcepub fn compact(&mut self)
pub fn compact(&mut self)
Reclaim consumed buffer space by moving unconsumed data to the front.
Call when spare() is empty but there’s still data to read.
This is O(n) in the amount of unconsumed data.
Sourcepub fn should_compact(&self) -> bool
pub fn should_compact(&self) -> bool
Whether the ReadBuf should be compacted based on the configured threshold.
Returns true when at least one byte has been consumed, consumed bytes
meet or exceed the threshold set by FrameReaderBuilder::compact_at,
and there is unconsumed data to preserve.
Default threshold is 50% of buffer capacity.
Sourcepub fn next(&mut self) -> Result<Option<Message<'_>>, ProtocolError>
pub fn next(&mut self) -> Result<Option<Message<'_>>, ProtocolError>
Parse the next complete message.
Sourcepub fn poll(&mut self) -> Result<bool, ProtocolError>
pub fn poll(&mut self) -> Result<bool, ProtocolError>
Advance the parser without constructing a Message.
Returns true if the next call to next() will return a message.
Trait Implementations§
Source§impl Debug for FrameReader
impl Debug for FrameReader
Source§impl ParserSink for FrameReader
Lets a WireStream feed bytes directly into
the FrameReader’s spare region — one fewer copy than going through
a slice intermediary.
impl ParserSink for FrameReader
Lets a WireStream feed bytes directly into
the FrameReader’s spare region — one fewer copy than going through
a slice intermediary.