- 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>
112 lines
4.1 KiB
GDScript
112 lines
4.1 KiB
GDScript
## Application entry point. Picks the mode from the command line:
|
|
##
|
|
## 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.
|
|
extends Node
|
|
|
|
var _config: ClientConfig
|
|
var _session: ClientSession
|
|
var _left: float = 0.0
|
|
var _browser: ServerBrowser = null
|
|
|
|
|
|
func _ready() -> void:
|
|
var args: PackedStringArray = OS.get_cmdline_user_args()
|
|
if not args.has("--server"):
|
|
# Also accept --server before the "--" separator.
|
|
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))
|
|
return
|
|
_config = ClientConfig.parse(args)
|
|
if not _config.is_valid():
|
|
for problem: String in _config.errors:
|
|
printerr("tidewall: " + problem)
|
|
get_tree().quit(2)
|
|
return
|
|
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():
|
|
var match_seed: int = _config.match_seed if _config.has_seed else int(Time.get_unix_time_from_system() * 1000.0) & 0x7FFFFFFF
|
|
_session.play_offline(match_seed, _config.bots)
|
|
else:
|
|
var error: Error = _session.connect_to(_config.host, _config.port, _config.player_name)
|
|
if error != OK:
|
|
printerr("tidewall: couldn't open a connection: %s" % error_string(error))
|
|
get_tree().quit(1)
|
|
return
|
|
add_child(_session)
|
|
_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
|
|
if _left > 0.0:
|
|
return
|
|
# A timed run says what happened, for scripts: one JSON line.
|
|
var phases: Array[String] = []
|
|
for phase: int in _session.phases:
|
|
phases.append(Match.PHASE_NAMES[phase])
|
|
var joined: bool = _session.local != null or _session.link.state == NetClient.JOINED
|
|
print(JSON.stringify({
|
|
"event": "client", "joined": joined, "status": _session.status_text(), "phases": phases,
|
|
"frames": _session.frames, "frames_playing": _session.frames_playing,
|
|
"world_messages": _session.game.replica.applied, "bad_packets": _session.game.bad_packets,
|
|
"corrections": _session.game.prediction.mispredictions,
|
|
}))
|
|
_session.leave()
|
|
_session = null
|
|
get_tree().quit(0 if joined else 1)
|