Skip to main content

StreamMultiplexer

Struct StreamMultiplexer 

Source
pub struct StreamMultiplexer {
    pub config: MultiplexerConfig,
    /* private fields */
}
Expand description

Multiplexes multiple logical streams over a single connection with flow control and priority-based scheduling.

§Example

use ipfrs_network::stream_multiplexer::{
    MultiplexerConfig, StreamMultiplexer, StreamPriority,
};

let config = MultiplexerConfig::default();
let mut mux = StreamMultiplexer::new(config);

let sid = mux.open_stream(StreamPriority::Normal).expect("open stream");
let bytes = mux.send(sid, b"hello world".to_vec(), 0).expect("send");
assert_eq!(bytes, 11);

// Drain all enqueued frames (SYN + data)
let frames = mux.dequeue_frames(10);
assert_eq!(frames.len(), 2);

Fields§

§config: MultiplexerConfig

Effective configuration for this multiplexer instance.

Implementations§

Source§

impl StreamMultiplexer

Source

pub fn new(config: MultiplexerConfig) -> Self

Create a new multiplexer with the given configuration.

Source

pub fn open_stream( &mut self, priority: StreamPriority, ) -> Result<StreamId, MuxError>

Open a new logical stream with the specified priority.

A SYN frame (flags = FLAG_SYN) is immediately enqueued.

§Errors

Returns MuxError::MaxStreamsReached if MultiplexerConfig::max_streams streams are already open.

Source

pub fn open_stream_with_priority( &mut self, priority_raw: u8, ) -> Result<StreamId, MuxError>

Open a new logical stream with a raw priority byte (0-255).

The raw byte is mapped to the nearest StreamPriority tier via priority_from_u8. The raw value is preserved in LogicalStream::priority_raw.

§Errors

Returns MuxError::MaxStreamsReached if the maximum stream count is exceeded.

Source

pub fn close_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>

Send a FIN frame and transition the stream to StreamState::HalfClosed.

§Errors
Source

pub fn reset_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>

Send an RST frame and transition the stream to StreamState::Reset.

§Errors
Source

pub fn send( &mut self, stream_id: StreamId, data: Vec<u8>, now: u64, ) -> Result<usize, MuxError>

Fragment data into frames of at most MultiplexerConfig::max_frame_size bytes and enqueue them on the send queue.

Returns the total number of bytes enqueued.

§Errors
Source

pub fn receive_events( &mut self, frame: StreamFrame, now: u64, ) -> Result<Vec<MuxEvent>, MuxError>

Process an incoming frame from the remote peer.

Returns a list of MuxEvents describing what happened:

§Errors
Source

pub fn receive( &mut self, frame: StreamFrame, _now: u64, ) -> Result<Vec<u8>, MuxError>

Process an incoming frame from the remote peer (original API).

Handles RST, FIN, and data frames. Returns the payload bytes.

§Errors
Source

pub fn drain_recv_buffer( &mut self, stream_id: StreamId, ) -> Result<Vec<Vec<u8>>, MuxError>

Drain all buffered receive payloads for stream_id.

Returns a Vec of payload byte vectors in arrival order. The receive buffer is emptied after this call.

§Errors

Returns MuxError::StreamNotFound if the stream does not exist.

Source

pub fn dequeue_frame(&mut self) -> Option<StreamFrame>

Pop the highest-priority frame from the send queue.

Updates total_frames_sent. For data frames (no control flags), the stream’s send_window has already been decremented in send; no further deduction is needed here.

Source

pub fn dequeue_frames(&mut self, n: usize) -> Vec<StreamFrame>

Pop up to n frames from the send queue in priority order.

Source

pub fn drain_send_queue(&mut self) -> Vec<StreamFrame>

Drain all pending outbound frames across all streams in priority order.

Equivalent to calling dequeue_frames with usize::MAX, but more efficient since it drains the heap directly.

Source

pub fn expire_idle(&mut self, current_ts: u64) -> Vec<MuxEvent>

Expire streams that have been idle longer than MultiplexerConfig::idle_timeout_us.

A stream is considered idle if last_activity + idle_timeout_us < current_ts.

Expired streams are transitioned to StreamState::Closed and a MuxEvent::IdleStreamExpired event is emitted for each one.

Source

pub fn update_window(&mut self, stream_id: StreamId, increment: u32) -> bool

Add increment bytes to the send window for stream_id.

Returns true if the stream exists and the window was updated, false otherwise.

Source

pub fn stream_state(&self, stream_id: StreamId) -> Option<&LogicalStream>

Return a reference to the LogicalStream for the given id, or None.

Source

pub fn stream_info(&self, stream_id: StreamId) -> Result<StreamInfo, MuxError>

Return a read-only StreamInfo snapshot for the given stream.

§Errors

Returns MuxError::StreamNotFound if the stream does not exist.

Source

pub fn active_streams(&self) -> Vec<StreamId>

Return the ids of all currently active (Open or Opening) streams.

Source

pub fn open_stream_count(&self) -> usize

Number of streams currently in StreamState::Open.

Source

pub fn stats(&self) -> MuxStats

Return the original compact statistics snapshot (MuxStats).

Source

pub fn multiplexer_stats(&self) -> MultiplexerStats

Return the richer MultiplexerStats snapshot.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more