- LAN discovery: a listening server answers QUERY packets on its game port plus one (ServerBeacon, off with --no-lan). ServerBrowser scans eight game ports from 24650 by broadcast. - A self-hostable list server: --list-server [--port=N] (ListServer, default UDP 24660). Game servers register with --list=HOST[:PORT] and --name=NAME; they are listed under their source address only after echoing a CHALLENGE token. Entries expire after 45 s, 8 per address, 256 in all; every source is rate-limited (SourceLimiter). - NetDiscovery messages: every QUERY is padded to 512 bytes and no answer is bigger, so neither service amplifies spoofed requests. - tidewall --browse [--list=...] [--lan-target=IP] [--lan-port=N] prints the servers found as JSON until menus (A10) show them. - People who connect mid-match or during results watch a living player (Match.watch) and take places ahead of bots in the next lobby, instead of being told the server is full. Bots fill only the places left when the match starts. - Tests: codec round trips, size limits and a 40,000-message fuzz; list server with fake servers (challenge, stolen and wrong tokens, renewal, expiry, caps, flood); LAN scan over loopback; watchers over UDP; lobby backfill. make discovery-check runs it all as real processes. Closes #37 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
215 lines
8.6 KiB
GDScript
215 lines
8.6 KiB
GDScript
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_someone_arriving_mid_match_watches_a_living_player() -> void:
|
|
_client("Mara")
|
|
_client("Odo")
|
|
assert_true(_pump(func() -> bool: return loop.match_flow.in_match()))
|
|
# Bots filled the other two places, so the newcomer watches until the next match.
|
|
var late: NetClient = _client("Late")
|
|
var late_index: int = clients.size() - 1
|
|
assert_true(_pump(func() -> bool: return late.state == NetClient.JOINED and receivers[late_index].own_id != 0), "the newcomer is sent someone to watch")
|
|
var flow: Match = loop.match_flow
|
|
assert_eq(flow.participants.size(), 4)
|
|
assert_true(flow.watchers.has(late.handle))
|
|
assert_false(flow.entity_of.has(late.handle), "a watcher has no player of their own")
|
|
var watched: int = receivers[late_index].own_id
|
|
assert_true(flow.handle_of.has(watched), "the snapshot follows a player in the match")
|
|
assert_false(flow.sim.entities.find(watched).eliminated)
|
|
assert_true(receivers[late_index].own.is_empty(), "nobody else's inventory is sent")
|
|
assert_eq(joined.size(), 3)
|
|
assert_true(bool(joined[2]["watching"]))
|
|
assert_eq(net.player_count(), 3)
|
|
# Leaving while watching is tidy.
|
|
late.leave()
|
|
assert_true(_pump(func() -> bool: return left.size() == 1))
|
|
assert_false(flow.watchers.has(late.handle))
|
|
assert_false(flow.spectating.has(late.handle))
|
|
assert_false(game.sender.has_client(late.handle))
|
|
|
|
|
|
func test_more_people_than_the_server_holds_are_turned_away() -> void:
|
|
net.max_players = 2
|
|
_client("Mara")
|
|
_client("Odo")
|
|
assert_true(_pump(func() -> bool: return net.player_count() == 2))
|
|
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(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())
|