JFLib

game/src/net/event.rs#

Source: lib/game/src/net/event.rs

event -- reliable events over the netcode's own packets: each delivered once and in order, over UDP and over a
WebSocket (docs/NETCODE.md, "Ereignisse")

  NetEvent   a tick, a channel (0 ..= 15: the game's kinds, such as the kill feed, hit confirmations, chat) and
             up to MAX_EVENT bytes
  Outbox     the sender's side: every event gets the next number of one sequence per direction (u16, wrapping)
             and stays until the peer acknowledges it; every packet carries the oldest ones up to a byte budget.
             Over a WebSocket (reliable and ordered) each goes once and is forgotten
  Inbox      the receiver's side: the number it expects next.  A packet's run of events starts at or before it
             (the sender forgets only what was acknowledged): the ones before are duplicates, the rest are
             delivered in order; the acknowledgement rides in the next packets of the other direction
  write_section / read_section   a run of events in a bit stream (after a message's header, proto.rs)

Server -> client the events ride in SNAPSHOTs, client -> server in INPUTs, and each side acknowledges in its own
packets what it got (proto.rs: the event part after the header, flagged in byte 0).  This is the rule of Quake 3's
reliable commands: whatever the network loses, duplicates or reorders, a packet that arrives holds a run without a
gap from what the receiver expects, so nothing waits for anything (no reordering buffer, no timer, no extra
packet).  Delivered in one order per direction, so in order per channel as well.  The price: an event rides in
every packet until its acknowledgement comes back -- at 30 snapshots a second and a round trip of 100 ms four or
five times; a kill-feed line of a few bytes costs little.  Why here and not in netchan: see NETCODE.md.
/// the bytes of one event at most
MAX_EVENT: usize = 255
/// channels (0 ..= 15)
CHANNELS: u8 = 16
/// events an outbox holds at most (more: the peer does not acknowledge; a server drops such a client)
MAX_PENDING: usize = 512
/// events one packet's run holds at most
MAX_RUN: usize = 256
/// packets of the other direction that carry the acknowledgement after events came (a duplicate starts it again)
ACK_PACKETS: u32 = 32
  • struct NetEvent one event (see the module header)
    • tick: u32 -- the sender's tick when it happened (the server's tick; a client's predicted tick)
    • channel: u8 -- the channel: the game's kind of event (0 ..= 15)
    • data: Vec<u8> -- the game's bytes (at most MAX_EVENT)

impl NetEvent

  • fn new(tick: u32, channel: u8, data: &[u8]) -> NetEvent an event of channel at tick with these bytes

  • fn valid(&self) -> bool within the limits (channel, size)

  • fn bits(&self, base_tick: u32) -> usize its bits in a run whose ticks count from base_tick

  • fn seq_diff(a: u16, b: u16) -> i32 a - b of two event numbers (they wrap)

  • fn write_section(w: &mut BitWriter, first: u16, events: &[NetEvent], base_tick: u32) write a run: the number of its first event, the count, then each event (channel, tick against base_tick, length, bytes)

  • fn read_section(r: &mut BitReader, base_tick: u32) -> Option<(u16, Vec<NetEvent>)> read a run (None when malformed: a count or length out of range, the stream too short)

  • struct OutboxStats what an outbox counted

    • queued: u64 -- events queued
    • refused: u64 -- events refused (too long, a bad channel, the outbox full)
    • sent: u64 -- events put into a packet for the first time
    • resent: u64 -- ... and again (UDP: until acknowledged)
    • done: u64 -- events acknowledged (or, over a WebSocket, sent)
    • bits: u64 -- the bits of the runs written
  • struct Outbox the sender's side (see the module header)

impl Outbox

  • fn new(reliable: bool, budget: usize) -> Outbox an empty outbox; reliable: the transport delivers (a WebSocket), each event goes once; budget: the bytes of the events a packet carries at most (it always carries at least one)

  • fn push(&mut self, e: NetEvent) -> bool queue an event -> false when refused (too long, a bad channel, MAX_PENDING waiting)

  • fn pending(&self) -> usize events waiting (not yet acknowledged; over a WebSocket: not yet sent)

  • fn stats(&self) -> OutboxStats what it counted

  • fn take_run(&mut self, base_tick: u32) -> Option<(u16, Vec<NetEvent>)> the run the next packet carries (the number of its first event and the events), None when nothing waits; ticks of the bit count against base_tick (the packet's tick)

  • fn ack(&mut self, seq: u16) the peer has every event up to seq (an acknowledgement of a number never sent is ignored)

  • struct InboxStats what an inbox counted

    • delivered: u64 -- events delivered (once each)
    • duplicates: u64 -- events that came again (dropped)
    • gaps: u64 -- runs that started after the next expected number (an honest sender never sends one; dropped)
    • overflow: u64 -- events dropped because the app did not take them (MAX_PENDING waiting)
  • struct Inbox the receiver's side (see the module header)

impl Inbox

  • fn new(acks: bool) -> Inbox an empty inbox expecting event 0; acks: acknowledge (false over a WebSocket, whose sender does not wait)
  • fn receive(&mut self, first: u16, events: Vec<NetEvent>) a packet's run: the new events in order into the queue, duplicates dropped
  • fn take(&mut self) -> Option<NetEvent> the next event delivered (in order)
  • fn waiting(&self) -> usize events delivered and not yet taken
  • fn ack_for_packet(&mut self) -> Option<u16> the acknowledgement the next packet carries (every event up to it came), None when nothing came lately
  • fn stats(&self) -> InboxStats what it counted

Source: docs/API.md in the JFLib repository.