Broadcast disconnects; update fake client

Implement server-side disconnect lifecycle and update client handling. Added disconnect_client and broadcast_disconnect in server.py to remove clients, close sockets, and notify remaining clients with a disconnect packet; broadcast_transform now uses disconnect_client for failed recipients and logs delivery counts. Updated fake_client.py to handle disconnect packets, remove remote players from its in-memory table, and improve logging. Documentation (docs/dev-log.md and server/README.md) updated to record the change and next steps.
This commit is contained in:
2026-05-31 19:23:36 +12:00
parent 848c36cd1e
commit 52d934dab0
4 changed files with 84 additions and 14 deletions
+4
View File
@@ -51,6 +51,10 @@ Expected result:
- `fake_client.py` receives and prints the broadcast transform packets.
- `fake_client.py` stores the latest remote player state for each `playerId`
in memory and prints a readable state summary after each transform update.
- When a client disconnects, the server removes that client and broadcasts a
`disconnect` packet with the departed `playerId` to the remaining clients.
- `fake_client.py` removes disconnected players from its in-memory
`remote_players` table and prints the remaining tracked players.
The fake client exists so broadcast behavior can be tested before coordinating a
second Fallout 4/F4SE instance. It stores remote player transform state only; it
+3 -4
View File
@@ -86,7 +86,7 @@ def print_remote_player(player: dict[str, Any]) -> None:
def print_remote_players() -> None:
if not remote_players:
log("Known remote players: none")
log("No remote players currently tracked.")
return
log("Known remote players:")
@@ -115,10 +115,10 @@ def handle_disconnect_packet(packet: dict[str, Any]) -> None:
removed_player = remote_players.pop(player_id, None)
if removed_player is None:
log(f"Disconnect packet for unknown remote player {player_id}")
log(f"Received disconnect for unknown remote player {player_id}.")
return
log(f"Removed remote player {player_id}")
log(f"Removed remote player {player_id} after disconnect.")
print_remote_players()
@@ -132,7 +132,6 @@ def handle_packet(packet: dict[str, Any]) -> None:
elif packet_type == "disconnect":
handle_disconnect_packet(packet)
else:
# TODO: Remove remote players when the server broadcasts disconnect packets.
log(f"Received packet: {packet}")
+43 -9
View File
@@ -45,9 +45,9 @@ def assign_client(connection: socket.socket, address: tuple[str, int]) -> Client
return client
def remove_client(client: Client) -> None:
def remove_client(client: Client) -> bool:
with clients_lock:
clients.pop(client.connection, None)
return clients.pop(client.connection, None) is not None
def send_packet(connection: socket.socket, packet: dict[str, Any]) -> None:
@@ -55,6 +55,19 @@ def send_packet(connection: socket.socket, packet: dict[str, Any]) -> None:
connection.sendall(encoded)
def disconnect_client(client: Client) -> None:
if not remove_client(client):
return
try:
client.connection.close()
except OSError:
pass
log(f"Client disconnected: {client.label} (player {client.player_id})")
broadcast_disconnect(client)
def handle_client(connection: socket.socket, address: tuple[str, int]) -> None:
client = assign_client(connection, address)
log(f"Client connected: {client.label} (player {client.player_id})")
@@ -86,8 +99,7 @@ def handle_client(connection: socket.socket, address: tuple[str, int]) -> None:
except OSError as error:
log(f"Client connection error: {client.label} (player {client.player_id}): {error}")
finally:
remove_client(client)
log(f"Client disconnected: {client.label} (player {client.player_id})")
disconnect_client(client)
def handle_line(client: Client, line: str) -> None:
@@ -128,15 +140,37 @@ def broadcast_transform(sender: Client, packet: dict[str, Any]) -> None:
failed_recipients.append(recipient)
for recipient in failed_recipients:
remove_client(recipient)
try:
recipient.connection.close()
except OSError:
pass
disconnect_client(recipient)
log(f"Broadcast transform from player {sender.player_id} to {len(recipients) - len(failed_recipients)} other client(s)")
def broadcast_disconnect(disconnected_client: Client) -> None:
packet = {
"type": "disconnect",
"playerId": disconnected_client.player_id,
"serverTime": time.time(),
}
with clients_lock:
recipients = list(clients.values())
failed_recipients: list[Client] = []
for recipient in recipients:
try:
send_packet(recipient.connection, packet)
except OSError:
failed_recipients.append(recipient)
for recipient in failed_recipients:
disconnect_client(recipient)
log(
f"Broadcast disconnect for player {disconnected_client.player_id} "
f"to {len(recipients) - len(failed_recipients)} other client(s)"
)
def print_packet(client: Client, packet: dict[str, Any]) -> None:
if packet.get("type") != "transform":
log(f"Packet from {client.label}: {packet}")