JFLib

game/src/net/client.rs#

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

client -- the client side of the netcode, without I/O: netchan's events in, messages out

  NetClient<M>   joins a room (HELLO -> WELCOME), runs its own ticks ahead of the server (SteeredClock),
                 predicts its figure with M (Predictor) and sends every tick's input with the ones the
                 server may still lack; decodes snapshots against the base they name (a ring of the last
                 64 it decoded, the map's baseline), reconciles its figure with each newer one, and shows
                 the other entities interp_ms behind the newest snapshot (InterpClock, remotes)
  fetch          a UDP client whose full state does not fit a datagram is told so in WELCOME:
                 fetch_request() names the server's WebSocket port and a ticket, the app opens a second
                 netchan connection there and feeds its events to on_fetch_event (FETCH -> FULL)
  events         send_event (to the room's RoomSim) and take_event (the room's): reliable, in order, each
                 delivered once (event.rs); they ride in the INPUTs and SNAPSHOTs
  smoothing      a correction of the own figure becomes an offset of the drawn position that decays in
                 smooth_ms (predict::Smoother; above `snap` metres it jumps): drawn() is what a frame
                 shows, predicted() stays the exact state
  leave          LEAVE goes out, the server removes the figure at once (NetLink::leave also ends netchan)

Per frame: on_event for every netchan event (main channel), advance(now) -> n, then n times step(map,
input) with the input of that tick; take_out what to send.  The figure is drawn() (or predicted() and
predicted_prev() with alpha() for a frame between two ticks); the others are remotes(); events come from
take_event().
  • struct ClientOptions a client's settings

    • name: String -- the player's name
    • room: Option<RoomCode> -- the room to join (None: make one)
    • interp_ms: u32 -- how far behind the newest snapshot the others are shown (ms)
    • margin: Option<f64> -- the margin the inputs aim for (None: the server's, over a WebSocket plus stream_margin)
    • stream_margin: f64 -- ticks the margin gets over a WebSocket (set_reliable) on top of the server's: TCP delivers what follows a lost segment only after its retransmission (1: over WLAN 2.7 instead of 12 corrections a minute)
    • ring: usize -- snapshots kept as bases and for interpolation
    • min_redundancy: usize -- inputs every packet repeats at least (the newest and the ones before it)
    • smooth_ms: u32 -- the time constant in which a correction of the drawn own figure decays (ms; 0: no smoothing)
    • snap: f64 -- a correction larger than this is not smoothed: the picture jumps (m)
    • event_budget: usize -- the bytes of the events an INPUT carries at most
  • enum Phase where a client stands

    • Connecting -- the channel is not connected yet
    • Joining -- HELLO went out, no WELCOME yet
    • Playing -- in a room
    • Refused(u8) -- the server refused (proto::REFUSE_*)
    • Closed -- the channel ended
    • Left -- the client left (leave)
  • struct ClientStats what a client counted

    • snapshots: u64 -- snapshots decoded (FULL included)
    • fulls: u64 -- FULLs decoded
    • stale: u64 -- snapshots older than the newest (kept as bases only)
    • no_base: u64 -- snapshots whose base was not in the ring
    • bad: u64 -- messages that did not decode
    • early: u64 -- snapshots before WELCOME (it was lost or overtaken: they are dropped)
    • bytes_in: u64 -- payload bytes received (snapshots and FULLs)
    • inputs_sent: u64 -- INPUT messages sent and their payload bytes
    • bytes_out: u64 -- see inputs_sent
    • start_lead: u32 -- ticks between the server's tick and the client's when WELCOME came (the first estimate)
  • struct Remote another entity as a frame shows it: its states in the two snapshots around the render time and the fraction between them (a == b and t == 0 when the newer one is missing: it holds)

    • id: u16 -- the net id
    • generation: u8 -- the generation
    • a: NetState -- the state before the render time
    • b: NetState -- the state after it
    • t: f64 -- the fraction from a to b
  • fn lerp_field(a: i32, b: i32, t: f64) -> f64 a field blended linearly

  • fn lerp_angle_field(a: i32, b: i32, t: f64) -> f64 a field holding an angle in u16 steps (quant::angle_q), blended the short way -> the angle's step as f64

  • struct NetClient<M: Movement> the client (see the module header)

impl<M: Movement> NetClient<M>

  • fn new(opts: ClientOptions, mover: M, schema: Schema, baseline: Snapshot) -> NetClient<M> a client of a game with this schema and map baseline (both as the server has them), predicting with mover
  • fn phase(&self) -> Phase where it stands
  • fn welcome(&self) -> Option<&Welcome> the server's WELCOME (in a room)
  • fn own_id(&self) -> u16 the net id of the own figure (0 before WELCOME)
  • fn mover(&self) -> &M the movement
  • fn schema(&self) -> &Schema the schema
  • fn stats(&self) -> &ClientStats what it counted
  • fn predictor(&self) -> &Predictor<M> the prediction (its counts: steps, mismatches, errors)
  • fn clock(&self) -> &SteeredClock the prediction clock
  • fn interp(&self) -> &InterpClock the interpolation clock
  • fn last_outcome(&self) -> Option<Outcome> what the last reconciliation found
  • fn set_reliable(&mut self, on: bool) the channel is reliable (a WebSocket): its events go once instead of until acknowledged, its margin is stream_margin more (call it before WELCOME). NetLink and the simulated network's users set it; without it a client resends over a WebSocket too (harmless: duplicates are dropped)
  • fn send_event(&mut self, channel: u8, data: &[u8]) -> bool queue an event for the room's RoomSim (reliable, in order) -> false when refused: not in a room, a channel over 15, more than event::MAX_EVENT bytes, event::MAX_PENDING not yet acknowledged
  • fn take_event(&mut self) -> Option<NetEvent> the next event of the room (each once, in the order the room sent them)
  • fn events_out(&self) -> OutboxStats what the outbox of the client's events counted
  • fn events_in(&self) -> InboxStats what the inbox of the room's events counted
  • fn events_pending(&self) -> usize events of the client not yet acknowledged by the server
  • fn leave(&mut self) leave the room: LEAVE goes out (take_out), the server removes the figure at once; the phase is Left and nothing more is sent. (NetLink::leave sends it and ends netchan's connection too.)
  • fn smoothing(&self) -> Vec3 what a frame adds to the predicted position of the own figure: the rest of the corrections, decaying
  • fn smoother(&self) -> &Smoother the smoothing's counts: (corrections, of those snapped)
  • fn drawn(&self) -> Option<Vec3> where a frame draws the own figure: the predicted position between the last two ticks (alpha) plus the decaying rest of the corrections (None before the first snapshot)
  • fn on_event(&mut self, map: &physics::World, e: &ClientEvent, now_ns: u64) one of netchan's client events of the main channel at time now_ns
  • fn fetch_request(&mut self) -> Option<(u16, u64)> (UDP whose full state does not fit) the server's WebSocket port and the ticket, once: open a second netchan connection there (Endpoint::WebSocket) and feed its events to on_fetch_event
  • fn on_fetch_event(&mut self, map: &physics::World, e: &ClientEvent, now_ns: u64) one of netchan's client events of the fetch's connection
  • fn take_out(&mut self) -> Option<Vec<u8>> the next message for the main channel
  • fn take_fetch_out(&mut self) -> Option<Vec<u8>> the next message for the fetch's connection
  • fn advance(&mut self, now_ns: u64) -> u32 the clock stands at now_ns -> the ticks due (call step once for each)
  • fn tick(&self) -> u32 the next tick step predicts
  • fn alpha(&self) -> f64 the clock's place between two ticks (a frame blends predicted_prev and predicted by it)
  • fn step(&mut self, map: &physics::World, input: NetInput) -> u32 one tick with this input: predict, queue the INPUT (its view time is set here) -> the tick
  • fn predicted(&self) -> Option<M::State> the predicted state of the own figure after the last step (None before the first snapshot)
  • fn predicted_prev(&self) -> Option<M::State> the predicted state a tick before (for alpha)
  • fn newest(&self) -> Option<&Snapshot> the newest snapshot decoded
  • fn render_time(&self) -> Option<(u32, f64)> the render time of the others (a tick and the fraction), None before the first snapshot
  • fn remotes(&self, out: &mut Vec<Remote>) the other entities at the render time (the own figure left out), in id order

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