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:
@@ -0,0 +1,111 @@
|
||||
## 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, 20 times a second.
|
||||
##
|
||||
## 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()
|
||||
## 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
|
||||
|
||||
## Handle -> the newest command tick taken from them this match.
|
||||
var _last_command: 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()
|
||||
|
||||
|
||||
## 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":
|
||||
if loop.match_flow.join(handle):
|
||||
sender.add_client(handle)
|
||||
_last_command[handle] = -1
|
||||
_send_match(handle)
|
||||
events.append(event)
|
||||
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)
|
||||
events.append(event)
|
||||
for packet: Dictionary in net.packets:
|
||||
var handle: int = packet["handle"]
|
||||
if not sender.has_client(handle):
|
||||
continue
|
||||
var input: Dictionary = NetSnapshot.decode_input(packet["bytes"])
|
||||
if input.is_empty():
|
||||
bad_inputs += 1
|
||||
continue
|
||||
sender.acknowledge(handle, int(input["ack"]))
|
||||
if not loop.match_flow.in_match():
|
||||
continue
|
||||
# Packets repeat recent commands; each tick's command is offered once, whether
|
||||
# or not the simulation takes it.
|
||||
for command: PlayerCommand in input["commands"]:
|
||||
if command.tick <= int(_last_command[handle]):
|
||||
continue
|
||||
_last_command[handle] = command.tick
|
||||
if loop.submit(handle, command):
|
||||
sender.note_command(handle, command.tick)
|
||||
|
||||
|
||||
## Tells everyone about a change of phase, and sends snapshots when one is due.
|
||||
func after_tick() -> void:
|
||||
var flow: Match = loop.match_flow
|
||||
var changed: bool = false
|
||||
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)
|
||||
if loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS != 0:
|
||||
return
|
||||
for handle: int in net.handles():
|
||||
if not sender.has_client(handle):
|
||||
continue
|
||||
var bytes: PackedByteArray = sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase)
|
||||
net.send(handle, NetProtocol.CHANNEL_STATE, bytes, false)
|
||||
|
||||
|
||||
## The entity a participant is watching: their own player, the player they spectate
|
||||
## once eliminated, 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 or not flow.entity_of.has(handle):
|
||||
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[handle])
|
||||
|
||||
|
||||
func _send_match(handle: int) -> void:
|
||||
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
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(flow.phase, index, flow.current_seed()), true)
|
||||
Reference in New Issue
Block a user