N2: Snapshot replication and interest management

NetSnapshot defines the game traffic after the handshake: delta snapshots of
quantized entity records, the client's input packet and a reliable match
notice. SnapshotSender builds each client's snapshot against the newest one
it acknowledged, tells it only about entities near the player it is watching,
and keeps every snapshot under 1,200 bytes, nearest first, with a staleness
bonus so nothing starves. SnapshotReceiver rebuilds the picture, ignores old
and repeated snapshots and asks for a fresh start if it is ever sent a delta
on something it doesn't hold.

NetGame joins the transport to the match: people who connect become
participants, their inputs go through ServerLoop.submit() once per tick, and
snapshots go out 20 times a second. `--server --listen` makes the headless
server take network players and wait in the lobby for the first of them.

Measured by test on a 24-player match start with every player a client, a
tenth of packets lost and late acknowledgements: 2.4 KB/s mean and 2.5 KB/s
for the worst client, packet headers included, against a 40 KB/s limit.

Closes #33

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 08:26:02 -05:00
co-authored by Claude Opus 5.5
parent 881f449d10
commit e535c1f39c
26 changed files with 1695 additions and 24 deletions
+8 -2
View File
@@ -10,6 +10,8 @@ var clients: Array[NetClient] = []
## Every server event since the test began.
var seen: Array[Dictionary] = []
var raw_hosts: Array[ENetConnection] = []
## Game packets the server handed to its owner since the test began.
var handed: int = 0
func before_each() -> void:
@@ -17,6 +19,7 @@ func before_each() -> void:
server.match_seed = 4242
clients.clear()
seen.clear()
handed = 0
raw_hosts.clear()
assert_eq(server.listen(0, HOST), OK)
assert_true(server.port() > 0)
@@ -43,6 +46,7 @@ func _pump(done: Callable, limit_msec: int = 4000) -> bool:
while Time.get_ticks_msec() - started < limit_msec:
server.poll()
seen.append_array(server.events)
handed += server.packets.size()
for client: NetClient in clients:
client.poll()
for host: ENetConnection in raw_hosts:
@@ -242,11 +246,13 @@ func test_random_packets_from_guests_and_players_never_crash_the_server() -> voi
for index: int in bytes.size():
bytes[index] = rng.randi_range(0, 255)
peer.send(rng.randi_range(0, 1), bytes, ENetPacketPeer.FLAG_RELIABLE)
# A player who keeps sending HELLOs and junk stays a player and changes nothing.
# A player who keeps sending HELLOs and junk stays a player and changes nothing. The
# HELLOs are dropped; junk on the game channel goes to the owner, who can't read it.
for _packet: int in 20:
member._peer.send(NetProtocol.CHANNEL_CONTROL, NetProtocol.encode_hello("Someone Else"), ENetPacketPeer.FLAG_RELIABLE)
member._peer.send(NetProtocol.CHANNEL_STATE, PackedByteArray([1, 2, 3]), ENetPacketPeer.FLAG_RELIABLE)
assert_true(_pump(func() -> bool: return _events("refused").size() >= 12 and server.bad_packets >= 52), "refused %d, dropped %d" % [_events("refused").size(), server.bad_packets])
assert_true(_pump(func() -> bool: return _events("refused").size() >= 12 and server.bad_packets >= 32 and handed >= 20), "refused %d, dropped %d, handed on %d" % [_events("refused").size(), server.bad_packets, handed])
assert_eq(handed, 20)
assert_eq(server.player_count(), 1)
assert_eq(server.name_of(1), "Mara")
assert_eq(_events("joined").size(), 1)