The client's side of a networked match, in src/net/: - ClientGame takes the server's notices and snapshots, generates the island from the seed and answers with commands. It works on packets, so it runs over a NetClient or a test's simulated link. - ClientClock decides which tick the next command is for, from the server's echo of the newest command tick it has received, holding three commands waiting at the server. - ClientPrediction runs each command on a copy of the client's own player with the server's movement code, and compares every snapshot with what it predicted. A difference is replayed from the server's state and eased in. - SnapshotInterpolator draws everyone else four ticks in the past, between snapshots, on a clock steered gently by arrivals. Snapshots now carry the client's own block: exact position and velocity, the water around the player, stamina and state flags, so a prediction can match the server to the bit. The command echo is now the newest tick received rather than taken. Protocol version 2. Snapshots go out every second tick. The interval was three ticks, which is 10 a second, not the 20 the documents claimed; 15 a second is the nearest even spacing and what interpolation is tuned for. Drop.ride() holds the barge ride for the server and the client alike, and NetGame's joining, input and snapshot steps can be called without a socket. Measured in test/net/test_prediction_link.gd over a simulated link with 100 ms each way, 20 ms jitter and 2% loss: no corrections to own movement in about 1,400 snapshots per client, and the other player drawn 0.4 mm on average from where a perfect link would draw them. Closes #34 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
6.3 KiB
GDScript
131 lines
6.3 KiB
GDScript
extends TestCase
|
|
|
|
## Issue N2's numbers: a whole 24-player match start, every player a client, over a
|
|
## link that loses a tenth of its packets and acknowledges late. Measures what each
|
|
## client is sent, and checks that nothing outside its interest area is in it.
|
|
|
|
## Bytes around every snapshot on the wire: IPv4 and UDP headers, and ENet's protocol
|
|
## and command headers for an unreliable, unsequenced send.
|
|
const PACKET_OVERHEAD_BYTES: int = 28 + 4 + 8
|
|
const TICKS: int = 2700
|
|
## Snapshots between a snapshot leaving and its acknowledgement arriving.
|
|
const ACK_DELAY: int = 2
|
|
|
|
|
|
func test_24_players_stay_under_40_kb_a_second_and_inside_their_interest() -> void:
|
|
var loop: ServerLoop = ServerLoop.new(4, 0, Tuning.SOLO_PLAYERS)
|
|
var game: NetGame = NetGame.new(loop, NetServer.new())
|
|
var flow: Match = loop.match_flow
|
|
var sender: SnapshotSender = game.sender
|
|
var receivers: Dictionary = {}
|
|
var pending: Dictionary = {}
|
|
var handles: Array[int] = flow.participants.duplicate()
|
|
assert_eq(handles.size(), Tuning.SOLO_PLAYERS)
|
|
for handle: int in handles:
|
|
sender.add_client(handle)
|
|
receivers[handle] = SnapshotReceiver.new()
|
|
pending[handle] = []
|
|
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|
rng.seed = 99
|
|
var limit_players: float = Tuning.NET_INTEREST_PLAYERS_METRES + 0.05
|
|
var limit_pickups: float = Tuning.NET_INTEREST_PICKUPS_METRES + 0.05
|
|
var records_seen: int = 0
|
|
var most_entities: int = 0
|
|
var biggest: int = 0
|
|
var send_usec: int = 0
|
|
var rounds: int = 0
|
|
# Per client, the bytes of the busiest second: that many snapshots in a row.
|
|
var per_second: int = Tuning.TICK_RATE_HZ / Tuning.NET_SNAPSHOT_INTERVAL_TICKS
|
|
var window: Dictionary = {}
|
|
var worst_second: int = 0
|
|
while loop.ticks < TICKS and flow.phase != Match.RESULTS:
|
|
loop.run_tick()
|
|
if loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS != 0:
|
|
continue
|
|
rounds += 1
|
|
var started: int = Time.get_ticks_usec()
|
|
var built: Dictionary = {}
|
|
for handle: int in handles:
|
|
built[handle] = game.snapshot_for(handle)
|
|
send_usec += Time.get_ticks_usec() - started
|
|
for handle: int in handles:
|
|
var bytes: PackedByteArray = built[handle]
|
|
var viewer: SimEntity = loop.sim.entities.find(game.viewer_of(handle))
|
|
assert_true(bytes.size() <= Tuning.NET_SNAPSHOT_MAX_BYTES)
|
|
biggest = maxi(biggest, bytes.size())
|
|
var recent: Array = window.get_or_add(handle, [])
|
|
recent.append(bytes.size() + PACKET_OVERHEAD_BYTES)
|
|
if recent.size() > per_second:
|
|
recent.pop_front()
|
|
var second: int = 0
|
|
for size: int in recent:
|
|
second += size
|
|
worst_second = maxi(worst_second, second)
|
|
for entry: Array in NetSnapshot.decode_snapshot(bytes)["records"]:
|
|
records_seen += 1
|
|
var entity: SimEntity = loop.sim.entities.find(entry[0])
|
|
if entity == null or viewer == null:
|
|
fail("a record for entity %d, which isn't there" % entry[0])
|
|
continue
|
|
var away: float = Vector2(entity.position.x - viewer.position.x, entity.position.z - viewer.position.z).length()
|
|
var limit: float = limit_pickups if entity.archetype == SimEntity.ARCHETYPE_PICKUP else limit_players
|
|
if entity.id != viewer.id and away > limit:
|
|
fail("client %d was sent entity %d, %.1f m away (limit %.1f)" % [handle, entity.id, away, limit])
|
|
var receiver: SnapshotReceiver = receivers[handle]
|
|
if rng.randf() >= 0.1:
|
|
receiver.apply(bytes)
|
|
most_entities = maxi(most_entities, receiver.entities.size())
|
|
var queue: Array = pending[handle]
|
|
queue.append(receiver.ack_tick())
|
|
if queue.size() > ACK_DELAY:
|
|
var ack: int = queue.pop_front()
|
|
if rng.randf() >= 0.1:
|
|
sender.acknowledge(handle, ack)
|
|
assert_true(flow.phase == Match.PLAY or flow.phase == Match.RESULTS, "the run reached play: %s" % flow.phase_name())
|
|
var seconds: float = float(rounds) * Tuning.NET_SNAPSHOT_INTERVAL_TICKS / Tuning.TICK_RATE_HZ
|
|
var worst_rate: float = 0.0
|
|
var total: float = 0.0
|
|
var full: int = 0
|
|
var deferred: int = 0
|
|
for handle: int in handles:
|
|
var stats: Dictionary = sender.stats(handle)
|
|
var rate: float = (float(stats["bytes"]) + float(stats["snapshots"]) * PACKET_OVERHEAD_BYTES) / seconds
|
|
worst_rate = maxf(worst_rate, rate)
|
|
total += rate
|
|
full += int(stats["full"])
|
|
deferred += int(stats["deferred"])
|
|
var receiver: SnapshotReceiver = receivers[handle]
|
|
assert_eq(receiver.malformed, 0)
|
|
assert_eq(receiver.missing_baseline, 0)
|
|
print(" %d clients, %.0f s, 10%% loss: mean %.1f KB/s, worst client %.1f KB/s, worst single second %.1f KB, largest snapshot %d bytes" % [handles.size(), seconds, total / handles.size() / 1000.0, worst_rate / 1000.0, worst_second / 1000.0, biggest])
|
|
print(" %d records sent, %d full snapshots, %d updates deferred, most entities one client held %d, building all 24 took %.2f ms a round" % [records_seen, full, deferred, most_entities, send_usec / 1000.0 / rounds])
|
|
assert_true(worst_rate < 40_000.0, "worst client averaged %.0f bytes a second" % worst_rate)
|
|
assert_true(worst_second < 40_000, "worst single second was %d bytes" % worst_second)
|
|
assert_true(records_seen > 10_000, "the match really was replicated: %d records" % records_seen)
|
|
# Snapshots build on nothing until the first acknowledgement arrives, and again if
|
|
# that first one was lost. After that, loss never forces another.
|
|
assert_true(full <= handles.size() * (ACK_DELAY + 3), "%d full snapshots" % full)
|
|
# With the link quiet again, every client ends with exactly what it may see.
|
|
for _round: int in 12:
|
|
for _tick: int in Tuning.NET_SNAPSHOT_INTERVAL_TICKS:
|
|
loop.run_tick()
|
|
if flow.phase == Match.RESULTS:
|
|
break
|
|
for handle: int in handles:
|
|
var receiver: SnapshotReceiver = receivers[handle]
|
|
receiver.apply(sender.build(handle, loop.sim, game.viewer_of(handle), loop.ticks, flow.phase))
|
|
sender.acknowledge(handle, receiver.ack_tick())
|
|
var checked: int = 0
|
|
for handle: int in handles:
|
|
var receiver: SnapshotReceiver = receivers[handle]
|
|
var viewer: SimEntity = loop.sim.entities.find(game.viewer_of(handle))
|
|
var visible: Array[int] = SnapshotSender.interest(loop.sim, viewer)
|
|
assert_eq(receiver.entities.size(), visible.size(), "client %d's entity count" % handle)
|
|
for entity_id: int in visible:
|
|
if receiver.entities.has(entity_id):
|
|
checked += 1
|
|
assert_eq(receiver.entities[entity_id], NetSnapshot.record_of(loop.sim.entities.find(entity_id)))
|
|
else:
|
|
fail("client %d is missing entity %d" % [handle, entity_id])
|
|
assert_true(checked > handles.size())
|