Skip to content

Async AcceptConnection#9962

Open
nixxquality wants to merge 3 commits intoFacepunch:masterfrom
nixxquality:asyncrejectconnection
Open

Async AcceptConnection#9962
nixxquality wants to merge 3 commits intoFacepunch:masterfrom
nixxquality:asyncrejectconnection

Conversation

@nixxquality
Copy link
Contributor

Summary

This PR replaces AcceptConnection with an async version. The original functionality is left in place, but marked as Obsolete.

Motivation & Context

An async function lets you make a time-consuming call like a check with a database or on-disk ban list without causing a server freeze on connections.

Fixes: #1563

Implementation Details

Discussion happened in Discord: https://discord.com/channels/833983068468936704/895597253316714497/1468245963356508322
The initial implementation was just a string where null means no rejection.
Further discussion led to a new ConnectionRequestResult struct with static defaults so it can be expanded in the future if need be.

Note: I have no idea where the ConnectionRequestResult struct should go. I can either follow instructions or you can eel free to pull in the PR and edit it to your hearts content.

Screenshots / Videos (if applicable)

(Note: this video was recorded before the rework but not much has changed)

2026-02-03_15-20-52.mp4

Sample projects:

Checklist

  • Code follows existing style and conventions
  • No unnecessary formatting or unrelated changes
  • Public APIs are documented (if applicable)
  • Unit tests added where applicable and all passing
  • I’m okay with this PR being rejected or requested to change 🙂

Copilot AI review requested due to automatic review settings February 3, 2026 15:25
@badandbest
Copy link
Contributor

badandbest commented Feb 3, 2026

I'd recommend squashing the commits into one when you're done

Comment on lines +660 to +662
var result = await c.AcceptConnection( channel );

if ( !result ) return result;
Copy link
Contributor

@badandbest badandbest Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could combine both SceneNetworkingSystem.AcceptConnection methods by making the obsolete INetworkListener.AcceptConnection return a ConnectionRequestResult.Reject. This would simplify other parts of the class.

Suggested change
var result = await c.AcceptConnection( channel );
if ( !result ) return result;
#pragma warning disable CS0618 // Games still use the old method.
var denialReason = "";
if ( !c.AcceptConnection( channel, ref denialReason ) )
return ConnectionRequestResult.Reject( denialReason );
#pragma warning restore CS0618
var result = await c.AcceptConnection( channel );
if ( !result ) return result;

Copy link
Contributor Author

@nixxquality nixxquality Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double functions exist because AcceptConnection is a public member of the public abstract partial class GameNetworkSystem. Changing that would be a breaking change.
https://github.com/Facepunch/sbox-public/blob/69a2985c8645c3b94a70175596e8f86f78e91706/engine/Sandbox.Engine/Systems/Networking/System/GameNetworkSystem/GameNetworkSystem.cs

Copy link
Contributor

@badandbest badandbest Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even possible for games to get access to GameNetworkSystem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026-02-04_10-18-07

Not sure what you'd use it for, but again, it is public so yes it can be accessed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nixxquality
Copy link
Contributor Author

As an aside, I noticed that if an exception is thrown in the AcceptConnection function (both current and new) then the connecting user gets stuck in the loading screen until he gives up and presses cancel.

I could fix that in this PR as well, if you would like me to.

@badandbest
Copy link
Contributor

badandbest commented Feb 4, 2026

if an exception is thrown in the AcceptConnection function then the connecting user gets stuck in the loading screen until he gives up and presses cancel

Yeah you should probably fix that right away

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.

Async INetworkListener.AcceptConnection

2 participants