JFLib

game/src/net/input.rs#

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

input -- what a client sends every tick, the redundancy that makes a lost datagram cost nothing, and
the server's buffer that hands each tick its input

  NetInput        one tick's input, quantised: held buttons (the game's bits), two axes, yaw, pitch and
                  the view time for lag compensation (how far behind the tick the client's picture of
                  the others was, in 1/256 ticks)
  write_inputs    the newest input whole, every older one against the next newer (1 bit when it is the
                  same); read_inputs the inverse (None when malformed)
  InputBuffer     the server's: put what arrives (late and far-future ticks are refused), take(tick)
                  hands the tick its input or repeats the last one (a missed input), have() says up to
                  which tick the inputs are complete (the client resends from there), the margin says
                  how early inputs arrive (the client steers its clock by it)

Ticks are u32 and wrap; every comparison goes through tick_diff.
/// inputs a packet carries at most (the newest and the 15 before it)
MAX_REDUNDANT: usize = 16
/// ticks the server's buffer looks ahead (inputs further in the future are refused)
BUFFER: usize = 64
  • struct NetInput one tick's input (see the module header)

    • buttons: u16 -- the buttons held (the game's bits; edges come from comparing with the previous tick)
    • move_x: i8 -- right (+) and left (-), quant::axis_q
    • move_z: i8 -- forward (+) and back (-), quant::axis_q
    • yaw: u16 -- the view's yaw, quant::angle_q
    • pitch: i16 -- the view's pitch, quant::pitch_q
    • view: u16 -- how far the client's picture of the other entities lagged behind this tick, in 1/256 ticks (the lag compensation's view time is tick - view / 256)
  • fn write_inputs(w: &mut BitWriter, newest: u32, inputs: &[NetInput]) write inputs (the newest first, at most MAX_REDUNDANT) whose newest is for tick newest

  • fn read_inputs(r: &mut BitReader) -> Option<(u32, Vec<NetInput>)> read what write_inputs wrote -> (the newest tick, the inputs newest first); None when malformed

  • struct InputStats what an input buffer counted

    • received: u64 -- inputs taken in (each tick once)
    • repeated: u64 -- inputs that came again (redundancy)
    • late: u64 -- inputs for ticks already simulated
    • early: u64 -- inputs too far in the future (refused)
    • missed: u64 -- ticks simulated without the client's input (the last one repeated)
  • struct InputBuffer the server's inputs of one client (see the module header)

impl InputBuffer

  • fn new(next: u32) -> InputBuffer a buffer whose first tick is next
  • fn put_packet(&mut self, newest: u32, inputs: &[NetInput]) a packet's inputs (newest first, the newest for tick newest)
  • fn put(&mut self, tick: u32, input: NetInput) -> bool one input for tick; false when refused (late, too far ahead, or already there)
  • fn take(&mut self, tick: u32) -> (NetInput, bool) the input of tick (the buffer moves past it) and whether it is the client's own (false: missed, the last one repeated)
  • fn have(&self) -> u32 the newest tick up to which every input from the next one on is here (next - 1 when the next is missing)
  • fn take_margin(&mut self) -> Option<i32> the smallest margin since the last call (None: no packet came)
  • fn next(&self) -> u32 the next tick take() hands out
  • fn stats(&self) -> InputStats what it counted

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