BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Packets, bit by bit

BF22142Heroes

One UDP socket, one datagram per packet, no compression and no fragmentation: a payload that does not fit one datagram is an error, not two packets. What makes the protocol small is that everything is written as bits, with the width each value needs.

Server (authoritative) simulation · Python · rules GhostManager GameEventManager PlayerActionManager UDP 16567 (BF2) · 1 socket Client prediction · render · sound ghosts -> objects events, data blocks input -> actions stream 2: ghosts (object state, server -> client) stream 1: reliable ordered game events + data blocks stream 0: player actions (input, client -> server) SWConnection: 6-bit sequence, 32-bit ack mask no compression, no fragmentation, one bit stream per packet
One packet carries three streams: player actions up, game events both ways, ghosts down.

The layers#

LayerWhat it doesOriginalOurs
gamegame events, data blocks, player actions, ghostsGame\GameServer, Game\GameClientbf2-server, bf2-client
connectionthree stream managers write one bit-stream payload per packetClientConnection.cppbf2-net streams.rs
notify window6-bit sequence, 32-bit acknowledge mask; tells the streams what arrivedSWConnection.cppbf2-net window.rs
transporthandshake, ping and pong, time-outs, disconnectNetServer.cpp, NetClient.cppbf2-net packet.rs
socketone UDP socketUDPSocketbf2-net udp.rs (a client's sockets go through bf2-netio)

The bit stream#

BitStream (IO\BitStream.cpp; read BF2.exe!0x006b6470, write 0x006b6550; ours bf2-net bitstream.rs) is a string of bits in little-endian order: stream bit i is bit i % 8 of byte i / 8, and an n-bit value puts its bit k at stream bit position + k. So values are written least significant bit first and do not line up with bytes. The last byte's unused bits are 0.

PrimitiveWire formatWrite / read (bf2_w32ded.exe)
raw bitsthe low n bits, least significant first0x00661440 / 0x00661360
bool1 bit0x00471f70
ranged [lo, hi]v - lo in b bits, b the smallest with 2^b - 1 >= hi - lo + 1 (one bit more than needed for a power-of-two count)0x00471e80 / 0x00471e30
signeda sign bit (1 = negative), then the magnitude in n - 1 bits0x00471fe0 / 0x00472030
unit float [0, 1]trunc(clamp(f) x (2^n - 1)), read back as v / (2^n - 1)0x004720f0 / 0x00472090
signed unit float [-1, 1]a raw float when n = 32, else the unit float of (f + 1) / 20x0047ac20 / 0x0047ae40
vectorthree raw floats0x00538fb0 / 0x00539300
delta vectora 2- or 3-bit class, then three signed values of that class's width holding trunc((p - reference) / step); class 0 is three raw floats0x006618f0 .. 0x00662000
quaternionw, x, y, z as signed unit floats, or the sign of w and x, y, z (the reader rebuilds w)0x006622a0, 0x00662520

The delta vectors are why a soldier's position costs 3 x 12 bits instead of 96 most of the time: the control block's position is the reference of everything behind it in the ghost stream, and the class says how many bits the difference needs (widths 20 / 16 / 12, 12 / 10 / 8, or 24 down to 8 in the three tables at 0x00834e44, 0x00834e54, 0x00834e64).

Float math on the wire

The readers and writers compute on the x87 at the precision the process runs at. A unit float is truncated, not rounded, so a value written and read back is never above the original. See float precision for what that does to an axis of the player's input.

The datagram#

Every datagram is a bit stream that starts with type:4 id:8 (write 0x00684840, read 0x006848f0), at most 0x5c0 bytes. Game traffic is type 15, Data:

Data datagram (type 15): 72 header bits, then len payload bytes; bits are written least significant first type 4 id 8 seq 6 ack 6 mask 32 len 16 type, connection id | the notify window: my sequence, the last sequence I received, 32 earlier ones | payload bytes The payload: one bit stream, the three stream managers in this order, each behind a presence bit stream 0 player actions client -> server present:1 count:4 budget:9 first_seq:32, then count x 138 bits: 6 axes, 41 buttons, 1 flag stream 1 game events both ways, reliable, ordered present:1 count:8 seq:5 resend:1, then count events: type:7 and the event's own fields stream 2 ghosts server -> client present:1 tick:32 count:8 has_control:1 [control block] then entries: kind:2 id:16 [flag:1 size:11 state] write / read: datagram 0x00683a20 / 0x00664890, payload 0x00548f40 (bf2_w32ded.exe)
A data datagram. The header is 72 bits; the payload holds the three streams in a fixed order.

Explore a packet#

Two made-up packets, bit by bit: a client's packet with one player action, and the server's answer with a control block and one ghost entry. Click a field or any of its bits.

FieldBitsMeaning
type415 = Data (7 = Ping, 8 = Pong; 1 to 5 are the connect and disconnect packets)
id8the connection id the server gave at the accept
seq6this packet's sequence number; the first is 1
ack6the last sequence the sender received
mask32bit i set: packet ack - i was received
len16payload bytes that follow

The notify window#

SWConnection gives unreliable delivery with a delivery notice for every packet. The sender stops while (seq - last acknowledged) & 63 >= 31 (0x00683cd0). Every data packet, ping and pong carries ack and the mask, and the receiver of an acknowledgement tells the three stream managers, oldest packet first, whether each of their packets arrived or was lost (0x00684250). What a stream does with a loss is its own business:

  • the event stream sends the lost block again, with the same sequence;
  • the ghost stream marks the fields that packet carried dirty again, and a later packet carries their newest value;
  • the action stream does nothing: every packet repeats the last three actions anyway.

Timing#

WhatValueWhere
keep-alive pingevery 3000 msserver 0x00664da0, client BF2.exe!0x006be5b0
clock-sync pings after the acceptevery 200 ms until 32 round-trip samplesBF2.exe!0x006bd410, 0x006be1e0
receive time-out10000 ms, both sidesNetServer+0x48c, NetClient+0xb0
disconnectsent 3 times0x00663b50

The payload's three streams#

The stream managers are registered in a fixed order by the connection's constructor (0x00549300) and write and read in that order in every packet (0x00548f40). Each starts with a presence bit.

Stream 0: player actions (client to server)#

Write 0x00616ff0 (BF2.exe!0x005bfbe0), read 0x006172b0.

FieldBitsNotes
present10 when there is no player or nothing fits
count4the actions that follow: the last three at most
budget9a latency budget in ms; the server keeps min(v, 512)
first_seq32, signedonly when count > 0; action i has sequence first_seq + i
actionscount x 138six axes of negative:1 magnitude:15, buttons:32, buttons_hi:9, flag:1

The server applies only actions newer than the last one it applied, so the repeats cost bits and nothing else. An axis is clamp(v x 100, +-32767) truncated: a hundredth of the stick's travel is the unit.

Stream 1: game events (both ways, reliable, ordered)#

Write 0x006187d0, read 0x00618590: present:1, then count:8 seq:5 resend:1 and count events, each a type:7 and the event's own fields. Events carry no length; one event may be at most 1024 bits (0x00618090). A new block goes out only while fewer than 15 blocks are unacknowledged; a block that overtook a lost one waits at the receiver for the re-send, so events run in the order they were written. Every event class and data block of the three games is in the generated network reference.

Stream 2: ghosts (server to client)#

present:1 tick:32 count:8 has_control:1, the control block when there is one, then count entries. The details have pages of their own: ghosts and the control block.

How it is tested#

  • The bit stream (crates/bf2-net/src/bitstream.rs, 17 tests): bit order, writes that keep their neighbours' bits, a read past the end that fails without moving, a reserved field patched later. Everything else on this page is built on it.
  • The handshake and the join (handshake.rs, 26 tests; join.rs, 46): both sides are state machines without sockets, driven datagram by datagram: the connection limits per game, reconnect slots, the rate a connection type gives, the packet schedule.
  • Statement tests from the listing (join_stmt_bf2_tests.rs, 32): each hands one game event to the join server's handler and asserts what the original's listing does with it. They were written because those statements' only proof had been a scenario's run.
  • Against the original server: the player action sweep sends 519 cases to the port and to the original server side by side and compares what each sends back (test rigs).

In the code#

WhatWhere
the bit stream and its primitivescrates/bf2-net/src/bitstream.rs
datagrams, the handshakecrates/bf2-net/src/packet.rs
the notify windowcrates/bf2-net/src/window.rs
the payload's streamscrates/bf2-net/src/streams.rs, the events module, ghostmgr.rs
a minimal server that speaks all of itcrates/bf2-net/src/join.rs (JoinServer)
a viewer for captured packetscrates/bf2-netview

Notes: notes/subsystems/network.md, notes/subsystems/network-events.md.

Source: docs/dev/network/packets.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.