pub struct EventReceiver { /* private fields */ }Implementations§
Source§impl EventReceiver
impl EventReceiver
Sourcepub fn admit(
&mut self,
packet: &EventPacket,
capacity: usize,
) -> Result<Receipt, Error>
pub fn admit( &mut self, packet: &EventPacket, capacity: usize, ) -> Result<Receipt, Error>
Retains a receipt independently of delivery. ACK only Ok, including
duplicates; Err retains nothing new and leaves all admission state intact.
capacity.min(EVENT_LIMIT) caps the total retained packet count for this
admission, not just new work. Lowering it never evicts existing receipts.
Duplicates still succeed at zero/full capacity. Admission copies a fresh
payload and may allocate both payload and map storage; capacity is a logical
bound, not preallocation or an allocation-failure guarantee.
Validation order is oversized payload, already-delivered duplicate,
outside the forward 256-sequence window, reserved u64::MAX, buffered
duplicate, then capacity. u64::MAX - 1 is the final admissible sequence,
matching the sender; delivery advances to u64::MAX without wrapping.
Retrying a future/outside-window packet never advances the window.
First receipt wins on conflicting bytes for a buffered duplicate. For
already-delivered sequences bytes are not retained for comparison. Even
duplicates must satisfy the payload size bound. This preserves legacy
duplicate handling; it does not authenticate content. Validate session and
application payload before admission. A window containing later packets
needs space for its missing head: use the full window capacity or reserve
that space when choosing a smaller cap. An ACK allows the sender to forget
data, so keep the receiver alive until delivery completes. Replacing it
with default() discards all work and resets to sequence zero; do that only
at a new externally validated transport session rejecting stale packets.
This supplied-time recipe loses the first packet, retains/ACKs the second while delivery is blocked, retries the first and drains into one owned slot. A full destination never discards the remaining tail. ACKs are individual and applied after the sender callback releases its borrow.
use ferravex_net::reliable::{EventReceiver, EventSender, Receipt, EVENT_LIMIT};
let (mut sender, mut receiver) = (EventSender::default(), EventReceiver::default());
sender.queue(b"first")?;
sender.queue(b"second")?;
sender.send_due(0, 1, 5, |_| true); // Accepted locally, lost in transit.
let mut ack = None;
sender.send_due(1, 1, 6, |packet| {
assert_eq!(receiver.admit(packet, EVENT_LIMIT), Ok(Receipt::Retained));
ack = Some(packet.sequence);
true
});
assert!(sender.acknowledge(ack.take().unwrap()));
assert_eq!(receiver.drain_into(&mut [], 99).buffered, 1);
assert_eq!(sender.send_due(99, 2, 99, |_| unreachable!()).sent, 0);
sender.send_due(100, 1, 5, |packet| {
receiver.admit(packet, EVENT_LIMIT).unwrap();
ack = Some(packet.sequence);
true
});
assert!(sender.acknowledge(ack.unwrap()));
let mut slot = [None];
let report = receiver.drain_into(&mut slot, 5);
assert_eq!((report.delivered, report.remaining_ready, report.buffered), (1, 1, 1));
assert_eq!(slot[0].take().unwrap().payload, b"first");
receiver.drain_into(&mut slot, 6);
assert_eq!(slot[0].take().unwrap().payload, b"second");
assert_eq!((sender.pending(), receiver.buffered()), (0, 0));Sourcepub fn drain_into(
&mut self,
output: &mut [Option<EventPacket>],
byte_budget: usize,
) -> DeliveryReport
pub fn drain_into( &mut self, output: &mut [Option<EventPacket>], byte_budget: usize, ) -> DeliveryReport
Moves an ordered prefix into consecutive None slots starting at zero.
Stops at the first occupied slot, missing sequence or payload that exceeds
the remaining byte budget. Never overwrites a slot, skips a head, or clears
any untouched output tail. output.len() is the packet budget; empty
payloads cost zero bytes but one slot. The report describes only this call.
next_sequence advances only after a slot owns its payload.
No payload clone or new allocation occurs in this method; map nodes can be deallocated. Ownership moves to the caller, who must consume/retain it. The first admission’s payload/map allocation is a separate cost.
use ferravex_net::reliable::{EventPacket, EventReceiver, EVENT_LIMIT};
let mut receiver = EventReceiver::default();
receiver.admit(&EventPacket { sequence: 0, payload: vec![7] }, EVENT_LIMIT)?;
let mut output = [None, None];
assert_eq!(receiver.drain_into(&mut output, 0).remaining_ready, 1);
assert_eq!(receiver.drain_into(&mut output, 1).delivered, 1);
assert_eq!(output[0].take().unwrap().payload, [7]);
assert!(output[1].is_none());Sourcepub fn drain_ready(
&mut self,
packet_budget: usize,
byte_budget: usize,
accept: impl FnMut(u64, &[u8]) -> bool,
) -> DeliveryReport
pub fn drain_ready( &mut self, packet_budget: usize, byte_budget: usize, accept: impl FnMut(u64, &[u8]) -> bool, ) -> DeliveryReport
Offers an ordered prefix by borrowed sequence/payload. Only true consumes
the head and advances next_sequence; false stops with head/tail intact.
Both budgets count accepted packets/payload bytes. A too-large head stops
before the callback; no smaller tail is skipped. Empty payloads cost a
packet but zero bytes. At most EVENT_LIMIT packets are visited per call.
The callback must synchronously consume the bytes or explicitly copy them;
borrowed data cannot outlive it. No internal allocation or cloning occurs.
On native unwinding, earlier accepted deliveries persist; the panicking
head and untouched tail remain retained with the same next_sequence.
External callback side effects are not rolled back: consuming then refusing
or panicking may repeat those effects on retry. Aborting panics do not
resume. The exclusive borrow prevents safe same-receiver reentrancy.
use ferravex_net::reliable::{EventPacket, EventReceiver, EVENT_LIMIT};
let mut receiver = EventReceiver::default();
receiver.admit(&EventPacket { sequence: 0, payload: vec![3] }, EVENT_LIMIT)?;
assert_eq!(receiver.drain_ready(1, 1, |_, _| false).remaining_ready, 1);
let mut applied = 0;
let report = receiver.drain_ready(1, 1, |sequence, bytes| {
assert_eq!((sequence, bytes), (0, [3].as_slice()));
applied += 1;
true
});
assert_eq!((applied, report.delivered), (1, 1));Sourcepub fn receive(
&mut self,
packet: &EventPacket,
) -> Result<Vec<EventPacket>, Error>
pub fn receive( &mut self, packet: &EventPacket, ) -> Result<Vec<EventPacket>, Error>
ACK only on success, including duplicates. Delivery is strictly once and ordered. The receive window bounds memory even when event zero never arrives.
Allocating compatibility wrapper: admits at full EVENT_LIMIT capacity and
drains every ready packet, including packets retained by admit. A valid
duplicate can therefore trigger delivery of a previously retained prefix.
An admission error drains nothing. Mixing this with budgeted drains shares
one cursor/queue; returned packets are consumed and must be handled by the
caller. First receipt wins conflicting duplicate bytes, as before.
Reserved sequence MAX now fails without removing data or wrapping the cursor.