Skip to content

Commit cc3e11a

Browse files
committed
Merge branch 'develop' into sam/feat/adding-new-index-entries
# Conflicts: # README.md
2 parents f447f17 + 5821500 commit cc3e11a

14 files changed

+82
-133
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class ConnectionManager : MonoBehaviour
7676
internal readonly ClientConnectingState m_ClientConnecting = new ClientConnectingState();
7777
internal readonly ClientConnectedState m_ClientConnected = new ClientConnectedState();
7878
internal readonly ClientReconnectingState m_ClientReconnecting = new ClientReconnectingState();
79-
internal readonly DisconnectingWithReasonState m_DisconnectingWithReason = new DisconnectingWithReasonState();
8079
internal readonly StartingHostState m_StartingHost = new StartingHostState();
8180
internal readonly HostingState m_Hosting = new HostingState();
8281

@@ -87,7 +86,7 @@ void Awake()
8786

8887
void Start()
8988
{
90-
List<ConnectionState> states = new() { m_Offline, m_ClientConnecting, m_ClientConnected, m_ClientReconnecting, m_DisconnectingWithReason, m_StartingHost, m_Hosting };
89+
List<ConnectionState> states = new() { m_Offline, m_ClientConnecting, m_ClientConnected, m_ClientReconnecting, m_StartingHost, m_Hosting };
9190
foreach (var connectionState in states)
9291
{
9392
m_Resolver.Inject(connectionState);
@@ -172,43 +171,5 @@ public void RequestShutdown()
172171
{
173172
m_CurrentState.OnUserRequestedShutdown();
174173
}
175-
176-
/// <summary>
177-
/// Registers the message handler for custom named messages. This should only be done once StartClient has been
178-
/// called (start client will initialize NetworkSceneManager and CustomMessagingManager)
179-
/// </summary>
180-
public void RegisterCustomMessages()
181-
{
182-
NetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(nameof(ReceiveServerToClientSetDisconnectReason_CustomMessage), ReceiveServerToClientSetDisconnectReason_CustomMessage);
183-
}
184-
185-
void ReceiveServerToClientSetDisconnectReason_CustomMessage(ulong clientID, FastBufferReader reader)
186-
{
187-
reader.ReadValueSafe(out ConnectStatus status);
188-
m_CurrentState.OnDisconnectReasonReceived(status);
189-
}
190-
191-
/// <summary>
192-
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the clients.
193-
/// </summary>
194-
/// <param name="status"> The reason for the upcoming disconnect.</param>
195-
public void SendServerToAllClientsSetDisconnectReason(ConnectStatus status)
196-
{
197-
var writer = new FastBufferWriter(sizeof(ConnectStatus), Allocator.Temp);
198-
writer.WriteValueSafe(status);
199-
NetworkManager.CustomMessagingManager.SendNamedMessageToAll(nameof(ReceiveServerToClientSetDisconnectReason_CustomMessage), writer);
200-
}
201-
202-
/// <summary>
203-
/// Sends a DisconnectReason to the indicated client. This should only be done on the server, prior to disconnecting the client.
204-
/// </summary>
205-
/// <param name="clientID"> id of the client to send to </param>
206-
/// <param name="status"> The reason for the upcoming disconnect.</param>
207-
public void SendServerToClientSetDisconnectReason(ulong clientID, ConnectStatus status)
208-
{
209-
var writer = new FastBufferWriter(sizeof(ConnectStatus), Allocator.Temp);
210-
writer.WriteValueSafe(status);
211-
NetworkManager.CustomMessagingManager.SendNamedMessage(nameof(ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
212-
}
213174
}
214175
}

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectedState.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Unity.BossRoom.ConnectionManagement
77
{
88
/// <summary>
99
/// Connection state corresponding to a connected client. When being disconnected, transitions to the
10-
/// ClientReconnecting state. When receiving a disconnect reason, transitions to the DisconnectingWithReason state.
10+
/// ClientReconnecting state if no reason is given, or to the Offline state.
1111
/// </summary>
1212
class ClientConnectedState : ConnectionState
1313
{
@@ -26,20 +26,24 @@ public override void Exit() { }
2626

2727
public override void OnClientDisconnect(ulong _)
2828
{
29-
m_ConnectStatusPublisher.Publish(ConnectStatus.Reconnecting);
30-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientReconnecting);
29+
var disconnectReason = m_ConnectionManager.NetworkManager.DisconnectReason;
30+
if (string.IsNullOrEmpty(disconnectReason))
31+
{
32+
m_ConnectStatusPublisher.Publish(ConnectStatus.Reconnecting);
33+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientReconnecting);
34+
}
35+
else
36+
{
37+
var connectStatus = JsonUtility.FromJson<ConnectStatus>(disconnectReason);
38+
m_ConnectStatusPublisher.Publish(connectStatus);
39+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
40+
}
3141
}
3242

3343
public override void OnUserRequestedShutdown()
3444
{
3545
m_ConnectStatusPublisher.Publish(ConnectStatus.UserRequestedDisconnect);
3646
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
3747
}
38-
39-
public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
40-
{
41-
m_ConnectStatusPublisher.Publish(disconnectReason);
42-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason);
43-
}
4448
}
4549
}

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace Unity.BossRoom.ConnectionManagement
99
{
1010
/// <summary>
1111
/// Connection state corresponding to when a client is attempting to connect to a server. Starts the client when
12-
/// entering. If successful, transitions to the ClientConnected state. If not, transitions to the Offline state. If
13-
/// given a disconnect reason first, transitions to the DisconnectingWithReason state.
12+
/// entering. If successful, transitions to the ClientConnected state. If not, transitions to the Offline state.
1413
/// </summary>
1514
class ClientConnectingState : OnlineState
1615
{
@@ -49,16 +48,19 @@ public override void OnClientDisconnect(ulong _)
4948

5049
protected void StartingClientFailedAsync()
5150
{
52-
m_ConnectStatusPublisher.Publish(ConnectStatus.StartClientFailed);
51+
var disconnectReason = m_ConnectionManager.NetworkManager.DisconnectReason;
52+
if (string.IsNullOrEmpty(disconnectReason))
53+
{
54+
m_ConnectStatusPublisher.Publish(ConnectStatus.StartClientFailed);
55+
}
56+
else
57+
{
58+
var connectStatus = JsonUtility.FromJson<ConnectStatus>(disconnectReason);
59+
m_ConnectStatusPublisher.Publish(connectStatus);
60+
}
5361
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
5462
}
5563

56-
public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
57-
{
58-
m_ConnectStatusPublisher.Publish(disconnectReason);
59-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason);
60-
}
61-
6264

6365
internal async Task ConnectClientAsync()
6466
{
@@ -74,7 +76,6 @@ internal async Task ConnectClientAsync()
7476
}
7577

7678
SceneLoaderWrapper.Instance.AddOnSceneEventCallback();
77-
m_ConnectionManager.RegisterCustomMessages();
7879
}
7980
catch (Exception e)
8081
{

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ namespace Unity.BossRoom.ConnectionManagement
88
{
99
/// <summary>
1010
/// Connection state corresponding to a client attempting to reconnect to a server. It will try to reconnect a
11-
/// number of times defined by k_NbReconnectAttempts. If it succeeds, it will transition to the ClientConnected
12-
/// state. If not, it will transition to the Offline state. If given a disconnect reason first, depending on the
13-
/// reason given, may transition to the DisconnectingWithReason state.
11+
/// number of times defined by the ConnectionManager's NbReconnectAttempts property. If it succeeds, it will
12+
/// transition to the ClientConnected state. If not, it will transition to the Offline state. If given a disconnect
13+
/// reason first, depending on the reason given, may not try to reconnect again and transition directly to the
14+
/// Offline state.
1415
/// </summary>
1516
class ClientReconnectingState : ClientConnectingState
1617
{
@@ -47,27 +48,44 @@ public override void OnClientConnected(ulong _)
4748

4849
public override void OnClientDisconnect(ulong _)
4950
{
51+
var disconnectReason = m_ConnectionManager.NetworkManager.DisconnectReason;
5052
if (m_NbAttempts < m_ConnectionManager.NbReconnectAttempts)
5153
{
52-
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
54+
if (string.IsNullOrEmpty(disconnectReason))
55+
{
56+
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
57+
}
58+
else
59+
{
60+
var connectStatus = JsonUtility.FromJson<ConnectStatus>(disconnectReason);
61+
m_ConnectStatusPublisher.Publish(connectStatus);
62+
switch (connectStatus)
63+
{
64+
case ConnectStatus.UserRequestedDisconnect:
65+
case ConnectStatus.HostEndedSession:
66+
case ConnectStatus.ServerFull:
67+
case ConnectStatus.IncompatibleBuildType:
68+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
69+
break;
70+
default:
71+
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
72+
break;
73+
}
74+
}
5375
}
5476
else
5577
{
56-
m_ConnectStatusPublisher.Publish(ConnectStatus.GenericDisconnect);
57-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
58-
}
59-
}
78+
if (string.IsNullOrEmpty(disconnectReason))
79+
{
80+
m_ConnectStatusPublisher.Publish(ConnectStatus.GenericDisconnect);
81+
}
82+
else
83+
{
84+
var connectStatus = JsonUtility.FromJson<ConnectStatus>(disconnectReason);
85+
m_ConnectStatusPublisher.Publish(connectStatus);
86+
}
6087

61-
public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
62-
{
63-
m_ConnectStatusPublisher.Publish(disconnectReason);
64-
switch (disconnectReason)
65-
{
66-
case ConnectStatus.UserRequestedDisconnect:
67-
case ConnectStatus.HostEndedSession:
68-
case ConnectStatus.ServerFull:
69-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason);
70-
break;
88+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
7189
}
7290
}
7391

Assets/Scripts/ConnectionManagement/ConnectionState/ConnectionState.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public virtual void StartHostLobby(string playerName) { }
3636

3737
public virtual void OnUserRequestedShutdown() { }
3838

39-
public virtual void OnDisconnectReasonReceived(ConnectStatus disconnectReason) { }
40-
4139
public virtual void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response) { }
4240

4341
public virtual void OnTransportFailure() { }

Assets/Scripts/ConnectionManagement/ConnectionState/DisconnectingWithReasonState.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

Assets/Scripts/ConnectionManagement/ConnectionState/DisconnectingWithReasonState.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,16 @@ public override void OnClientDisconnect(ulong clientId)
7171

7272
public override void OnUserRequestedShutdown()
7373
{
74-
m_ConnectionManager.SendServerToAllClientsSetDisconnectReason(ConnectStatus.HostEndedSession);
75-
// Wait before shutting down to make sure clients receive that message before they are disconnected
76-
m_ConnectionManager.StartCoroutine(WaitToShutdown());
74+
var reason = JsonUtility.ToJson(ConnectStatus.HostEndedSession);
75+
for (var i = m_ConnectionManager.NetworkManager.ConnectedClientsIds.Count - 1; i >= 0; i--)
76+
{
77+
var id = m_ConnectionManager.NetworkManager.ConnectedClientsIds[i];
78+
if (id != m_ConnectionManager.NetworkManager.LocalClientId)
79+
{
80+
m_ConnectionManager.NetworkManager.DisconnectClient(id, reason);
81+
}
82+
}
83+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
7784
}
7885

7986
/// <summary>
@@ -119,21 +126,8 @@ public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest requ
119126
return;
120127
}
121128

122-
// In order for clients to not just get disconnected with no feedback, the server needs to tell the client why it disconnected it.
123-
// This could happen after an auth check on a service or because of gameplay reasons (server full, wrong build version, etc)
124-
// Since network objects haven't synced yet (still in the approval process), we need to send a custom message to clients, wait for
125-
// UTP to update a frame and flush that message, then give our response to NetworkManager's connection approval process, with a denied approval.
126-
IEnumerator WaitToDenyApproval()
127-
{
128-
response.Pending = true; // give some time for server to send connection status message to clients
129-
response.Approved = false;
130-
m_ConnectionManager.SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
131-
yield return null; // wait a frame so UTP can flush it's messages on next update
132-
response.Pending = false; // connection approval process can be finished.
133-
}
134-
135-
m_ConnectionManager.SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
136-
m_ConnectionManager.StartCoroutine(WaitToDenyApproval());
129+
response.Approved = false;
130+
response.Reason = JsonUtility.ToJson(gameReturnStatus);
137131
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
138132
{
139133
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id);
@@ -155,11 +149,5 @@ ConnectStatus GetConnectStatus(ConnectionPayload connectionPayload)
155149
return SessionManager<SessionPlayerData>.Instance.IsDuplicateConnection(connectionPayload.playerId) ?
156150
ConnectStatus.LoggedInAgain : ConnectStatus.Success;
157151
}
158-
159-
IEnumerator WaitToShutdown()
160-
{
161-
yield return null;
162-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
163-
}
164152
}
165153
}

Assets/Tests/Runtime/ConnectionManagementTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,6 @@ public IEnumerator ClientCancellingWhileConnectingToListeningServer_ConnectionCa
468468
m_ClientConnectionManagers[i].StartClientIp($"client{i}", "127.0.0.1", 9998);
469469
}
470470

471-
yield return null;
472-
yield return null;
473-
474471
m_ClientConnectionManagers[0].RequestShutdown();
475472

476473
yield return WaitForClientsConnectedOrTimeOut();

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Additional documentation and release notes are available at [Multiplayer Documen
99
## [unreleased] - yyyy-mm-dd
1010
### Changed
1111
* Bumped RNSM to 1.1.0: Switched x axis units to seconds instead of frames now that it's available. This means adjusting the sample count to a lower value as well to 30 seconds, since the x axis was moving too slowly. (#788)
12+
* Updated Boss Room to NGO 1.2.0 (#791).
13+
* Removed a workaround in our tests waiting for two frames before shutting down a client that is attempting to connect to a server. (see https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/pull/2261)
14+
* Replaced the workaround using custom messages to send a disconnect reason to clients with the new DisconnectReason feature in NGO. (#790)
1215

1316
## [2.0.3] - 2022-12-05
1417

Lines changed: 2 additions & 2 deletions
Loading

Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"com.unity.learn.iet-framework": "2.2.2",
1111
"com.unity.memoryprofiler": "0.5.0-preview.1",
1212
"com.unity.multiplayer.tools": "1.1.0",
13-
"com.unity.netcode.gameobjects": "1.1.0",
13+
"com.unity.netcode.gameobjects": "1.2.0",
1414
"com.unity.performance.profile-analyzer": "1.1.1",
1515
"com.unity.postprocessing": "3.2.2",
1616
"com.unity.render-pipelines.universal": "12.1.7",

Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"url": "https://packages.unity.com"
143143
},
144144
"com.unity.netcode.gameobjects": {
145-
"version": "1.1.0",
145+
"version": "1.2.0",
146146
"depth": 0,
147147
"source": "registry",
148148
"dependencies": {

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ Running the game over internet currently requires setting up a relay.
179179
* Win state - [Assets/Scripts/Gameplay/GameState/PersistentGameState.cs](Assets/Scripts/Gameplay/GameState/PersistentGameState.cs)
180180

181181
### Connectivity
182-
* Connection approval return value with custom messaging - WaitToDenyApproval() in [Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs ](Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs)
182+
* Disconnecting every client with reason - OnUserRequestedShutdown() in [Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs ](Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs)
183+
* Connection approval with reason sent to the client when denied - ApprovalCheck() in [Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs ](Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs)
183184
* Connection state machine with error handling - [Assets/Scripts/ConnectionManagement/ConnectionManager.cs ](Assets/Scripts/ConnectionManagement/ConnectionManager.cs) <br> [Assets/Scripts/ConnectionManagement/ConnectionState/](Assets/Scripts/ConnectionManagement/ConnectionState/)
184185
* UTP setup for IP - ConnectionMethodIP in [Assets/Scripts/ConnectionManagement/ConnectionMethod.cs](Assets/Scripts/ConnectionManagement/ConnectionMethod.cs)
185186
* UTP setup for Relay - ConnectionMethodRelay in [Assets/Scripts/ConnectionManagement/ConnectionMethod.cs](Assets/Scripts/ConnectionManagement/ConnectionMethod.cs)

0 commit comments

Comments
 (0)