JFLib

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 Hello HELLO

    • version: u16 -- VERSION of the client
    • room: Option<RoomCode> -- the room to join (None: make one)
    • name: String -- the player's name as typed (the server cleans it)
  • struct Welcome WELCOME

    • room: RoomCode -- the room's code
    • player: u16 -- the net id of the player's figure
    • rate: u16 -- the server's ticks a second
    • snapshot_every: u8 -- a snapshot every this many ticks
    • tick: u32 -- the server's tick when it sent this
    • margin: 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 with ticket
    • ticket: u64 -- the ticket for FETCH
    • ws_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 Base a snapshot's base

    • None -- nothing: every entity is an add
    • Map -- the map's baseline (the client has it from the map)
    • Back(u8) -- the snapshot this many ticks back (1 ..= 255)
  • struct SnapHeader a SNAPSHOT's or FULL's header

    • tick: u32 -- the tick
    • base: Base -- what the entities are encoded against
    • player: u16 -- the net id of the receiver's figure (0: none)
    • have: u32 -- the receiver's inputs are complete up to this tick
    • margin: 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 one
    • events: bool -- the event part: a run of the server's events follows the header (event::read_section, ticks against tick)

impl SnapHeader

  • fn new(tick: u32, base: Base, player: u16, have: u32, margin: Option<i8>) -> SnapHeader a header without an event part

  • struct InputMsg an INPUT

    • newest: u32 -- the tick of the newest input
    • inputs: Vec<NetInput> -- the inputs, the newest first
    • ack: Option<u32> -- the newest snapshot the client decoded
    • full: 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 one
    • events: Option<(u16, Vec<NetEvent>)> -- the event part: a run of the client's events (the number of the first and the events; ticks against newest)

impl InputMsg

  • fn new(newest: u32, inputs: Vec<NetInput>, ack: Option<u32>, full: Option<u32>) -> InputMsg an INPUT without an event part

  • fn msg_type(b: &[u8]) -> Option<u8> the type of a message without the EXT flag (None: empty)

  • fn has_ext(b: &[u8]) -> bool whether a message has an event part (EXT)

  • fn write_hello(h: &Hello) -> Vec<u8> HELLO as bytes

  • fn read_hello(b: &[u8]) -> Option<Hello> HELLO from bytes

  • fn write_welcome(m: &Welcome) -> Vec<u8> WELCOME as bytes

  • fn read_welcome(b: &[u8]) -> Option<Welcome> WELCOME from bytes

  • fn write_refuse(reason: u8) -> Vec<u8> REFUSE as bytes

  • fn read_refuse(b: &[u8]) -> Option<u8> REFUSE's reason

  • fn write_input(m: &InputMsg) -> Vec<u8> INPUT as bytes

  • fn read_input(b: &[u8]) -> Option<InputMsg> INPUT from bytes

  • fn write_snap_header(kind: u8, h: &SnapHeader) -> BitWriter the header of a SNAPSHOT (kind MSG_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 entities

  • fn write_fetch(ticket: u64) -> Vec<u8> FETCH as bytes

  • fn read_fetch(b: &[u8]) -> Option<u64> FETCH's ticket

  • fn write_leave() -> Vec<u8> LEAVE as bytes

  • fn is_leave(b: &[u8]) -> bool whether the bytes are a LEAVE

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