On this page
Packets, bit by bit
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.
The layers#
| Layer | What it does | Original | Ours |
|---|---|---|---|
| game | game events, data blocks, player actions, ghosts | Game\GameServer, Game\GameClient | bf2-server, bf2-client |
| connection | three stream managers write one bit-stream payload per packet | ClientConnection.cpp | bf2-net streams.rs |
| notify window | 6-bit sequence, 32-bit acknowledge mask; tells the streams what arrived | SWConnection.cpp | bf2-net window.rs |
| transport | handshake, ping and pong, time-outs, disconnect | NetServer.cpp, NetClient.cpp | bf2-net packet.rs |
| socket | one UDP socket | UDPSocket | bf2-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.
| Primitive | Wire format | Write / read (bf2_w32ded.exe) |
|---|---|---|
| raw bits | the low n bits, least significant first | 0x00661440 / 0x00661360 |
| bool | 1 bit | 0x00471f70 |
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 |
| signed | a sign bit (1 = negative), then the magnitude in n - 1 bits | 0x00471fe0 / 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) / 2 | 0x0047ac20 / 0x0047ae40 |
| vector | three raw floats | 0x00538fb0 / 0x00539300 |
| delta vector | a 2- or 3-bit class, then three signed values of that class's width holding trunc((p - reference) / step); class 0 is three raw floats | 0x006618f0 .. 0x00662000 |
| quaternion | w, 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).
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:
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.
| Field | Bits | Meaning |
|---|---|---|
type | 4 | 15 = Data (7 = Ping, 8 = Pong; 1 to 5 are the connect and disconnect packets) |
id | 8 | the connection id the server gave at the accept |
seq | 6 | this packet's sequence number; the first is 1 |
ack | 6 | the last sequence the sender received |
mask | 32 | bit i set: packet ack - i was received |
len | 16 | payload 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#
| What | Value | Where |
|---|---|---|
| keep-alive ping | every 3000 ms | server 0x00664da0, client BF2.exe!0x006be5b0 |
| clock-sync pings after the accept | every 200 ms until 32 round-trip samples | BF2.exe!0x006bd410, 0x006be1e0 |
| receive time-out | 10000 ms, both sides | NetServer+0x48c, NetClient+0xb0 |
| disconnect | sent 3 times | 0x00663b50 |
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.
| Field | Bits | Notes |
|---|---|---|
present | 1 | 0 when there is no player or nothing fits |
count | 4 | the actions that follow: the last three at most |
budget | 9 | a latency budget in ms; the server keeps min(v, 512) |
first_seq | 32, signed | only when count > 0; action i has sequence first_seq + i |
| actions | count x 138 | six 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#
| What | Where |
|---|---|
| the bit stream and its primitives | crates/bf2-net/src/bitstream.rs |
| datagrams, the handshake | crates/bf2-net/src/packet.rs |
| the notify window | crates/bf2-net/src/window.rs |
| the payload's streams | crates/bf2-net/src/streams.rs, the events module, ghostmgr.rs |
| a minimal server that speaks all of it | crates/bf2-net/src/join.rs (JoinServer) |
| a viewer for captured packets | crates/bf2-netview |
Notes: notes/subsystems/network.md, notes/subsystems/network-events.md.
docs/dev/network/packets.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.