game/src/net/proto.rs#
Source: lib/game/src/net/proto.rs
proto -- the netcode's messages over a netchan channel (one message a packet; byte 0 is the type, the
rest a bit stream of bits.rs)
HELLO client -> server the version, a room code (none: make a new room), the player's name
WELCOME server -> client the room's code, the player's net id, rate and snapshot interval, the
server's tick now, the input margin to aim for, the lag compensation's
limit, and for a UDP client whose full state does not fit a datagram a
ticket and the WebSocket port to fetch it with
REFUSE server -> client why not (version, room full, no such room, no name, server full, ticket)
INPUT client -> server the newest snapshot it decoded, the full state it fetched (until
confirmed), its inputs (input.rs: the newest and those the server may lack)
SNAPSHOT server -> client the tick, the base (none, the map's baseline, or a tick back), the
player's id, up to which tick its inputs are complete, the margin of its
inputs, then the entities (snapshot.rs)
FETCH client -> server (on a WebSocket of its own) the ticket of WELCOME
FULL server -> client (on that WebSocket) a SNAPSHOT against the baseline without a budget
LEAVE client -> server the player leaves: the server removes the figure at once
The event part (event.rs): bit 7 of byte 0 (EXT) says that an INPUT or SNAPSHOT carries an acknowledgement of
the other side's events and / or a run of its own events: after a SNAPSHOT's header (before the entities, so the
budget counts it), after an INPUT's inputs. Without events the bytes are those of version 1.
Every reader returns None for anything malformed or too large (bytes from the network never panic).
/// the netcode's own version (HELLO); a game's version goes into netchan's protocol id. 2 since G7e part 2 (the
/// event part, LEAVE)
VERSION: u16 = 2
/// message types (byte 0)
MSG_HELLO: u8 = 1
/// see the module header
MSG_WELCOME: u8 = 2
/// see the module header
MSG_REFUSE: u8 = 3
/// see the module header
MSG_INPUT: u8 = 4
/// see the module header
MSG_SNAPSHOT: u8 = 5
/// see the module header
MSG_FETCH: u8 = 6
/// see the module header
MSG_FULL: u8 = 7
/// see the module header
MSG_LEAVE: u8 = 8
/// bit 7 of byte 0: an event part follows (see the module header)
EXT: u8 = 0x80
/// REFUSE: another version of the netcode
REFUSE_VERSION: u8 = 1
/// REFUSE: the room has its players
REFUSE_ROOM_FULL: u8 = 2
/// REFUSE: no room has that code
REFUSE_NO_ROOM: u8 = 3
/// REFUSE: the name is empty after cleaning
REFUSE_NAME: u8 = 4
/// REFUSE: the server has its rooms
REFUSE_SERVER_FULL: u8 = 5
/// REFUSE: the ticket of a FETCH is unknown or used
REFUSE_TICKET: u8 = 6
/// REFUSE: the message was not what the server expected
REFUSE_PROTOCOL: u8 = 7
struct HelloHELLOversion: u16-- VERSION of the clientroom: Option<RoomCode>-- the room to join (None: make one)name: String-- the player's name as typed (the server cleans it)
struct WelcomeWELCOMEroom: RoomCode-- the room's codeplayer: u16-- the net id of the player's figurerate: u16-- the server's ticks a secondsnapshot_every: u8-- a snapshot every this many tickstick: u32-- the server's tick when it sent thismargin: u8-- the margin of inputs to aim for (ticks)max_rewind: u8-- how far back the server rewinds for hits (ticks)fetch: bool-- (UDP) the full state does not fit a datagram: fetch it over a WebSocket withticketticket: u64-- the ticket for FETCHws_port: u16-- the port of the server's WebSocket (0: none)full_bytes: u32-- the bytes of the full state when the player joined
enum Basea snapshot's baseNone-- nothing: every entity is an addMap-- the map's baseline (the client has it from the map)Back(u8)-- the snapshot this many ticks back (1 ..= 255)
struct SnapHeadera SNAPSHOT's or FULL's headertick: u32-- the tickbase: Base-- what the entities are encoded againstplayer: u16-- the net id of the receiver's figure (0: none)have: u32-- the receiver's inputs are complete up to this tickmargin: Option<i8>-- the smallest margin of its inputs since the last snapshot (None: none came)ev_ack: Option<u16>-- the event part: the server has every event of the client up to this oneevents: bool-- the event part: a run of the server's events follows the header (event::read_section, ticks againsttick)
impl SnapHeader
fn new(tick: u32, base: Base, player: u16, have: u32, margin: Option<i8>) -> SnapHeadera header without an event partstruct InputMsgan INPUTnewest: u32-- the tick of the newest inputinputs: Vec<NetInput>-- the inputs, the newest firstack: Option<u32>-- the newest snapshot the client decodedfull: Option<u32>-- the FULL it fetched (repeated until the server bases on it)ev_ack: Option<u16>-- the event part: the client has every event of the server up to this oneevents: Option<(u16, Vec<NetEvent>)>-- the event part: a run of the client's events (the number of the first and the events; ticks againstnewest)
impl InputMsg
fn new(newest: u32, inputs: Vec<NetInput>, ack: Option<u32>, full: Option<u32>) -> InputMsgan INPUT without an event partfn msg_type(b: &[u8]) -> Option<u8>the type of a message without the EXT flag (None: empty)fn has_ext(b: &[u8]) -> boolwhether a message has an event part (EXT)fn write_hello(h: &Hello) -> Vec<u8>HELLO as bytesfn read_hello(b: &[u8]) -> Option<Hello>HELLO from bytesfn write_welcome(m: &Welcome) -> Vec<u8>WELCOME as bytesfn read_welcome(b: &[u8]) -> Option<Welcome>WELCOME from bytesfn write_refuse(reason: u8) -> Vec<u8>REFUSE as bytesfn read_refuse(b: &[u8]) -> Option<u8>REFUSE's reasonfn write_input(m: &InputMsg) -> Vec<u8>INPUT as bytesfn read_input(b: &[u8]) -> Option<InputMsg>INPUT from bytesfn write_snap_header(kind: u8, h: &SnapHeader) -> BitWriterthe header of a SNAPSHOT (kindMSG_SNAPSHOT) or FULL (MSG_FULL); the entities follow in the same writer (snapshot::encode)fn read_snap_header(b: &[u8]) -> Option<(u8, SnapHeader, BitReader<'_>)>the header of a SNAPSHOT or FULL and the reader at its entitiesfn write_fetch(ticket: u64) -> Vec<u8>FETCH as bytesfn read_fetch(b: &[u8]) -> Option<u64>FETCH's ticketfn write_leave() -> Vec<u8>LEAVE as bytesfn is_leave(b: &[u8]) -> boolwhether the bytes are a LEAVE