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
+188
View File
@@ -0,0 +1,188 @@
extends TestCase
## Issue N2 end to end: a real NetServer, a ServerLoop and NetClients over loopback
## UDP. People join the lobby, the match starts, snapshots arrive and inputs steer.
const HOST: String = "127.0.0.1"
var loop: ServerLoop
var net: NetServer
var game: NetGame
var clients: Array[NetClient] = []
var receivers: Array[SnapshotReceiver] = []
## Per client: every MATCH notice it was sent.
var notices: Array[Array] = []
## Per client: the yaw it steers towards, in command steps.
var steer: Array[int] = []
var joined: Array[Dictionary] = []
var left: Array[Dictionary] = []
func before_each() -> void:
var rules: MatchRules = MatchRules.new()
rules.max_players = 4
rules.min_players = 2
rules.lobby_ticks = 6
rules.countdown_ticks = 6
rules.results_ticks = 6
loop = ServerLoop.new(31, 0, 2, rules)
net = NetServer.new()
assert_eq(net.listen(0, HOST), OK)
game = NetGame.new(loop, net)
clients.clear()
receivers.clear()
notices.clear()
steer.clear()
joined.clear()
left.clear()
func after_each() -> void:
for client: NetClient in clients:
client.leave()
net.close()
func _client(wanted: String) -> NetClient:
var client: NetClient = NetClient.new()
clients.append(client)
receivers.append(SnapshotReceiver.new())
notices.append([])
steer.append(0)
assert_eq(client.connect_to(HOST, net.port(), wanted), OK)
return client
## Runs the server and every client, a tick per round, until `done` or the time is up.
func _pump(done: Callable, limit_msec: int = 8000) -> bool:
var started: int = Time.get_ticks_msec()
while Time.get_ticks_msec() - started < limit_msec:
game.poll()
for event: Dictionary in game.events:
(joined if event["type"] == "joined" else left).append(event)
loop.run_tick()
game.after_tick()
for index: int in clients.size():
var client: NetClient = clients[index]
var receiver: SnapshotReceiver = receivers[index]
client.poll()
for packet: Dictionary in client.packets:
var bytes: PackedByteArray = packet["bytes"]
if packet["channel"] == NetProtocol.CHANNEL_CONTROL:
var notice: Dictionary = NetSnapshot.decode_match(bytes)
if not notice.is_empty():
notices[index].append(notice)
elif receiver.apply(bytes):
pass
if client.state == NetClient.JOINED:
var commands: Array[PlayerCommand] = []
for ahead: int in range(1, Tuning.NET_INPUT_REDUNDANCY + 1):
var command: PlayerCommand = PlayerCommand.new()
command.tick = receiver.sim_tick + ahead
command.yaw = steer[index]
commands.append(command)
client.send(NetProtocol.CHANNEL_STATE, NetSnapshot.encode_input(receiver.ack_tick(), commands), false)
if done.call():
return true
OS.delay_msec(1)
return bool(done.call())
func test_two_people_join_play_and_are_sent_the_match() -> void:
var mara: NetClient = _client("Mara")
var odo: NetClient = _client("Odo")
steer[0] = 12345
steer[1] = 54321
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and receivers[0].own_id != 0 and receivers[1].own_id != 0), "the match started and both were told")
var flow: Match = loop.match_flow
assert_eq(joined.size(), 2)
assert_eq(flow.participants.size(), 4, "two people and two bots")
assert_eq(receivers[0].own_id, flow.entity_of[mara.handle])
assert_eq(receivers[1].own_id, flow.entity_of[odo.handle])
# Each was told the seed and every phase on the way in.
for index: int in 2:
var phases: Array[int] = []
for notice: Dictionary in notices[index]:
phases.append(notice["phase"])
assert_eq(notice["seed"], flow.current_seed())
assert_eq(phases.slice(phases.size() - 2), [Match.COUNTDOWN, Match.DROP], "client %d's notices: %s" % [index, phases])
# Everyone starts aboard the barge together, so each sees all four players.
assert_true(_pump(func() -> bool: return receivers[0].entities.size() >= 4 and receivers[1].entities.size() >= 4))
# Inputs arrive and steer: the server took commands, and each player faces its way.
assert_true(_pump(func() -> bool:
var mine: PackedInt32Array = receivers[0].entities.get(receivers[0].own_id, PackedInt32Array())
var theirs: PackedInt32Array = receivers[0].entities.get(receivers[1].own_id, PackedInt32Array())
return not mine.is_empty() and not theirs.is_empty() and mine[NetSnapshot.R_YAW] == 12345 and theirs[NetSnapshot.R_YAW] == 54321
), "both yaws reached Mara's picture")
assert_true(receivers[0].command_tick > 0, "the server echoes the command tick it took")
assert_eq(game.bad_inputs, 0)
assert_true(receivers[0].applied > 5)
assert_eq(receivers[0].malformed + receivers[1].malformed, 0)
assert_true(game.sender.stats(mara.handle)["bytes"] > 0)
func test_commands_repeated_in_packets_reach_the_simulation_once() -> void:
var mara: NetClient = _client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.phase == Match.COUNTDOWN))
var log: CommandLog = loop.record_commands()
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and loop.sim.tick > 40))
var ticks_seen: Dictionary = {}
var from_mara: int = 0
for index: int in log.count():
if log.handle_of(index) != mara.handle:
continue
from_mara += 1
var command_tick: int = log.command_of(index).tick
assert_false(ticks_seen.has(command_tick), "tick %d submitted twice" % command_tick)
ticks_seen[command_tick] = true
assert_true(from_mara > 10, "%d commands from Mara" % from_mara)
func test_leaving_takes_the_player_out_of_the_match() -> void:
var mara: NetClient = _client("Mara")
var odo: NetClient = _client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and receivers[1].own_id != 0))
var handle: int = mara.handle
var entity_id: int = loop.match_flow.entity_of[handle]
mara.leave()
assert_true(_pump(func() -> bool: return left.size() == 1))
assert_eq(left[0]["handle"], handle)
assert_false(loop.match_flow.participants.has(handle))
assert_false(game.sender.has_client(handle))
# Odo's picture loses her within a few snapshots.
assert_true(_pump(func() -> bool: return not receivers[1].entities.has(entity_id)))
assert_eq(odo.state, NetClient.JOINED)
func test_a_full_match_turns_the_next_person_away() -> void:
_client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match()))
# Bots filled the other two places, so there is no room until this match ends.
var late: NetClient = _client("Late")
assert_true(_pump(func() -> bool: return late.state == NetClient.CLOSED))
assert_eq(late.close_reason, NetProtocol.REASON_FULL)
assert_eq(loop.match_flow.participants.size(), 4)
assert_eq(net.player_count(), 2)
func test_junk_on_the_game_channel_is_counted_and_changes_nothing() -> void:
var mara: NetClient = _client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match()))
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = 5
for _packet: int in 20:
var bytes: PackedByteArray = PackedByteArray()
bytes.resize(rng.randi_range(1, 200))
for index: int in bytes.size():
bytes[index] = rng.randi_range(0, 255)
bytes[0] = NetSnapshot.MSG_INPUT if rng.randf() < 0.5 else rng.randi_range(0, 255)
# Never a well-formed length for an INPUT.
if (bytes.size() - 6) % PlayerCommand.ENCODED_SIZE == 0:
bytes.append(0)
mara.send(NetProtocol.CHANNEL_STATE, bytes, true)
assert_true(_pump(func() -> bool: return game.bad_inputs >= 20), "%d counted" % game.bad_inputs)
assert_eq(mara.state, NetClient.JOINED)
assert_true(loop.match_flow.in_match())