Abschlusskorrekturen M1c: Schläge der Gäste, Figuren und Fragebogen

- Der Host hält einen Schlag eines Gastes, der während der Abklingzeit
  ankommt, bis zum ersten Takt danach. Übers Internet ging sonst etwa
  jeder fünfte Schlag eines Gastes verloren.
- ENet drosselt die Figuren nie mehr; sonst konnten die Figuren eines
  von zwei Gästen dauerhaft stehen bleiben. Figuren gehen geordnet, und
  eine stille Verbindung gilt nach höchstens acht Sekunden als verloren.
- Positionen, die keine Zahlen sind, und Figuren von Spielern, die
  nicht online sind, werden verworfen.
- Ein Host mit angehaltener Simulation sagt das in der Statuszeile.
- Neue Tests: Schlag während der Abklingzeit, Zielauswahl von NetLink,
  verworfene Figuren, Neuabgleich über die Brücke.
- Der Fragebogen sagt, was jeder Rechner braucht, und die Freigabe in
  der Firewall gehört zum Host. CLAUDE.md beschreibt das Halten der
  Schläge, die Drosselung und die Zeitüberschreitung und ergänzt die
  Vertrauensregel um die Schlüssel.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Steven Wroblewski
2026-10-02 01:22:36 +02:00
parent 7f4bbd9606
commit be22e9f358
13 changed files with 258 additions and 27 deletions
+2 -1
View File
@@ -3,7 +3,8 @@ extends RefCounted
## What the command line asks for. Godot passes the arguments after "--":
## --profile=NAME keeps key, save and recordings in a folder of their own,
## so that two windows on one machine are two players
## --name=NAME the player's name for this start
## --name=NAME the player's name; like a name typed in the menu, it is
## remembered for the next start
## --solo starts playing alone at once, without the menu
## --host opens the world for guests at once
## --port=PORT the port an open world listens on
+8 -6
View File
@@ -237,15 +237,17 @@ func _leave(message: String) -> void:
## A broken session runs no more ticks. Instead of leaving the others
## waiting, a host closes its world to guests and a guest leaves.
## waiting, a host closes its world to guests and a guest leaves. The host's
## status line says why its world stands still.
func _stop_sharing() -> void:
if not session.is_host():
_leave(tr("SIMULATION_STOPPED"))
return
link.link.close()
link = null
figures.clear()
_update_status()
if link != null:
link.link.close()
link = null
figures.clear()
hud.show_status(tr("SIMULATION_STOPPED"))
## Ends a pause once the host accepted a guest: the guest joins in the next
@@ -262,7 +264,7 @@ func _process(delta: float) -> void:
if link != null:
for notice in link.pump(delta):
_on_notice(notice)
if session != null and session.is_broken() and link != null:
if session != null and session.is_broken():
_stop_sharing()
if session == null or not session.is_running() or session.is_broken():
return
+15 -3
View File
@@ -2,8 +2,9 @@ class_name NetLink
extends RefCounted
## The network connection of a session, over ENet. A host listens for guests;
## a guest connects to its host. Messages of the core travel on a reliable,
## ordered channel. Positions of figures travel on a second, unreliable
## channel, because a lost one is soon replaced by the next.
## ordered channel. Positions of figures travel on a second channel that is
## unreliable but ordered: a lost position is soon replaced by the next, and
## an older one that arrives after a newer one is dropped.
## Port a host listens on unless told otherwise.
const DEFAULT_PORT := 24680
@@ -14,6 +15,10 @@ const CHANNEL_CORE := 0
const CHANNEL_FIGURES := 1
## Seconds a guest waits for its host to answer before it gives up.
const CONNECT_SECONDS := 8.0
## Milliseconds after which a connection whose packets stay unanswered counts
## as lost. ENet's own limit of 30 seconds would leave a guest whose host
## vanished in a standing world for that long.
const TIMEOUT_MSEC := 8000
## Seconds this guest waits for its host. Tests shorten it.
var connect_seconds := CONNECT_SECONDS
@@ -77,6 +82,13 @@ func _start() -> void:
func _on_connected(peer: int) -> void:
_peers[peer] = true
var connection := _peer.get_peer(peer)
# ENet slows unreliable packets down after a spike in the round trip and
# can keep them at nothing for the rest of a session, so that the figures
# of one guest stop. Positions are a few bytes ten times a second; they
# are never slowed down.
connection.throttle_configure(5000, ENetPacketPeer.PACKET_THROTTLE_SCALE, 0)
connection.set_timeout(32, 5000, TIMEOUT_MSEC)
_events.append({"type": "connected", "peer": peer})
@@ -131,7 +143,7 @@ func send(peer: int, bytes: PackedByteArray) -> void:
## Sends the position of a figure to `peer`, to every connection if `peer`
## is 0, or to every connection but one if `peer` is that one negated.
func send_figure(peer: int, bytes: PackedByteArray) -> void:
_put(peer, bytes, CHANNEL_FIGURES, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE)
_put(peer, bytes, CHANNEL_FIGURES, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE_ORDERED)
func _put(peer: int, bytes: PackedByteArray, channel: int, mode: MultiplayerPeer.TransferMode) -> void:
+9 -3
View File
@@ -22,7 +22,8 @@ func _init(game_session: GwSession, net_link: NetLink) -> void:
## - "world_received" (entry `resync`), "refused" (entry `reason`),
## "protocol_error", "host_lost" and "failed", at a guest
## - "divergence" (entries `peer`, `tick`, `hash`), at the host
## - "figure" (entries `player`, `position`), everywhere
## - "figure" (entries `player`, `position`), everywhere, for another player
## who is online, with a position of real numbers
## `delta` is the time since the last call.
func pump(delta: float) -> Array[Dictionary]:
var notices: Array[Dictionary] = []
@@ -80,12 +81,17 @@ func _receive_figure(peer: int, bytes: PackedByteArray, notices: Array[Dictionar
if player < 0 or bytes.size() != POSITION_BYTES:
return
var position := Vector2(bytes.decode_float(0), bytes.decode_float(4))
# Not a number or infinite, as a broken or changed client may send it:
# such a position would spoil the figure for good.
if not position.is_finite():
return
notices.append({"type": "figure", "player": player, "position": position})
link.send_figure(-peer, _figure_bytes(player, position))
elif bytes.size() == 1 + POSITION_BYTES:
var player := bytes.decode_u8(0)
if player != session.local_player():
var position := Vector2(bytes.decode_float(1), bytes.decode_float(5))
var position := Vector2(bytes.decode_float(1), bytes.decode_float(5))
# A position can arrive after its player left the world.
if player != session.local_player() and session.online_players().has(player) and position.is_finite():
notices.append({"type": "figure", "player": player, "position": position})
+1 -1
View File
@@ -333,7 +333,7 @@ func test_a_host_that_stops_sharing_lets_its_guests_go() -> void:
# session, _process calls what this test calls.
host._stop_sharing()
check(host.link == null, "the host no longer listens")
check_eq(host.hud.status_text(), "", "and no longer says that it does")
check_eq(host.hud.status_text(), tr("SIMULATION_STOPPED"), "its status line says why")
check(await _wait_until(func() -> bool: return guest.session == null), "the guest's session ends")
check_eq(guest.menu.status(), tr("HOST_LEFT"), "and the guest learns why")
_clean_up([host_dir, guest_dir])
+55
View File
@@ -26,6 +26,13 @@ func _first(link: NetLink, type: String) -> Dictionary:
return {}
## The bytes of every event of `type` that `link` got, each as an Array.
func _received(link: NetLink, type: String) -> Array:
return _events.get(link, []).filter(func(event: Dictionary) -> bool: return event["type"] == type).map(
func(event: Dictionary) -> Array: return Array(event["bytes"])
)
## A host and a guest that have connected.
func _connected_pair() -> Array[NetLink]:
var host := NetLink.new()
@@ -139,6 +146,54 @@ func test_nothing_is_sent_to_connections_that_are_gone_or_closing() -> void:
link.close()
## A packet goes to the connection it is addressed to, to every connection
## for peer 0, and to every connection but one for a negative peer.
func test_packets_go_only_where_they_are_addressed() -> void:
var host := NetLink.new()
check_eq(host.host(0, "127.0.0.1"), OK, "the host listens")
var guests: Array[NetLink] = []
var everyone: Array[NetLink] = [host]
for i in 3:
var guest := NetLink.new()
guest.join("127.0.0.1", host.local_port())
guests.append(guest)
everyone.append(guest)
await _pump(everyone, 15)
# Each guest says who it is, so that the host's id for it is known.
for i in 3:
guests[i].send(GwSession.HOST_PEER, PackedByteArray([i]))
await _pump(everyone, 10)
var peer_of: Array[int] = [0, 0, 0]
for event: Dictionary in _events[host]:
if event["type"] == "message":
peer_of[event["bytes"][0]] = event["peer"]
check(not peer_of.has(0), "the host knows all three guests")
_events.clear()
# Repeated, because a position that gets lost is no failure of the link.
for repeat in 5:
host.send(peer_of[0], PackedByteArray([10]))
host.send_figure(peer_of[2], PackedByteArray([20]))
host.send_figure(-peer_of[1], PackedByteArray([30]))
host.send_figure(0, PackedByteArray([40]))
await _pump(everyone, 2)
await _pump(everyone, 10)
check_eq(_received(guests[0], "message").size(), 5, "the first guest got the messages to it")
check_eq(_received(guests[1], "message"), [], "the second guest got none")
check_eq(_received(guests[2], "message"), [], "the third guest got none")
var first := _received(guests[0], "figure")
var second := _received(guests[1], "figure")
var third := _received(guests[2], "figure")
check(not first.has([20]), "the first guest got nothing for the third")
check(first.has([30]) and first.has([40]), "the first guest got 'all but the second' and 'all'")
check(not second.has([20]) and not second.has([30]), "the second guest got neither the one for the third nor 'all but itself'")
check(second.has([40]), "the second guest got 'all'")
check(third.has([20]) and third.has([30]) and third.has([40]), "the third guest got all three kinds")
for link in everyone:
link.close()
func test_a_guest_gives_up_when_nobody_answers() -> void:
# A port that was free a moment ago and has nobody listening now.
var probe := NetLink.new()
+21
View File
@@ -219,6 +219,27 @@ func test_the_hit_of_a_guest_becomes_a_message_to_the_host() -> void:
check_eq(host.damage_codes()[rock.y * host.map_width() + rock.x], 1, "and so does the host")
func test_a_guest_that_missed_a_tick_gets_the_world_again() -> void:
var pair := _host_and_guest()
var host := pair[0]
var guest := pair[1]
host.step()
# The message with this tick gets lost on the way.
host.take_messages()
host.step()
_carry(host, guest)
for i in guest.pending_ticks():
guest.step()
check_eq(_carry(host, guest), [{"type": "world_received", "resync": true}], "the guest gets the host's world again")
check_eq(host.take_divergences().size(), 1, "the host hears of it")
check(guest.take_diverged_recording().size() > 0, "the guest keeps the recording of the world that went astray")
host.step()
_carry(host, guest)
for i in guest.pending_ticks():
guest.step()
check_eq(guest.state_hash_hex(), host.state_hash_hex(), "both worlds are the same again")
func test_a_guest_learns_why_it_is_turned_away() -> void:
var host := _joined_session()
host.connect_peer(5)
+17
View File
@@ -109,6 +109,23 @@ func test_figures_travel_to_the_other_machines() -> void:
_close([host, guest, other])
func test_figures_that_cannot_be_shown_are_dropped() -> void:
_start_host()
var guest := _start_guest(guest_key, "Grace")
var other := _start_guest(PackedByteArray([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]), "Linus")
await _play([guest, other], 30)
# A position that is no number, as a broken or changed client may send.
guest.link.send_figure(GwSession.HOST_PEER, guest._position_bytes(Vector2(NAN, 1.0)))
# A position of a player who is not online, as it can arrive late.
host.link.send_figure(0, host._figure_bytes(5, Vector2(1.0, 2.0)))
await _play([guest, other], 10)
check(not _types(host).has("figure"), "the host drops the position that is no number")
check(not _types(other).has("figure"), "passes nothing on, and nobody shows the player who is not online")
check(not _types(guest).has("figure"), "not even the guest that sent it")
_close([host, guest, other])
func test_a_guest_with_the_key_of_a_player_in_the_world_is_turned_away() -> void:
_start_host()
var twin := _start_guest(host_key, "Host")