Files
tidewall/src/server/net_game.gd
T
claudeandClaude Opus 5.5 05c7568ec1 N8: 24-client network soak, and the server fixes it found
make soak runs a real server and 24 headless clients in three processes for
two whole matches in real time. Each client reaches the server through its own
UdpRelay, which adds 60 ms, +/-20 ms jitter and 2% loss each way, so ENet runs
unchanged over a bad link. SoakPlayer drives each client from what a client
knows: it drops, glides, heads for high ground, harvests and tries to build.
tools/soak_report.gd checks world hashes against the server's, errors,
unreadable packets, every client playing every match with its connection
intact, memory between matches, and the p99 tick with the network's share.
make soak-selfcheck proves a drifted client world is caught.

Server changes: --matches=N, --world-hash-every=N, tick timing that includes
after_tick, and memory in match and progress lines.

What the soak found and fixed:
- Snapshot building for 24 clients took the p99 tick to 13.8 ms, over the
  12 ms budget, against a 2.8 ms simulation. Encoded records are now shared
  between clients for the tick, unchanged records skip the field diff, and
  clients are spread over the snapshot interval by handle. p99 is 7.3 ms.
- ENet peers had a timeout limit of 0, which gives up on a peer as soon as any
  reliable message has waited the minimum timeout. Now ENet's default, 32.

Not fixed: one or two clients still lose their connection about 15 s into the
second match's drop in each run (see the issue), so the soak still fails and
N8 stays open.

Refs #39

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:56:01 -05:00

212 lines
8.4 KiB
GDScript

## Joins the transport to the match: people who connect become participants, their
## input packets become commands through ServerLoop.submit(), and everyone is sent
## snapshots of what they may see, 15 times a second. Built pieces, props, chests and
## the tide's schedule go to everyone reliably as they change (WorldSender), and each
## player is told what they carry whenever it changes.
##
## Call poll() once a frame before running ticks, and after_tick() after each tick.
class_name NetGame
extends RefCounted
var loop: ServerLoop
var net: NetServer
var sender: SnapshotSender = SnapshotSender.new()
var world: WorldSender = WorldSender.new()
## What happened during the last poll(): {"type": "joined" or "left", "handle", "name",
## "reason"}, for the server's log.
var events: Array[Dictionary] = []
## Input packets that couldn't be read, so far.
var bad_inputs: int = 0
## The WORLD message the last after_tick() sent everyone, or empty, and the OWN
## messages it sent, by handle. For owners that carry the packets themselves.
var world_changes: PackedByteArray = PackedByteArray()
var own_changes: Dictionary = {}
## Handle -> the newest command tick received from them this match.
var _last_command: Dictionary = {}
## Handle -> what they were last told they carry, as _own_notice() lists it.
var _own_sent: Dictionary = {}
func _init(server_loop: ServerLoop, server_net: NetServer) -> void:
loop = server_loop
net = server_net
net.max_players = loop.match_flow.rules.max_players
net.match_seed = loop.match_flow.current_seed()
world.begin(loop.sim)
## Services the network: joins and leaves, then every input packet that arrived.
func poll() -> void:
events.clear()
net.poll()
# kick() adds to net.events, so walk a copy.
for event: Dictionary in net.events.duplicate():
var handle: int = int(event.get("handle", 0))
if event["type"] == "joined":
# In the lobby they take a place; once a match is under way they watch it
# and take a place in the next one.
var watching: bool = false
if not loop.match_flow.join(handle):
watching = loop.match_flow.watch(handle)
if loop.match_flow.participants.has(handle) or watching:
adopt(handle)
_send_match(handle)
net.send(handle, NetProtocol.CHANNEL_CONTROL, world_notice(), true)
var joined: Dictionary = event.duplicate()
joined["watching"] = watching
events.append(joined)
else:
net.kick(handle, NetProtocol.REASON_FULL)
elif event["type"] == "left" and sender.has_client(handle):
loop.match_flow.leave(handle)
sender.remove_client(handle)
_last_command.erase(handle)
_own_sent.erase(handle)
events.append(event)
for packet: Dictionary in net.packets:
take_input(packet["handle"], packet["bytes"])
## Starts sending snapshots to a participant who is already in the match flow, and
## taking their input. poll() does this for everyone who joins over the network.
func adopt(handle: int) -> void:
sender.add_client(handle)
_last_command[handle] = -1
## Takes one INPUT packet from a participant: the snapshot they hold, and commands.
func take_input(handle: int, bytes: PackedByteArray) -> void:
if not sender.has_client(handle):
return
var input: Dictionary = NetSnapshot.decode_input(bytes)
if input.is_empty():
bad_inputs += 1
return
sender.acknowledge(handle, int(input["ack"]))
if not loop.match_flow.in_match():
return
# Packets repeat recent commands; each tick's command is offered once, whether
# or not the simulation takes it. The newest tick received is echoed either way,
# so a client whose commands come too late or too early can tell.
# A tick further ahead than the simulation would ever take is ignored outright,
# so a corrupt or hostile one can't push the marker past every real command.
var furthest: int = loop.sim.tick + Tuning.MAX_COMMAND_LEAD_TICKS
for command: PlayerCommand in input["commands"]:
if command.tick <= int(_last_command[handle]) or command.tick > furthest:
continue
_last_command[handle] = command.tick
sender.note_command(handle, command.tick)
loop.submit(handle, command)
## Tells everyone about a change of phase and whatever changed in the world, and sends
## snapshots when one is due.
func after_tick() -> void:
var flow: Match = loop.match_flow
var changed: bool = false
world_changes = PackedByteArray()
own_changes.clear()
for event: Dictionary in flow.events:
if event["type"] == "phase":
changed = true
if event["to"] == "lobby":
# A new simulation: entity IDs and ticks start again.
sender.reset()
for handle: int in _last_command:
_last_command[handle] = -1
net.match_seed = flow.current_seed()
if changed:
for handle: int in net.handles():
_send_match(handle)
# The world goes before the snapshots, which say how much of it there is to know.
if not world.is_for(loop.sim):
# A new match's simulation: everyone starts again from the whole of it.
world.begin(loop.sim)
_own_sent.clear()
world_changes = world.full()
else:
world_changes = world.changes()
if not world_changes.is_empty():
for handle: int in net.handles():
if sender.has_client(handle):
net.send(handle, NetProtocol.CHANNEL_CONTROL, world_changes, true)
if flow.in_match():
for handle: int in _last_command:
var bytes: PackedByteArray = _own_notice(handle)
if not bytes.is_empty():
own_changes[handle] = bytes
net.send(handle, NetProtocol.CHANNEL_CONTROL, bytes, true)
for handle: int in net.handles():
if sender.has_client(handle) and snapshot_due_for(handle):
net.send(handle, NetProtocol.CHANNEL_STATE, snapshot_for(handle), false)
## True on the ticks a snapshot goes out to an owner that carries packets for a single
## client itself (LocalServer and tests).
func snapshot_due() -> bool:
return loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS == 0
## True on the ticks a networked client's snapshot goes out. Clients are spread over
## the ticks of the interval by handle, so the cost of building snapshots, the largest
## share of a busy server's tick, is split between them instead of landing on one.
func snapshot_due_for(handle: int) -> bool:
var interval: int = Tuning.NET_SNAPSHOT_INTERVAL_TICKS
return (loop.ticks + posmod(handle, interval)) % interval == 0
## The snapshot for a participant as things stand: what they may see, and their own
## block while they are playing.
func snapshot_for(handle: int) -> PackedByteArray:
var flow: Match = loop.match_flow
var own_id: int = int(flow.entity_of.get(handle, 0)) if flow.in_match() else 0
return sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase, own_id, world.seq)
## The whole world as it stands, as one WORLD message: what someone who has just
## joined is sent after the MATCH notice.
func world_notice() -> PackedByteArray:
return world.full()
## The MATCH notice as things stand: the phase, and the match's index and seed.
func match_notice() -> PackedByteArray:
var flow: Match = loop.match_flow
var started: bool = flow.in_match() or flow.phase == Match.RESULTS
var index: int = flow.matches_started - 1 if started else flow.matches_started
return NetSnapshot.encode_match(flow.phase, index, flow.current_seed())
## The entity a participant is watching: their own player, the player they spectate
## once eliminated (or, for someone who arrived mid-match, from the start), or 0 when
## they aren't in a match.
func viewer_of(handle: int) -> int:
var flow: Match = loop.match_flow
if flow.phase == Match.LOBBY or flow.phase == Match.COUNTDOWN:
return 0
var target: int = flow.spectate_target(handle)
if target != 0 and flow.entity_of.has(target):
return int(flow.entity_of[target])
return int(flow.entity_of.get(handle, 0))
## An OWN message for a participant if what they carry has changed since they were
## last told, or nothing.
func _own_notice(handle: int) -> PackedByteArray:
var flow: Match = loop.match_flow
var player: SimEntity = loop.sim.entities.find(int(flow.entity_of.get(handle, 0)))
if player == null or player.eliminated:
return PackedByteArray()
var carried: Array = [player.selected, player.materials, player.slot_items, player.slot_counts, player.slot_rounds, player.slot_tiers, player.ammo]
if _own_sent.get(handle, []) == carried:
return PackedByteArray()
# Copies: the player's own arrays go on changing.
_own_sent[handle] = carried.duplicate(true)
return NetWorld.encode_own(player, loop.sim.tick)
func _send_match(handle: int) -> void:
net.send(handle, NetProtocol.CHANNEL_CONTROL, match_notice(), true)