N6: Joining games: direct connect, LAN and server list

- 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>
This commit is contained in:
2026-11-12 17:33:32 -05:00
co-authored by Claude Opus 5.5
parent 53e81bebe1
commit f795ee3fa5
31 changed files with 1524 additions and 17 deletions
+41
View File
@@ -3,6 +3,8 @@
## tools/godot.sh -- --server [options] headless dedicated server (see ServerConfig)
## tidewall [options] the game: an offline match, or
## tidewall --connect=HOST[:PORT] a match on a server (see ClientConfig)
## tidewall --browse list the servers it can find
## tools/godot.sh -- --list-server a list server (see ListServerRunner)
##
## Without a window (tools/godot.sh is always headless) the game only starts when
## --offline or --connect asks for it, so a bare run prints a line and stops.
@@ -11,6 +13,7 @@ extends Node
var _config: ClientConfig
var _session: ClientSession
var _left: float = 0.0
var _browser: ServerBrowser = null
func _ready() -> void:
@@ -20,6 +23,9 @@ func _ready() -> void:
var all: PackedStringArray = OS.get_cmdline_args()
if all.has("--server"):
args = all
if args.has("--list-server") or OS.get_cmdline_args().has("--list-server"):
add_child(ListServerRunner.new(args if args.has("--list-server") else OS.get_cmdline_args()))
return
var config: ServerConfig = ServerConfig.parse(args)
if config.server:
add_child(HeadlessServer.new(config))
@@ -33,6 +39,9 @@ func _ready() -> void:
if DisplayServer.get_name() == "headless" and not _config.asked:
print("Tidewall")
return
if _config.browse:
_start_browsing()
return
_session = ClientSession.new()
_session.input.autoplay = _config.autoplay
if _config.host.is_empty():
@@ -48,7 +57,39 @@ func _ready() -> void:
_left = _config.seconds
func _start_browsing() -> void:
_browser = ServerBrowser.new()
if _browser.open() != OK:
printerr("tidewall: couldn't open a UDP socket to look for servers")
get_tree().quit(1)
return
var targets: Array[String] = _config.lan_targets.duplicate()
if targets.is_empty():
targets.append("255.255.255.255")
_browser.scan_lan(targets, _config.lan_port)
for target: Dictionary in _config.lists:
var host: String = String(target["host"])
var address: String = host if host.is_valid_ip_address() else IP.resolve_hostname(host)
if address.is_empty():
printerr("tidewall: couldn't find list server '%s'" % host)
continue
_browser.ask_list(address, int(target["port"]))
_left = _config.seconds if _config.seconds > 0.0 else 2.0
func _process(delta: float) -> void:
if _browser != null:
_browser.poll()
_left -= delta
if _left <= 0.0:
var found: Array[Dictionary] = _browser.sorted()
for server: Dictionary in found:
server["phase"] = Match.PHASE_NAMES[int(server["phase"])] if int(server["phase"]) < Match.PHASE_NAMES.size() else "unknown"
print(JSON.stringify({"event": "servers", "servers": found}))
_browser.close()
_browser = null
get_tree().quit(0)
return
if _session == null or _config.seconds <= 0.0:
return
_left -= delta