Enum BorrowedEvent

Source
pub enum BorrowedEvent<'a> {
    ReliableEvent {
        session: u64,
        event: &'a EventPacket,
    },
    Command {
        session: u64,
        event: &'a EventPacket,
    },
}
Expand description

Borrowed v5 reliable/tactical envelope for an reliable::EventSender::send_due callback. Uses the owned packet codec, without cloning an EventPacket or its payload. Both wire families add 24 bytes to the payload; reliable events accept empty payloads, commands require 1..=900 bytes. No sequence policy or application payload validation is added; validate the outer session and game payload before receiver admission. Safe Rust requires disjoint output/input.

Supplied-time recipe: local refusal, accepted loss, out-of-order receipt, individual wire ACKs, retry, and draining after downstream capacity reopens. Decode/admission and initial sender queueing may allocate; encoding does not. A real transport callback must return true only for a complete datagram or durable outbound queue acceptance. WouldBlock and short sends return false; retain fatal errors outside send_due and propagate them afterward. Local acceptance never manufactures a remote ACK.

use ferravex_net::{*, reliable::{EventSender, EventReceiver, EVENT_LIMIT}};
let (mut sender, mut receiver) = (EventSender::default(), EventReceiver::default());
sender.queue(b"first").unwrap();
sender.queue(b"second").unwrap();
let mut wire = [0; MAX_PACKET_BYTES];
let refused = sender.send_due(1000, 2, 11, |event| {
    BorrowedEvent::ReliableEvent { session: 7, event }.encode_into(&mut wire).unwrap();
    false // e.g. WouldBlock; no retry timestamp is committed.
});
assert_eq!(refused.sent, 0);
let mut ack_wire = [0; 22];
let mut ack_len = 0;
let report = sender.send_due(1001, 2, 11, |event| {
    let envelope = BorrowedEvent::ReliableEvent { session: 7, event };
    let n = envelope.encode_into(&mut wire).unwrap();
    assert_eq!(n, envelope.encoded_len().unwrap());
    if event.sequence == 0 { return true; } // Locally accepted, lost in transit.
    let ServerPacket::ReliableEvent { session: 7, event } = decode_server(&wire[..n]).unwrap()
        else { panic!("wrong session/family") };
    receiver.admit(&event, EVENT_LIMIT).unwrap(); // Application validation first in FPS.
    ack_len = encode_client_into(&ClientPacket::EventAck {
        session: 7, sequence: event.sequence,
    }, &mut ack_wire).unwrap();
    true
});
assert_eq!(report.sent, 2);
let ClientPacket::EventAck { session: 7, sequence } = decode_client(&ack_wire[..ack_len]).unwrap()
    else { panic!("wrong session/family") };
assert!(sender.acknowledge(sequence)); // Only the retained successor is ACKed.
assert_eq!(receiver.drain_ready(2, 11, |_, _| true).delivered, 0);
assert_eq!(sender.send_due(1100, 2, 11, |_| panic!("too early")).sent, 0);
sender.send_due(1101, 1, 5, |event| {
    let n = BorrowedEvent::ReliableEvent { session: 7, event }.encode_into(&mut wire).unwrap();
    let ServerPacket::ReliableEvent { session: 7, event } = decode_server(&wire[..n]).unwrap()
        else { panic!("wrong session/family") };
    receiver.admit(&event, EVENT_LIMIT).unwrap();
    ack_len = encode_client_into(&ClientPacket::EventAck {
        session: 7, sequence: event.sequence,
    }, &mut ack_wire).unwrap();
    true
});
let ClientPacket::EventAck { session: 7, sequence } = decode_client(&ack_wire[..ack_len]).unwrap()
    else { panic!("wrong session/family") };
assert!(sender.acknowledge(sequence));
assert_eq!(sender.pending(), 0);
assert_eq!(receiver.drain_ready(2, 11, |_, _| false).delivered, 0);
// Later downstream capacity opens, without a new incoming packet.
assert_eq!(receiver.drain_ready(1, 5, |seq, bytes| { assert_eq!((seq, bytes), (0, b"first".as_slice())); true }).delivered, 1);
assert_eq!(receiver.drain_ready(1, 6, |seq, bytes| { assert_eq!((seq, bytes), (1, b"second".as_slice())); true }).delivered, 1);

Variants§

§

ReliableEvent

Fields

§session: u64
§event: &'a EventPacket
§

Command

Fields

§session: u64
§event: &'a EventPacket

Implementations§

Source§

impl BorrowedEvent<'_>

Source

pub fn encoded_len(self) -> Result<usize, EncodeError>

Validated wire length, with the same precedence as encoded_client_len. See the complete supplied-time example on BorrowedEvent.

Source

pub fn encode_into(self, destination: &mut [u8]) -> Result<usize, EncodeError>

Checked caller-buffer encoding; shares encode_client_into’s full failure atomicity and untouched-tail guarantees. No clone/allocation.

use ferravex_net::{BorrowedEvent, ClientExtension, ClientPacket, decode_client};
use ferravex_net::reliable::EventPacket;
let event = EventPacket { sequence: 3, payload: vec![1, 2] };
let envelope = BorrowedEvent::Command { session: 7, event: &event };
let mut wire = [99; 28];
let n = envelope.encode_into(&mut wire).unwrap();
assert_eq!(n, envelope.encoded_len().unwrap());
assert_eq!(&wire[n..], &[99; 2]);
assert_eq!(decode_client(&wire[..n]).unwrap(),
    ClientPacket::Extension(ClientExtension::Command { session: 7, event }));

Trait Implementations§

Source§

impl<'a> Clone for BorrowedEvent<'a>

Source§

fn clone(&self) -> BorrowedEvent<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for BorrowedEvent<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Copy for BorrowedEvent<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for BorrowedEvent<'a>

§

impl<'a> RefUnwindSafe for BorrowedEvent<'a>

§

impl<'a> Send for BorrowedEvent<'a>

§

impl<'a> Sync for BorrowedEvent<'a>

§

impl<'a> Unpin for BorrowedEvent<'a>

§

impl<'a> UnwindSafe for BorrowedEvent<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.