pub struct Stream { /* private fields */ }Expand description
Stream - multiplexed data channel within a session
Implementations§
Source§impl Stream
impl Stream
Sourcepub async fn state(&self) -> StreamState
pub async fn state(&self) -> StreamState
Get current state
Sourcepub fn set_priority(&self, priority: u32)
pub fn set_priority(&self, priority: u32)
Set priority
Sourcepub fn peer_send_window(&self) -> u32
pub fn peer_send_window(&self) -> u32
Bytes the peer currently allows us to send.
Sourcepub fn try_consume_send_window(&self, n: u32) -> bool
pub fn try_consume_send_window(&self, n: u32) -> bool
Atomically reserve n bytes from the peer’s send window.
Returns true if the reservation succeeded (and the window
was decremented); false if the window doesn’t have enough
capacity — caller must wait for a WINDOW_UPDATE.
Sourcepub fn apply_peer_window_update(&self, credit: u32)
pub fn apply_peer_window_update(&self, credit: u32)
Process an inbound WINDOW_UPDATE from the peer. The payload is a
relative credit — the number of bytes the peer’s application just
consumed and is therefore newly willing to receive. We add it to the
send window (saturating at MAX_SEND_WINDOW so a misbehaving peer’s
inflated credit cannot overflow the counter).
Relative credit (vs. an absolute window) is what makes flow control
correct for a session of any length: the sender’s window is
initial + Σ credit_granted − Σ bytes_sent = initial + consumed − sent, so the receiver’s outstanding (unconsumed) bytes sent − consumed
are bounded by initial. An absolute u32 window could not express this
for sessions exceeding 4 GiB and over-committed the receiver’s buffer.
Sourcepub fn local_recv_window(&self) -> u32
pub fn local_recv_window(&self) -> u32
Bytes the local side has granted the peer.
Sourcepub fn record_app_consumed(&self, n: u32) -> Option<u32>
pub fn record_app_consumed(&self, n: u32) -> Option<u32>
Record that the application has actually consumed n bytes from this
stream (called by the receive delivery task on real drainage, not
on routing). Accumulates the consumed bytes and, once the unreported
total crosses half the initial window, returns Some(credit) — the
relative credit to advertise in a WINDOW_UPDATE (the peer adds
it to its send window). The half-window threshold trades update frequency
against peer stalls.
Sourcepub fn stage_window_update_credit(&self, credit: u32)
pub fn stage_window_update_credit(&self, credit: u32)
Stage relative flow-control credit to be flushed by the send loop.
Called by the receive delivery task after it credits real app
consumption. Credits accumulate additively (saturating at
u32::MAX) rather than overwriting, so several grants landing between
two send-loop flushes are summed instead of lost — the send loop is the
single emitter (epoch-safe), and it may run arbitrarily after a grant.
Sourcepub fn take_pending_window_update(&self) -> Option<u32>
pub fn take_pending_window_update(&self) -> Option<u32>
Take all staged credit (swaps the slot back to 0). The send loop calls
this each drain pass and emits one WINDOW_UPDATE carrying the summed
credit if Some.
Sourcepub async fn send_reliable(&self, data: Bytes) -> SequenceNumber
pub async fn send_reliable(&self, data: Bytes) -> SequenceNumber
Queue data for sending with reliability
Returns the sequence number assigned to this chunk.
Sourcepub fn next_send_sequence(&self) -> SequenceNumber
pub fn next_send_sequence(&self) -> SequenceNumber
Reserve the next outbound sequence number from this stream’s send space.
Control frames that are emitted directly on a data stream (e.g.
WINDOW_UPDATE, a bare FIN) MUST draw their sequence from here rather
than a private counter: the AEAD nonce is (epoch, stream_id, sequence, path_id) and the receiver’s replay window is keyed on (stream_id, sequence), so a control frame sharing a (stream_id, sequence) with a
data packet in the same epoch would reuse a nonce and be dropped as a
replay. Sharing one monotonic space keeps every packet on the stream
unique. Control frames are unreliable, so the resulting gap in the data
sequence is harmless (no ACK is expected, nothing waits to reassemble it).
Sourcepub async fn send_unreliable(&self, data: Bytes) -> SequenceNumber
pub async fn send_unreliable(&self, data: Bytes) -> SequenceNumber
Queue data for unreliable sending
Returns the sequence number assigned to this chunk.
Sourcepub async fn poll_send(&self, cwnd_budget: u64) -> Option<OutboundSegment>
pub async fn poll_send(&self, cwnd_budget: u64) -> Option<OutboundSegment>
Get the next segment to (re)transmit, or None if nothing is due.
cwnd_budget is how many bytes of new data the congestion window
currently permits. Retransmissions ignore it — loss recovery must always
proceed — but a first transmission is withheld (None) when it would
exceed the budget, so the next drain resumes once ACKs free the window.
Pass u64::MAX to disable the limit.
Sourcepub async fn ack(&self, sequence: SequenceNumber) -> Option<(Instant, u64)>
pub async fn ack(&self, sequence: SequenceNumber) -> Option<(Instant, u64)>
Mark a sequence number as acknowledged. Returns the timestamp when the packet was originally sent and its size, if found.
Sourcepub async fn mark_unsent(&self, sequence: SequenceNumber)
pub async fn mark_unsent(&self, sequence: SequenceNumber)
Reset a still-buffered reliable segment’s send timestamp so the next
poll_send re-offers it immediately (as an unsent
segment) rather than waiting a full RTO for the retransmit pass. Used
when a send attempt failed after poll_send had already stamped
sent_at — the bytes never reached the wire, so the segment must not be
treated as in-flight. No-op if the segment was already acknowledged and
removed.
Sourcepub async fn on_receive(&self, sequence: SequenceNumber, data: Bytes)
pub async fn on_receive(&self, sequence: SequenceNumber, data: Bytes)
Handle received data
Data is buffered until it can be delivered in order.
Sourcepub async fn recv(&self) -> Option<Bytes>
pub async fn recv(&self) -> Option<Bytes>
Read data from the stream (async, waits if no data available)
Sourcepub async fn on_remote_finish(&self)
pub async fn on_remote_finish(&self)
Mark remote side as finished
Sourcepub async fn pending_send_count(&self) -> usize
pub async fn pending_send_count(&self) -> usize
Get number of pending send chunks
Sourcepub async fn pending_recv_count(&self) -> usize
pub async fn pending_recv_count(&self) -> usize
Get number of pending receive chunks