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 ClientOptionsa client's settingsname: String-- the player's nameroom: 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 interpolationmin_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 Phasewhere a client standsConnecting-- the channel is not connected yetJoining-- HELLO went out, no WELCOME yetPlaying-- in a roomRefused(u8)-- the server refused (proto::REFUSE_*)Closed-- the channel endedLeft-- the client left (leave)
struct ClientStatswhat a client countedsnapshots: u64-- snapshots decoded (FULL included)fulls: u64-- FULLs decodedstale: u64-- snapshots older than the newest (kept as bases only)no_base: u64-- snapshots whose base was not in the ringbad: u64-- messages that did not decodeearly: 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 bytesbytes_out: u64-- see inputs_sentstart_lead: u32-- ticks between the server's tick and the client's when WELCOME came (the first estimate)
struct Remoteanother 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 idgeneration: u8-- the generationa: NetState-- the state before the render timeb: NetState-- the state after itt: f64-- the fraction from a to b
fn lerp_field(a: i32, b: i32, t: f64) -> f64a field blended linearlyfn lerp_angle_field(a: i32, b: i32, t: f64) -> f64a field holding an angle in u16 steps (quant::angle_q), blended the short way -> the angle's step as f64struct 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 withmoverfn phase(&self) -> Phasewhere it standsfn welcome(&self) -> Option<&Welcome>the server's WELCOME (in a room)fn own_id(&self) -> u16the net id of the own figure (0 before WELCOME)fn mover(&self) -> &Mthe movementfn schema(&self) -> &Schemathe schemafn stats(&self) -> &ClientStatswhat it countedfn predictor(&self) -> &Predictor<M>the prediction (its counts: steps, mismatches, errors)fn clock(&self) -> &SteeredClockthe prediction clockfn interp(&self) -> &InterpClockthe interpolation clockfn last_outcome(&self) -> Option<Outcome>what the last reconciliation foundfn 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]) -> boolqueue 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 acknowledgedfn 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) -> OutboxStatswhat the outbox of the client's events countedfn events_in(&self) -> InboxStatswhat the inbox of the room's events countedfn events_pending(&self) -> usizeevents of the client not yet acknowledged by the serverfn 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) -> Vec3what a frame adds to the predicted position of the own figure: the rest of the corrections, decayingfn smoother(&self) -> &Smootherthe 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_nsfn 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_eventfn on_fetch_event(&mut self, map: &physics::World, e: &ClientEvent, now_ns: u64)one of netchan's client events of the fetch's connectionfn take_out(&mut self) -> Option<Vec<u8>>the next message for the main channelfn take_fetch_out(&mut self) -> Option<Vec<u8>>the next message for the fetch's connectionfn advance(&mut self, now_ns: u64) -> u32the clock stands at now_ns -> the ticks due (call step once for each)fn tick(&self) -> u32the next tick step predictsfn alpha(&self) -> f64the clock's place between two ticks (a frame blends predicted_prev and predicted by it)fn step(&mut self, map: &physics::World, input: NetInput) -> u32one tick with this input: predict, queue the INPUT (its view time is set here) -> the tickfn 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 decodedfn render_time(&self) -> Option<(u32, f64)>the render time of the others (a tick and the fraction), None before the first snapshotfn remotes(&self, out: &mut Vec<Remote>)the other entities at the render time (the own figure left out), in id order