Skip to content

Add per-client slot disconnect reason to the server#154

Merged
gafferongames merged 1 commit into
mainfrom
server-client-disconnect-reason
Jul 9, 2026
Merged

Add per-client slot disconnect reason to the server#154
gafferongames merged 1 commit into
mainfrom
server-client-disconnect-reason

Conversation

@gafferongames

Copy link
Copy Markdown
Contributor

Summary

The connect_disconnect_callback only reports (client_index, connected), so server code cannot tell why a client disconnected — did it leave cleanly (disconnect packet), time out, or get disconnected by server code? This is the key "is it us or them" signal for game backends built on netcode (motivated by mas-bandwidth/yojimbo#290, which exposes disconnect reasons at the yojimbo layer but could not distinguish timeout from clean disconnect without this).

  • New function int netcode_server_client_disconnect_reason( server, client_index ) returning:
    • NETCODE_SERVER_CLIENT_DISCONNECT_REASON_NONE — no disconnect in this slot yet (or a new client has since connected to it)
    • NETCODE_SERVER_CLIENT_DISCONNECT_REASON_TIMED_OUT — client went silent past its timeout
    • NETCODE_SERVER_CLIENT_DISCONNECT_REASON_CLIENT_DISCONNECT — received a disconnect packet from the client (clean)
    • NETCODE_SERVER_CLIENT_DISCONNECT_REASON_SERVER_DISCONNECT — server code called netcode_server_disconnect_client / disconnect_all_clients / disconnect_loopback_client
  • Tracked per-client slot: reset to NONE at netcode_server_start and when a new client connects to the slot. Recorded before connect_disconnect_callback fires, so it can be queried from inside the callback.
  • Existing functions unchanged; the internal disconnect function gains a reason parameter, set at each call site where the cause is known.

Test plan

  • New test_server_client_disconnect_reason: reason is NONE at server start and while connected → client goes silent → server times it out → TIMED_OUT → reconnect clears the slot back to NONE
  • test_client_side_disconnect now asserts CLIENT_DISCONNECT and test_server_side_disconnect asserts SERVER_DISCONNECT — directly verifying their stated intent ("disconnects cleanly, rather than timing out")
  • Full suite passes in Debug and Release, builds warning-free
  • CI matrix (Linux x64/arm64, macOS, Windows MSVC + MinGW, ASan+UBSan, smoke fuzz)

🤖 Generated with Claude Code

The connect_disconnect_callback only reports (client_index, connected),
so server code could not tell why a client disconnected: did it leave
cleanly (disconnect packet), time out, or get disconnected by server
code calling netcode_server_disconnect_client?

New function int netcode_server_client_disconnect_reason( server,
client_index ) returns NETCODE_SERVER_CLIENT_DISCONNECT_REASON_NONE /
TIMED_OUT / CLIENT_DISCONNECT / SERVER_DISCONNECT for the client that
last occupied the slot. Tracked per-client slot: reset to NONE at
server start and when a new client connects to the slot, and recorded
before the connect_disconnect_callback fires, so the reason can be
queried from inside that callback. Existing functions are unchanged.

Tests: new test_server_client_disconnect_reason covers none ->
timed out -> cleared on reconnect, and the existing client-side and
server-side disconnect tests now also assert the recorded reason,
directly verifying their stated intent ("disconnects cleanly, rather
than timing out").

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@gafferongames gafferongames merged commit 4edccb4 into main Jul 9, 2026
12 checks passed
@gafferongames gafferongames deleted the server-client-disconnect-reason branch July 9, 2026 23:47
pull Bot pushed a commit to Mu-L/yojimbo that referenced this pull request Jul 10, 2026
Pulls in mas-bandwidth/netcode#154 (netcode@4edccb4): new function
netcode_server_client_disconnect_reason( server, client_index )
distinguishing TIMED_OUT / CLIENT_DISCONNECT / SERVER_DISCONNECT,
tracked per-client slot and recorded before the
connect_disconnect_callback fires.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant