pub struct EventSender { /* private fields */ }Implementations§
Source§impl EventSender
impl EventSender
pub fn queue(&mut self, payload: &[u8]) -> Result<u64, Error>
Sourcepub fn due(&mut self, now: u64, budget: usize) -> Vec<EventPacket>
pub fn due(&mut self, now: u64, budget: usize) -> Vec<EventPacket>
Returns at most budget events. Call with a monotonic millisecond clock.
Sourcepub fn send_due(
&mut self,
now: u64,
packet_budget: usize,
byte_budget: usize,
accept: impl FnMut(&EventPacket) -> bool,
) -> SendReport
pub fn send_due( &mut self, now: u64, packet_budget: usize, byte_budget: usize, accept: impl FnMut(&EventPacket) -> bool, ) -> SendReport
Offers due packets by immutable borrow, in sequence order, without
allocating or cloning. Only a callback returning true marks its packet
sent at now; false refuses that packet and stops the batch. Translate
transport errors/backpressure to false and retain any error externally.
A successful send is not an ACK: call Self::acknowledge separately.
At most packet_budget callbacks run (also bounded by EVENT_LIMIT).
byte_budget counts payload bytes only, excluding sequence numbers,
lengths, session IDs and all transport/protocol overhead. The first due
packet that cannot fit stops the batch without invoking the callback;
later smaller packets are not skipped ahead. Empty payloads fit a zero
byte budget but still consume one packet. Both budgets count accepted
work; a refusal is the final callback. The report counts all remaining
due packets, even after an early stop. Scans are bounded by the window.
Supply monotonic milliseconds from one clock domain for both send APIs.
Each accepted packet becomes due again after 100 ms. As with Self::due,
a backwards time uses saturating subtraction, delaying retries; no clock
is read or validated here. All acceptances in a batch use this one now.
The mutable sender borrow prevents safe reentrancy, including queueing or
ACKing this sender from the callback. Borrowed packets cannot be retained
beyond their callback; buffer/encode synchronously or explicitly copy.
On an unwinding callback panic, earlier accepted timestamps persist, but
the current packet and untouched tail keep their original timestamps.
Callback side effects are not rolled back: a send followed by false or
panic may be retried. With aborting panics the process does not resume.
This recipe drives the existing receiver with supplied times, local refusal, loss of the first accepted packet, a retry and individual ACKs. The receiver and its returned delivery vectors may allocate.
use ferravex_net::reliable::{EventReceiver, EventSender};
let (mut sender, mut receiver) = (EventSender::default(), EventReceiver::default());
sender.queue(b"first")?;
sender.queue(b"second")?;
let report = sender.send_due(0, 2, 11, |packet| {
packet.sequence == 0 // Accept zero (lost in transit); refuse one.
});
assert_eq!((report.sent, report.remaining_due), (1, 1));
let mut ack = None;
sender.send_due(1, 2, 11, |packet| {
assert_eq!(packet.sequence, 1); // Refused packet is immediately due.
assert!(receiver.receive(packet).unwrap().is_empty()); // Waits for zero.
ack = Some(packet.sequence); // ACK only a successful receive.
true
});
assert!(sender.acknowledge(ack.unwrap()));
assert_eq!(sender.pending(), 1); // ACK one does not hide missing zero.
assert_eq!(sender.send_due(99, 2, 11, |_| unreachable!()).sent, 0);
sender.send_due(100, 2, 11, |packet| {
let delivered = receiver.receive(packet).unwrap();
assert_eq!(delivered.len(), 2);
assert_eq!(delivered[0].payload, b"first");
assert_eq!(delivered[1].payload, b"second");
ack = Some(packet.sequence);
true
});
assert!(sender.acknowledge(ack.unwrap()));
assert_eq!(sender.pending(), 0);Sourcepub fn acknowledge(&mut self, sequence: u64) -> bool
pub fn acknowledge(&mut self, sequence: u64) -> bool
Acknowledgments are individual, so loss of an earlier event cannot be hidden by a newer ACK.