Skip to content

fix: prevent exceptions when multiple connection approval are used [MTT-3496] #1958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
4 changes: 4 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Fixed NetworkManager to cleanup connected client lists after stopping (#1945)
- Fixed NetworkHide followed by NetworkShow on the same frame works correctly (#1940)

### Changed

- NetworkManager.ConnectionApprovalCallback is now a delegate instead of an event. Adding a second one is forbidden and an error is generated if client code attempts it. (#1941)

## [1.0.0-pre.8] - 2022-04-27

### Changed
Expand Down
20 changes: 19 additions & 1 deletion com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,28 @@ public IReadOnlyList<ulong> ConnectedClientsIds
/// <param name="rotation">The rotation to spawn the client with. If null, the prefab position is used.</param>
public delegate void ConnectionApprovedDelegate(bool createPlayerObject, uint? playerPrefabHash, bool approved, Vector3? position, Quaternion? rotation);

public delegate void ConnectionApprovalDelegate(byte[] payload, ulong clientId, ConnectionApprovedDelegate connectionApprovedCallback);

/// <summary>
/// The callback to invoke during connection approval
/// </summary>
public event Action<byte[], ulong, ConnectionApprovedDelegate> ConnectionApprovalCallback = null;
public ConnectionApprovalDelegate ConnectionApprovalCallback
{
get => m_ConnectionApprovalDelegate;
set
{
if (value != null && value.GetInvocationList().Length > 1)
{
Debug.LogError($"Only one connection approval handler is supported in {nameof(ConnectionApprovalCallback)}. Rejecting further adds.");
}
else
{
m_ConnectionApprovalDelegate = value;
}
}
}

private ConnectionApprovalDelegate m_ConnectionApprovalDelegate = null;

internal void InvokeConnectionApproval(byte[] payload, ulong clientId, ConnectionApprovedDelegate action) => ConnectionApprovalCallback?.Invoke(payload, clientId, action);

Expand Down