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: MultiplexerConfigEffective configuration for this multiplexer instance.
Implementations§
Source§impl StreamMultiplexer
impl StreamMultiplexer
Sourcepub fn new(config: MultiplexerConfig) -> Self
pub fn new(config: MultiplexerConfig) -> Self
Create a new multiplexer with the given configuration.
Sourcepub fn open_stream(
&mut self,
priority: StreamPriority,
) -> Result<StreamId, MuxError>
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.
Sourcepub fn open_stream_with_priority(
&mut self,
priority_raw: u8,
) -> Result<StreamId, MuxError>
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.
Sourcepub fn close_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>
pub fn close_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>
Send a FIN frame and transition the stream to StreamState::HalfClosed.
§Errors
MuxError::StreamNotFoundif the stream does not exist.MuxError::StreamNotOpenif the stream is already Closed or Reset.
Sourcepub fn reset_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>
pub fn reset_stream(&mut self, stream_id: StreamId) -> Result<(), MuxError>
Send an RST frame and transition the stream to StreamState::Reset.
§Errors
MuxError::StreamNotFoundif the stream does not exist.
Sourcepub fn send(
&mut self,
stream_id: StreamId,
data: Vec<u8>,
now: u64,
) -> Result<usize, MuxError>
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
MuxError::StreamNotFoundif the stream does not exist.MuxError::StreamNotOpenif the stream is notStreamState::Open.MuxError::WindowExhaustedifdata.len()exceeds the remaining send window.
Sourcepub fn receive_events(
&mut self,
frame: StreamFrame,
now: u64,
) -> Result<Vec<MuxEvent>, MuxError>
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:
- SYN →
MuxEvent::StreamOpened(opens the stream if not present) - DATA →
MuxEvent::FrameReceived(payload buffered in recv buffer) - FIN →
MuxEvent::StreamClosed - RST →
MuxEvent::StreamReset - Recv buffer full →
MuxEvent::SendBufferFull(frame dropped, counted indropped_frames)
§Errors
MuxError::StreamNotFoundif the stream is unknown and frame is not SYN.MuxError::SequenceErrorif the frame arrives out of order.
Sourcepub fn receive(
&mut self,
frame: StreamFrame,
_now: u64,
) -> Result<Vec<u8>, MuxError>
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
MuxError::StreamNotFoundif the stream is unknown.MuxError::SequenceErrorif the frame arrives out of order.
Sourcepub fn drain_recv_buffer(
&mut self,
stream_id: StreamId,
) -> Result<Vec<Vec<u8>>, MuxError>
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.
Sourcepub fn dequeue_frame(&mut self) -> Option<StreamFrame>
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.
Sourcepub fn dequeue_frames(&mut self, n: usize) -> Vec<StreamFrame>
pub fn dequeue_frames(&mut self, n: usize) -> Vec<StreamFrame>
Pop up to n frames from the send queue in priority order.
Sourcepub fn drain_send_queue(&mut self) -> Vec<StreamFrame>
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.
Sourcepub fn expire_idle(&mut self, current_ts: u64) -> Vec<MuxEvent>
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.
Sourcepub fn update_window(&mut self, stream_id: StreamId, increment: u32) -> bool
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.
Sourcepub fn stream_state(&self, stream_id: StreamId) -> Option<&LogicalStream>
pub fn stream_state(&self, stream_id: StreamId) -> Option<&LogicalStream>
Return a reference to the LogicalStream for the given id, or None.
Sourcepub fn stream_info(&self, stream_id: StreamId) -> Result<StreamInfo, MuxError>
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.
Sourcepub fn active_streams(&self) -> Vec<StreamId>
pub fn active_streams(&self) -> Vec<StreamId>
Return the ids of all currently active (Open or Opening) streams.
Sourcepub fn open_stream_count(&self) -> usize
pub fn open_stream_count(&self) -> usize
Number of streams currently in StreamState::Open.
Sourcepub fn multiplexer_stats(&self) -> MultiplexerStats
pub fn multiplexer_stats(&self) -> MultiplexerStats
Return the richer MultiplexerStats snapshot.
Auto Trait Implementations§
impl Freeze for StreamMultiplexer
impl RefUnwindSafe for StreamMultiplexer
impl Send for StreamMultiplexer
impl Sync for StreamMultiplexer
impl Unpin for StreamMultiplexer
impl UnsafeUnpin for StreamMultiplexer
impl UnwindSafe for StreamMultiplexer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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