Skip to main content

FrameReader

Struct FrameReader 

Source
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

Source

pub fn builder() -> FrameReaderBuilder

Create a builder.

Source

pub fn read(&mut self, src: &[u8]) -> Result<(), ReadError>

Buffer wire bytes from a source.

Source

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)?;
Source

pub fn spare(&mut self) -> &mut [u8]

Writable region for direct socket reads.

Source

pub fn filled(&mut self, n: usize)

Commit bytes written into spare().

Source

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.

Source

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.

Source

pub fn next(&mut self) -> Result<Option<Message<'_>>, ProtocolError>

Parse the next complete message.

Source

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.

Source

pub fn remaining(&self) -> usize

Bytes of buffer space remaining.

Source

pub fn buffered(&self) -> usize

Bytes of unconsumed data in the buffer.

Source

pub fn reset(&mut self)

Reset all state.

Trait Implementations§

Source§

impl Debug for FrameReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
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.

Source§

fn spare(&mut self) -> &mut [u8]

Writable region where new bytes go.
Source§

fn filled(&mut self, n: usize)

Commit n bytes written into spare(). Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V