Skip to content

Commit 7ccd612

Browse files
committed
Revert "replacing custom messaging with new disconnect reason (v1)"
This reverts commit a03f3e0.
1 parent a03f3e0 commit 7ccd612

File tree

9 files changed

+82
-77
lines changed

9 files changed

+82
-77
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ public enum ConnectStatus
1313
{
1414
Undefined,
1515
Success, //client successfully connected. This may also be a successful reconnect.
16-
Disconnected,
16+
ServerFull, //can't join, server is already at capacity.
17+
LoggedInAgain, //logged in on a separate client, causing this one to be kicked out.
1718
UserRequestedDisconnect, //Intentional Disconnect triggered by the user.
19+
GenericDisconnect, //server disconnected, but no specific reason given.
1820
Reconnecting, //client lost connection and is attempting to reconnect.
21+
IncompatibleBuildType, //client build type is incompatible with server.
22+
HostEndedSession, //host intentionally ended the session.
1923
StartHostFailed, // server failed to bind
20-
StartClientFailed, // failed to connect to server and/or invalid network endpoint
21-
ConnectionDenied
24+
StartClientFailed // failed to connect to server and/or invalid network endpoint
2225
}
2326

2427
public struct ReconnectMessage
@@ -122,7 +125,6 @@ internal void ChangeState(ConnectionState nextState)
122125

123126
void OnClientDisconnectCallback(ulong clientId)
124127
{
125-
Debug.Log(NetworkManager.DisconnectReason);
126128
m_CurrentState.OnClientDisconnect(clientId);
127129
}
128130

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectedState.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,8 @@ public override void Exit() { }
2626

2727
public override void OnClientDisconnect(ulong _)
2828
{
29-
if (string.IsNullOrEmpty(m_ConnectionManager.NetworkManager.DisconnectReason))
30-
{
31-
m_ConnectStatusPublisher.Publish(ConnectStatus.Reconnecting);
32-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientReconnecting);
33-
}
34-
else
35-
{
36-
m_ConnectStatusPublisher.Publish(ConnectStatus.Disconnected);
37-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
38-
}
29+
m_ConnectStatusPublisher.Publish(ConnectStatus.Reconnecting);
30+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientReconnecting);
3931
}
4032

4133
public override void OnUserRequestedShutdown()

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,7 @@ public override void OnClientDisconnect(ulong _)
4949

5050
protected void StartingClientFailedAsync()
5151
{
52-
if (string.IsNullOrEmpty(m_ConnectionManager.NetworkManager.DisconnectReason))
53-
{
54-
m_ConnectStatusPublisher.Publish(ConnectStatus.StartClientFailed);
55-
}
56-
else
57-
{
58-
m_ConnectStatusPublisher.Publish(ConnectStatus.ConnectionDenied);
59-
}
52+
m_ConnectStatusPublisher.Publish(ConnectStatus.StartClientFailed);
6053
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
6154
}
6255

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void OnClientDisconnect(ulong _)
5353
}
5454
else
5555
{
56-
m_ConnectStatusPublisher.Publish(ConnectStatus.Disconnected);
56+
m_ConnectStatusPublisher.Publish(ConnectStatus.GenericDisconnect);
5757
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
5858
}
5959
}
@@ -64,7 +64,8 @@ public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
6464
switch (disconnectReason)
6565
{
6666
case ConnectStatus.UserRequestedDisconnect:
67-
case ConnectStatus.ConnectionDenied:
67+
case ConnectStatus.HostEndedSession:
68+
case ConnectStatus.ServerFull:
6869
m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason);
6970
break;
7071
}

Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override void OnClientDisconnect(ulong clientId)
6262
var sessionData = SessionManager<SessionPlayerData>.Instance.GetPlayerData(playerId);
6363
if (sessionData.HasValue)
6464
{
65-
m_ConnectionEventPublisher.Publish(new ConnectionEventMessage() { ConnectStatus = ConnectStatus.Disconnected, PlayerName = sessionData.Value.PlayerName });
65+
m_ConnectionEventPublisher.Publish(new ConnectionEventMessage() { ConnectStatus = ConnectStatus.GenericDisconnect, PlayerName = sessionData.Value.PlayerName });
6666
}
6767
SessionManager<SessionPlayerData>.Instance.DisconnectClient(clientId);
6868
}
@@ -71,19 +71,9 @@ public override void OnClientDisconnect(ulong clientId)
7171

7272
public override void OnUserRequestedShutdown()
7373
{
74-
ulong[] clientIds = new ulong[m_ConnectionManager.NetworkManager.ConnectedClientsIds.Count];
75-
var i = 0;
76-
foreach (var id in m_ConnectionManager.NetworkManager.ConnectedClientsIds)
77-
{
78-
clientIds[i] = id;
79-
i++;
80-
}
81-
82-
foreach (var id in clientIds)
83-
{
84-
m_ConnectionManager.NetworkManager.DisconnectClient(id, "Host has ended game session.");
85-
}
86-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
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());
8777
}
8878

8979
/// <summary>
@@ -114,41 +104,62 @@ public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest requ
114104

115105
var payload = System.Text.Encoding.UTF8.GetString(connectionData);
116106
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
107+
var gameReturnStatus = GetConnectStatus(connectionPayload);
117108

118-
if (m_ConnectionManager.NetworkManager.ConnectedClientsIds.Count >= m_ConnectionManager.MaxConnectedPlayers)
119-
{
120-
response.Reason = "The Host is full and cannot accept any additional connections.";
121-
response.Approved = false;
122-
}
123-
else if (connectionPayload.isDebug != Debug.isDebugBuild)
124-
{
125-
response.Reason = "Server and client builds are not compatible. You cannot connect a release build to a development build or an in-editor session.";
126-
response.Approved = false;
127-
}
128-
else if (SessionManager<SessionPlayerData>.Instance.IsDuplicateConnection(connectionPayload.playerId))
129-
{
130-
response.Reason = "You have logged in elsewhere using the same account. If you still want to connect, select a different profile by using the 'Change Profile' button.";
131-
response.Approved = false;
132-
}
133-
else
109+
if (gameReturnStatus == ConnectStatus.Success)
134110
{
135111
SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
136112
new SessionPlayerData(clientId, connectionPayload.playerName, new NetworkGuid(), 0, true));
137113

138114
// connection approval will create a player object for you
115+
response.Approved = true;
139116
response.CreatePlayerObject = true;
140117
response.Position = Vector3.zero;
141118
response.Rotation = Quaternion.identity;
142-
response.Approved = true;
119+
return;
143120
}
144121

145-
if (!response.Approved)
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()
146127
{
147-
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
148-
{
149-
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id);
150-
}
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());
137+
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
138+
{
139+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id);
140+
}
141+
}
142+
143+
ConnectStatus GetConnectStatus(ConnectionPayload connectionPayload)
144+
{
145+
if (m_ConnectionManager.NetworkManager.ConnectedClientsIds.Count >= m_ConnectionManager.MaxConnectedPlayers)
146+
{
147+
return ConnectStatus.ServerFull;
148+
}
149+
150+
if (connectionPayload.isDebug != Debug.isDebugBuild)
151+
{
152+
return ConnectStatus.IncompatibleBuildType;
151153
}
154+
155+
return SessionManager<SessionPlayerData>.Instance.IsDuplicateConnection(connectionPayload.playerId) ?
156+
ConnectStatus.LoggedInAgain : ConnectStatus.Success;
157+
}
158+
159+
IEnumerator WaitToShutdown()
160+
{
161+
yield return null;
162+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
152163
}
153164
}
154165
}

Assets/Scripts/Gameplay/UI/ConnectionStatusMessageUIManager.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Unity.BossRoom.ConnectionManagement;
33
using Unity.BossRoom.Infrastructure;
4-
using Unity.Netcode;
54
using UnityEngine;
65
using VContainer;
76

@@ -40,17 +39,23 @@ void OnConnectStatus(ConnectStatus status)
4039
{
4140
case ConnectStatus.Undefined:
4241
case ConnectStatus.UserRequestedDisconnect:
42+
break;
43+
case ConnectStatus.ServerFull:
44+
PopupManager.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
45+
break;
4346
case ConnectStatus.Success:
4447
break;
45-
case ConnectStatus.Disconnected:
46-
if (!string.IsNullOrEmpty(NetworkManager.Singleton.DisconnectReason))
47-
{
48-
PopupManager.ShowPopupPanel("Disconnected From Host", NetworkManager.Singleton.DisconnectReason);
49-
}
50-
else
51-
{
52-
PopupManager.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost.");
53-
}
48+
case ConnectStatus.LoggedInAgain:
49+
PopupManager.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account. If you still want to connect, select a different profile by using the 'Change Profile' button.");
50+
break;
51+
case ConnectStatus.IncompatibleBuildType:
52+
PopupManager.ShowPopupPanel("Connection Failed", "Server and client builds are not compatible. You cannot connect a release build to a development build or an in-editor session.");
53+
break;
54+
case ConnectStatus.GenericDisconnect:
55+
PopupManager.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost.");
56+
break;
57+
case ConnectStatus.HostEndedSession:
58+
PopupManager.ShowPopupPanel("Disconnected From Host", "The host has ended the game session.");
5459
break;
5560
case ConnectStatus.Reconnecting:
5661
break;
@@ -60,9 +65,6 @@ void OnConnectStatus(ConnectStatus status)
6065
case ConnectStatus.StartClientFailed:
6166
PopupManager.ShowPopupPanel("Connection Failed", "Starting client failed.");
6267
break;
63-
case ConnectStatus.ConnectionDenied:
64-
PopupManager.ShowPopupPanel("Connection Denied", NetworkManager.Singleton.DisconnectReason);
65-
break;
6668
default:
6769
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
6870
break;

Assets/Scripts/Gameplay/UI/Lobby/LobbyUIMediator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ConnectionManager connectionManager
5858

5959
void OnConnectStatus(ConnectStatus status)
6060
{
61-
if (status is ConnectStatus.Disconnected or ConnectStatus.StartClientFailed)
61+
if (status is ConnectStatus.GenericDisconnect or ConnectStatus.StartClientFailed)
6262
{
6363
UnblockUIAfterLoadingIsComplete();
6464
}

Assets/Scripts/Gameplay/UI/UIMessageFeed.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ void OnConnectionEvent(ConnectionEventMessage eventMessage)
6565
case ConnectStatus.Success:
6666
DisplayMessage($"{eventMessage.PlayerName} has joined the game!");
6767
break;
68-
case ConnectStatus.Disconnected:
68+
case ConnectStatus.ServerFull:
69+
case ConnectStatus.LoggedInAgain:
6970
case ConnectStatus.UserRequestedDisconnect:
71+
case ConnectStatus.GenericDisconnect:
72+
case ConnectStatus.IncompatibleBuildType:
73+
case ConnectStatus.HostEndedSession:
7074
DisplayMessage($"{eventMessage.PlayerName} has left the game!");
7175
break;
7276
}

Assets/Tests/Runtime/ConnectionManagementTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public IEnumerator UserRequestedHostShutdownAfterClientsConnected_ClientsDisconn
235235
// ignoring the first success message that is in the buffer
236236
if (message != ConnectStatus.Success)
237237
{
238-
Assert.AreEqual(ConnectStatus.Disconnected, message, "Received unexpected ConnectStatus message.");
238+
Assert.AreEqual(ConnectStatus.HostEndedSession, message, "Received unexpected ConnectStatus message.");
239239
nbHostEndedSessionMsgsReceived++;
240240
}
241241
});
@@ -276,7 +276,7 @@ public IEnumerator AttemptingToConnectWithSamePlayerId_ClientsDisconnectedWithRe
276276
{
277277
subscriptions.Add(m_ClientScopes[i].Container.Resolve<ISubscriber<ConnectStatus>>().Subscribe(message =>
278278
{
279-
Assert.AreEqual(ConnectStatus.ConnectionDenied, message, "Received unexpected ConnectStatus message.");
279+
Assert.AreEqual(ConnectStatus.LoggedInAgain, message, "Received unexpected ConnectStatus message.");
280280
nbLoggedInAgainMsgsReceived++;
281281
}));
282282
}

0 commit comments

Comments
 (0)