This document specifies the on-the-wire packet format for the snapshot/delta codec.
Design goals:
- Minimal required fields for snapshot/delta replication.
- Safe decoding with explicit bounds and limits.
- Deterministic parsing.
- Versioned and evolvable.
This format is transport-agnostic (works over UDP, reliable channels, files, etc.).
- tick: The simulation tick the snapshot represents (u32).
- baseline_tick: The tick of the baseline snapshot used to encode a delta (u32).
- schema_hash: A deterministic 64-bit identifier of the replication schema.
- entity: An object with a stable
EntityIdwithin its lifetime. - component: A group of fields replicated together under a
ComponentId.
All integers are encoded in little-endian when byte-aligned.
Bit-level encoding is defined by the bitstream layer.
A packet is:
Header(byte-aligned)Payload(bit-packed sections)
Packets are self-contained: they include enough metadata to validate schema compatibility and decode safely.
The header is minimal and only includes fields we know we need now.
| Field | Type | Required | Description |
|---|---|---|---|
magic |
u32 | yes | Fixed constant to identify this protocol. |
version |
u16 | yes | Wire version. Version 2 uses 2. |
flags |
u16 | yes | Packet kind flags (see below). |
schema_hash |
u64 | yes | Reject packet if mismatched. |
tick |
u32 | yes | Snapshot tick. |
baseline_tick |
u32 | yes | For delta packets: baseline tick. For full snapshots: 0. |
payload_len |
u32 | yes | Payload length in bytes following the header. |
magic: chosen constant (set once; never change in versioned releases).version: starts at2.
Flags are a bitset:
FULL_SNAPSHOT(bit 0): payload contains a full snapshot.DELTA_SNAPSHOT(bit 1): payload contains a delta snapshot.SESSION_INIT(bit 2): packet establishes session context.
Exactly one of FULL_SNAPSHOT or DELTA_SNAPSHOT MUST be set in version 2, unless
SESSION_INIT is set (in which case both are unset).
Reserved bits:
- bits 2..15 reserved for future use; MUST be zero in version 2.
payload_len MUST match the number of bytes following the header. Packets with
extra or missing payload bytes are invalid in version 2.
The payload is a sequence of sections. Each section is:
section_tag(u8, byte-aligned)section_len(varuint, byte-aligned)section_body(section_lenbytes)
Rationale:
- Byte-aligned section framing simplifies skipping unknown sections in future versions.
- Section bodies may contain bit-packed structures internally.
section_lenis a varuint capped at u32 (max 5 bytes). Overflow is invalid.
Only the essential sections are defined.
| Tag | Name | Present in FULL | Present in DELTA | Purpose |
|---|---|---|---|---|
| 1 | ENTITY_CREATE |
optional | optional | Spawn new entities with initial component state. |
| 2 | ENTITY_DESTROY |
optional | optional | Despawn entities. |
| 3 | ENTITY_UPDATE |
optional | optional | Update existing entities (masked updates). |
| 4 | ENTITY_UPDATE_SPARSE |
optional | optional | Update existing entities (sparse field list, varint indices). |
| 5 | ENTITY_UPDATE_SPARSE_PACKED |
optional | optional | Update existing entities (sparse field list, bit-packed indices). |
| 6 | SESSION_INIT |
optional | optional | Session init body (session_id + compact mode). |
Notes:
- FULL snapshot can be represented as a set of creates + updates; however in the initial version we keep semantics simple:
- FULL packets SHOULD include all entities either as creates or updates.
- DELTA packets include only changes since baseline (creates/destroys/updates).
- Version 2 packets MUST NOT include more than one update section (
ENTITY_UPDATE,ENTITY_UPDATE_SPARSE, orENTITY_UPDATE_SPARSE_PACKED).
Unknown section tags:
- In the initial version: decoder MAY reject unknown tags.
- In later versions: decoders SHOULD skip unknown tags using
section_lenfor forward compatibility.
EntityId: u32
ComponentId: u16 (small integer id from schema)
Fields are encoded according to the schema.
In the initial version, schema describes each field codec as:
- primitive kind (bool, int, fixed-point)
- bit width (if fixed)
- bounds + precision (if fixed-point)
- optional threshold for change emission (delta encoder only)
The wire does not embed field types; it relies on schema_hash and schema agreement.
Body:
count(varuint)- repeated
counttimes:entity_id(u32)type_id(u16) // optional in the initial version if single entity type; keep if needed nowcomponent_mask(bitset, size = num_components in schema)- for each component present in
component_mask:field_mask(bitset, size = num_fields in component)- encoded field values for bits set in
field_mask
Notes:
- For creates,
field_maskSHOULD typically include all fields needed to initialize the entity. type_idis only included if the schema supports multiple entity types; if not needed initially, omittype_identirely in v2 and treat all entities as a single type.
Flexibility rule: only include type_id if you truly need multi-type entities in the initial version.
If you don’t, drop it from the initial version to keep the format minimal.
Body:
count(varuint)- repeated
counttimes:entity_id(u32)
Encoding:
- entity IDs MUST be unique within the section.
Session init packets establish session context. They use the standard header and
include a SESSION_INIT flag.
Body:
session_id(u64, optional; 0 means absent)compact_mode(u8; currently1for session compact header v1)
Rules:
SESSION_INITMUST be set, and FULL/DELTA MUST be unset.baseline_tickMUST be 0.- Packet MUST include exactly one
SESSION_INITsection.
For per-client replication, a compact session header can be used once schema/version have been negotiated out-of-band. This header replaces the standard packet header.
Session header layout:
flags(u8)tick_delta(varuint)baseline_delta(varuint)payload_len(varuint)
Rules:
tick_deltaMUST be > 0.- For full snapshots,
baseline_deltaMUST be 0. - For delta snapshots,
baseline_deltaMUST be > 0. - The receiver reconstructs
tickaslast_tick + tick_delta. - The receiver reconstructs
baseline_tickastick - baseline_delta.
This reduces per-packet overhead for per-client deltas while keeping section framing unchanged.
Body:
count(varuint)- repeated
counttimes:entity_id(u32)component_mask(bitset)- for each component present:
field_mask(bitset)- encoded values for fields set in
field_mask
Notes:
- Updates are relative to the reconstructed state at the receiver for
baseline_tick(delta) or the receiver’s cleared state (full snapshot semantics). - Field masks enable sparse changes and avoid sending unchanged fields.
Body:
count(varuint)- repeated
counttimes:entity_id(u32)component_id(u16)field_count(varuint)- repeated
field_counttimes:field_index(varuint, strictly increasing)- encoded field value (by field index)
Notes:
field_indexMUST be less than the number of fields in the component.field_countMUST be <=max_fields_per_component.field_indexentries MUST be strictly increasing (no duplicates).- This section is preferred for sparse updates or visibility-filtered packets.
Body:
count(varuint)- repeated
counttimes:entity_id(varuint)component_id(varuint)field_count(varuint)- repeated
field_counttimes:field_index_bits(bit-packed, width = ceil(log2(num_fields)))- encoded field value (bit-packed where possible)
Notes:
field_index_bitsare written without byte alignment.- Varint field values still align to byte boundaries before encoding.
field_index_bitsMUST be strictly increasing.field_countMUST be <=max_fields_per_component.
For packets with DELTA_SNAPSHOT set:
baseline_tickMUST be non-zero.- Receiver must have a baseline snapshot for
baseline_tick. - If missing:
- receiver should signal the caller (codec returns a typed error)
- caller typically triggers a resync (full snapshot)
The initial version does not define ACK messages; ACKs are out-of-band and transport-specific.
The codec only consumes baseline_tick provided by the caller.
Decoders MUST enforce limits before iterating or allocating.
Recommended CodecLimits (initial defaults; tune per game):
max_packet_bytesmax_sectionsmax_entities_createmax_entities_updatemax_entities_destroymax_components_per_entitymax_fields_per_componentmax_bitset_bits(derived from schema; still enforce)
If any limit is exceeded:
- decoding MUST fail with a structured error
- no partial state should be applied (atomic apply)
versionis the wire compatibility knob.- Version 2 decoders reject packets where
version != 2.
In v2+:
- allow unknown sections to be skipped (using
section_len) - add optional sections without breaking older decoders
- bump
version - keep
magicstable
- Minimal header: magic, version, flags, schema_hash, tick, baseline_tick, payload_len
- Sectioned payload with create/destroy/update
- Schema-driven field encoding
- Required limits for bounded decode