Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/multiplayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ Host loss ends clients' sessions and returns them to the **main menu**; a comple
returns to the lobby. Suspend abandons local state synchronously rather than awaiting these calls.
See [lifecycle](architecture.md#process-lifecycle) and [terminal loss](architecture.md#how-a-match-ends-badly).

Host departure is observed through both Party's peer-left notification and Godot's transport
notification. Hosted-lobby state changes also recheck the pinned host's ownership and connection
status, even when no further gameplay message arrives. These routes stay active during loading,
the countdown and behind popups; teardown is deferred until the native notice returns.

An already-admitted ordinary hosted/code/invite session may continue when only that client's
local Lobby notification connection is lost while its Party transport, session identity and
pinned host remain unchanged. This is continuity of an established session, not fresh Lobby
Expand Down
15 changes: 15 additions & 0 deletions scripts/autoload/net_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ func _note_network_rx(kind: StringName) -> void:

## Binds PartyService's notices. Idempotent, so a party bound twice is still told once.
func _connect_party(party: PartyService) -> void:
if not party.peer_left.is_connected(_on_transport_peer_left):
party.peer_left.connect(_on_transport_peer_left)
if not party.lobby_updated.is_connected(_on_party_lobby_updated):
party.lobby_updated.connect(_on_party_lobby_updated)
# Party losing the network is the other way a session can end. The transport route
# only fires when a *peer* goes away; if Party itself drops the network -- the relay
# becomes unreachable, the service fails the session, the console loses its
Expand Down Expand Up @@ -4182,6 +4186,17 @@ func _on_party_network_lost(reason: String, context: Variant = null) -> void:
_end_lost_network.call_deferred(reason, _entry_epoch, _active_join_request)


## Hosted lobbies have no scoped context. Recheck their pinned host after the
## native notice returns, without carrying an old lobby's update into a new session.
func _on_party_lobby_updated() -> void:
_recheck_lobby_authority.call_deferred(_session_generation)


func _recheck_lobby_authority(session: int) -> void:
if session != 0 and session == _session_generation:
_recheck_authority()


func _end_lost_network(reason: String, epoch: int, join: JoinRequest) -> void:
if epoch != _entry_epoch or (_active_join_request != null and _active_join_request != join):
return
Expand Down
16 changes: 15 additions & 1 deletion scripts/services/party_service.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ signal party_failed(message: String, context: Variant)
signal cleanup_status_changed(message: String)
## Level-triggered notice that cleanup readiness may have changed; consumers re-query.
signal cleanup_state_changed()
signal lobby_updated()
signal context_updated(context: Variant)
signal context_lost(reason: String, context: Variant)
signal multiplayer_invalidated(recovery_epoch: int)
Expand Down Expand Up @@ -1795,9 +1796,16 @@ func _attach_lobby(lobby: Variant) -> void:
var owner_value: Variant = _object_value(lobby, &"owner_entity_key", {})
_legacy_owner_key = _copy_entity_key(owner_value as Dictionary) \
if typeof(owner_value) == TYPE_DICTIONARY else {}
var callback := _on_lobby_changed.bind(lobby)
if lobby != null and lobby.has_signal("state_changed") and not lobby.is_connected("state_changed", callback):
lobby.connect("state_changed", callback)


func _detach_lobby() -> void:
if _lobby != null:
var callback := _on_lobby_changed.bind(_lobby)
if _lobby.has_signal("state_changed") and _lobby.is_connected("state_changed", callback):
_lobby.disconnect("state_changed", callback)
# A membership update posted against the lobby being dropped is unobservable and
# must not answer for the next lobby.
_lock_operation += 1
Expand All @@ -1807,6 +1815,11 @@ func _detach_lobby() -> void:
_legacy_owner_key = {}


func _on_lobby_changed(_change: Variant, lobby: Variant) -> void:
if lobby == _lobby:
lobby_updated.emit()


# --- Network ----------------------------------------------------------------

func _attach_network(network: Variant, is_host: bool) -> void:
Expand Down Expand Up @@ -4577,7 +4590,8 @@ func _on_context_network_state_changed(change: Variant, context: LobbyContext) -
&"NETWORK_CHANGE_PEER_JOINED":
peer_joined.emit(int(_object_value(change, &"peer_id", 0)))
&"NETWORK_CHANGE_PEER_LEFT":
peer_left.emit(int(_object_value(change, &"peer_id", 0)))
if context == _attached_context:
peer_left.emit(int(_object_value(change, &"peer_id", 0)))
&"NETWORK_CHANGE_DESCRIPTOR_UPDATED":
if context.active_permit > 0 and context.local_creator:
_republish_context_descriptor(context)
Expand Down
101 changes: 101 additions & 0 deletions tools/tests/cable_pull_tests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func run(test: Node) -> void:
await _loss_during_world_init()
await _session_end_during_world_init()
await _loss_during_countdown()
await _client_host_loss()
await _lobby_notice_continuity()
await _dispose()
NetManager.server_disconnected.disconnect(_disconnected)
NetManager._clock = Time.get_ticks_msec
Expand Down Expand Up @@ -320,3 +322,102 @@ func _loss_during_countdown() -> void:
"countdown %s: the match reached its start countdown" % kind[2])
_pull(kind)
await _expect_menu("countdown " + String(kind[2]))


func _guest_screen(state: NRTypes.MatchState) -> Variant:
_check(await _join(), "guest session established")
_party._lobby.members.append({
"entity_key": {"id": "2", "type": "title_player_account"},
"connection_status": 1, "properties": {},
})
var host := PlayerState.new()
host.peer_id = NetManager.HOST_PEER_ID
host.display_name = "Fixture Host"
host.in_game = true
NetManager.players[host.peer_id] = host
NetManager.multiplayer.multiplayer_peer.peer_connected.emit(host.peer_id)
if state == NRTypes.MatchState.LOADING:
return ScreenManager.replace_all(ScreenManager.LOBBY)
var gameplay: Variant = ScreenManager.replace_all(ScreenManager.GAMEPLAY)
NetManager._set_match_state(state)
if state == NRTypes.MatchState.STARTING:
NetManager._receive_countdown(3)
_check(gameplay._countdown_label.text == "3", "guest displays the host countdown")
return gameplay


func _client_host_loss() -> void:
print("CASE: lobby and network host departures return guests to the menu during lobby, loading, countdown and play")
for state: NRTypes.MatchState in [NRTypes.MatchState.LOADING, NRTypes.MatchState.PLAYERS_JOINING,
NRTypes.MatchState.STARTING, NRTypes.MatchState.RUNNING]:
for loss: String in ["transport", "network_peer", "owner_removed", "owner_changed", "owner_cleared", "owner_disconnected"]:
await _fixture()
await _guest_screen(state)
var label := "guest state=%d loss=%s" % [state, loss]
_check(not NetManager.is_host(), label + ": this is a client, not the host")
match loss:
"transport":
NetManager.multiplayer.multiplayer_peer.peer_disconnected.emit(NetManager.HOST_PEER_ID)
"network_peer":
_party._network.state_changed.emit({
"kind": PartyService.NETWORK_CHANGE_PEER_LEFT,
"network": _party._network, "peer_id": NetManager.HOST_PEER_ID,
})
_:
match loss:
"owner_removed":
_party._lobby.members.remove_at(0)
"owner_changed":
_party._lobby.owner_entity_key = {"id": "2", "type": "title_player_account"}
"owner_cleared":
_party._lobby.owner_entity_key = {}
"owner_disconnected":
_party._lobby.members[0]["connection_status"] = 0
_party._lobby.state_changed.emit({"kind": PartyService.LOBBY_CHANGE_OWNER_CHANGED})
_check(NetManager.has_session() and _disconnects == 0,
label + ": teardown waits for the native notice")
await _expect_menu(label)

print("CASE: a guest in the countdown also returns on every terminal network notice or server disconnect")
for kind: Array in _terminal_kinds():
await _fixture()
await _guest_screen(NRTypes.MatchState.STARTING)
_pull(kind)
await _expect_menu("guest countdown " + String(kind[2]))
await _fixture()
await _guest_screen(NRTypes.MatchState.STARTING)
NetManager.multiplayer.server_disconnected.emit()
await _expect_menu("guest countdown server disconnect")


func _lobby_notice_continuity() -> void:
print("CASE: healthy lobby updates and a local-only lobby drop preserve a countdown; host loss behind a popup is deduplicated")
await _fixture()
await _guest_screen(NRTypes.MatchState.STARTING)
ScreenManager.push(ScreenManager.GAME_MENU)
var lobby: MP.Lobby = _party._lobby
lobby.state_changed.emit({"kind": PartyService.LOBBY_CHANGE_OWNER_CHANGED})
await _frames()
_check(NetManager.has_session() and _disconnects == 0, "a healthy host survives a lobby update")
lobby.disconnected = true
lobby.state_changed.emit({"kind": PartyService.LOBBY_CHANGE_DISCONNECTED})
await _frames()
_check(NetManager.has_session() and _disconnects == 0,
"an admitted client losing only its lobby connection preserves its Party session")
lobby.members[0]["connection_status"] = 0
lobby.state_changed.emit({"kind": PartyService.LOBBY_CHANGE_OWNER_CHANGED})
var network: MP.Network = _party._network
network.state_changed.emit({
"kind": PartyService.NETWORK_CHANGE_PEER_LEFT, "network": network,
"peer_id": NetManager.HOST_PEER_ID,
})
NetManager.multiplayer.multiplayer_peer.peer_disconnected.emit(NetManager.HOST_PEER_ID)
await _expect_menu("host loss behind countdown popup from lobby and both peer notices")
_check(not lobby.state_changed.is_connected(_party._on_lobby_changed.bind(lobby)),
"leaving disconnects the lobby observer")
await _poll()
_check(await _join(), "a replacement session can be joined")
lobby.state_changed.emit({"kind": PartyService.LOBBY_CHANGE_OWNER_CHANGED})
await _frames()
_check(NetManager.has_session() and _disconnects == 1,
"the retired lobby cannot end its replacement session")
1 change: 1 addition & 0 deletions tools/tests/multiplayer_failure_tests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class Network extends RefCounted:


class Lobby extends RefCounted:
signal state_changed(change: Variant)
var backend: Backend
var connection_string := "fixture-connection"
var properties := {PartyService.DESCRIPTOR_KEY: "fixture-descriptor"}
Expand Down
Loading