Struct EventSender

Source
pub struct EventSender { /* private fields */ }

Implementations§

Source§

impl EventSender

Source

pub fn queue(&mut self, payload: &[u8]) -> Result<u64, Error>

Source

pub fn due(&mut self, now: u64, budget: usize) -> Vec<EventPacket>

Returns at most budget events. Call with a monotonic millisecond clock.

Source

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);
Source

pub fn acknowledge(&mut self, sequence: u64) -> bool

Acknowledgments are individual, so loss of an earlier event cannot be hidden by a newer ACK.

Source

pub fn pending(&self) -> usize

Trait Implementations§

Source§

impl Default for EventSender

Source§

fn default() -> EventSender

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

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<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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.