N1: Transport and connection handshake

ENet over UDP, used directly through ENetConnection, with a handshake before a
connection counts as a player.

- NetProtocol: HELLO, WELCOME and REJECT layouts, the protocol version, name
  cleaning, the reasons a connection ends and a sentence for the player about
  each. Decoders return nothing for malformed input and never raise an error.
- NetServer: listens, takes each guest through the handshake, and reports
  joined, left and refused. It refuses a different protocol version, a full
  server, a malformed HELLO and a guest that stays silent, each with its own
  reason. Handles are never reused; duplicate names get a number.
- NetClient: connects, introduces itself, and ends up joined or closed with a
  reason. Leaving says goodbye so the server knows at once; a client that
  vanishes is dropped by ENet's timeout.
- Tests run a real server and clients over loopback UDP, with bare ENet peers
  sending malformed and random packets.

The headless server doesn't listen yet; N2 connects this to the match.

Closes #32

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 05:51:12 -05:00
co-authored by Claude Opus 5.5
parent 1d6a24ab64
commit 98a1a05dc8
12 changed files with 1009 additions and 0 deletions
+29
View File
@@ -114,6 +114,35 @@ so a scripted match covers the drop, loot, combat, harvesting, building and the
from a player's seat. `ServerLoop.play_humans_by_script()` turns it on; no shipped mode
uses it.
## Transport and handshake
The transport is ENet over UDP, used directly through `ENetConnection` (not Godot's
high-level multiplayer API, so the server controls every byte it accepts). Channel 0
is reliable control traffic; channel 1 is for commands and snapshots.
`NetServer` and `NetClient` (`src/net/`) own the two ends, and `NetProtocol` the
message layouts and the protocol version. A new connection is a guest until its HELLO
is accepted:
1. The client sends HELLO: a magic number, its protocol version and the name it wants.
2. The server answers WELCOME (the player's handle, the match seed, tick rate, player
limit and the name as accepted) or REJECT with a reason, then disconnects.
A guest gets one try. A HELLO that is malformed, on the wrong channel or too long is
refused as such; a different protocol version, a full server and silence past the
handshake timeout each have their own reason, and the client turns the reason into a
sentence for the player (`NetProtocol.reason_text`). The reason also rides on the
disconnect itself, in case the REJECT is lost. Names are cleaned on both ends: control
and invisible characters removed, 16 characters at most, never empty, and a number
added if someone already has it. Handles are positive, count up and are never reused
while the server runs.
Leaving cleanly tells the server at once. A client that vanishes is dropped by ENet's
timeout (5 to 15 seconds without an acknowledgement), and the owner sees the same
"left" event with a different reason. Decoders return nothing for bad input and never
raise an engine error; anything a joined player sends that isn't understood is counted
and dropped.
## Snapshot format
A snapshot is a binary, little-endian packet: