Skip to content

Commit 66854e2

Browse files
committed
fix: leaving lobby when connection denied (#551)
* client now leaves lobby if their connection gets denied * host now removes client from lobby when denying connection * Moved beginning of lobby tracking to after successful connection through ngo (cherry picked from commit 7ec53d1)
1 parent 968029c commit 66854e2

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void OnFailedLobbyCreateOrJoin()
130130
void OnCreatedLobby(Lobby lobby)
131131
{
132132
m_LocalUser.IsHost = true;
133-
m_LobbyServiceFacade.BeginTracking(lobby);
133+
m_LobbyServiceFacade.SetRemoteLobby(lobby);
134134

135135
m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;
136136

@@ -150,7 +150,7 @@ void OnCreatedLobby(Lobby lobby)
150150

151151
void OnJoinedLobby(Lobby remoteLobby)
152152
{
153-
m_LobbyServiceFacade.BeginTracking(remoteLobby);
153+
m_LobbyServiceFacade.SetRemoteLobby(remoteLobby);
154154
m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;
155155

156156
switch (m_LocalLobby.OnlineMode)
@@ -243,6 +243,7 @@ void UnblockUIAfterLoadingIsComplete()
243243
/// </summary>
244244
void OnNetworkTimeout()
245245
{
246+
m_LobbyServiceFacade.EndTracking();
246247
UnblockUIAfterLoadingIsComplete();
247248
}
248249
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public void OnConnectFinished(ConnectStatus status)
112112
else
113113
{
114114
m_ConnectStatusPub.Publish(status);
115+
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
116+
{
117+
m_LobbyServiceFacade.BeginTracking();
118+
}
115119
}
116120
}
117121

@@ -145,9 +149,8 @@ private void OnDisconnectOrTimeout(ulong clientID)
145149
}
146150
m_ApplicationController.LeaveSession();
147151
}
148-
else if (DisconnectReason.Reason == ConnectStatus.GenericDisconnect || DisconnectReason.Reason == ConnectStatus.Undefined)
152+
else
149153
{
150-
// only call this if generic disconnect. Else if there's a reason, there's already code handling that popup
151154
NetworkTimedOut?.Invoke();
152155
}
153156
m_ConnectStatusPub.Publish(DisconnectReason.Reason);

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
178178
return;
179179
}
180180

181+
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
182+
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
183+
181184
ConnectStatus gameReturnStatus;
182185

183186
// Test for over-capacity connection. This needs to be done asap, to make sure we refuse connections asap and don't spend useless time server side
@@ -186,53 +189,42 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
186189
if (m_Portal.NetManager.ConnectedClientsIds.Count >= CharSelectData.k_MaxLobbyPlayers)
187190
{
188191
gameReturnStatus = ConnectStatus.ServerFull;
189-
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
190-
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
191-
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
192-
//and then manually close down the connection.
193-
SendServerToClientConnectResult(clientId, gameReturnStatus);
194-
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
195-
StartCoroutine(WaitToDisconnect(clientId));
196-
return;
197192
}
198-
199-
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
200-
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
201-
202-
int clientScene = connectionPayload.clientScene;
203-
204-
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);
205-
206-
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
207-
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
208-
? ConnectStatus.Success
209-
: ConnectStatus.LoggedInAgain;
210-
211-
//Test for Duplicate Login.
212-
if (gameReturnStatus == ConnectStatus.LoggedInAgain)
193+
else
213194
{
214-
SessionPlayerData? sessionPlayerData =
215-
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.playerId);
216-
217-
ulong oldClientId = sessionPlayerData?.ClientID ?? 0;
218-
// kicking old client to leave only current
219-
SendServerToClientSetDisconnectReason(oldClientId, ConnectStatus.LoggedInAgain);
195+
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);
220196

221-
StartCoroutine(WaitToDisconnect(clientId));
222-
return;
197+
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
198+
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
199+
? ConnectStatus.Success
200+
: ConnectStatus.LoggedInAgain;
223201
}
224202

225203
if (gameReturnStatus == ConnectStatus.Success)
226204
{
205+
int clientScene = connectionPayload.clientScene;
227206
SendServerToClientConnectResult(clientId, gameReturnStatus);
228207

229208
//Populate our dictionaries with the playerData
230209
m_ClientSceneMap[clientId] = clientScene;
231210

232211
connectionApprovedCallback(true, null, true, Vector3.zero, Quaternion.identity);
233-
234212
// connection approval will create a player object for you
235213
}
214+
else
215+
{
216+
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
217+
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
218+
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
219+
//and then manually close down the connection.
220+
SendServerToClientConnectResult(clientId, gameReturnStatus);
221+
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
222+
StartCoroutine(WaitToDisconnect(clientId));
223+
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
224+
{
225+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
226+
}
227+
}
236228
}
237229

238230
IEnumerator WaitToDisconnect(ulong clientId)

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyServiceFacade.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ public void Dispose()
7474
m_ServiceScope?.Dispose();
7575
}
7676

77-
public void BeginTracking(Lobby lobby)
77+
public void SetRemoteLobby(Lobby lobby)
78+
{
79+
CurrentUnityLobby = lobby;
80+
m_LocalLobby.ApplyRemoteData(lobby);
81+
}
82+
83+
public void BeginTracking()
7884
{
7985
if(!m_IsTracking)
8086
{
8187
m_IsTracking = true;
82-
CurrentUnityLobby = lobby;
83-
m_LocalLobby.ApplyRemoteData(lobby);
8488
// 2s update cadence is arbitrary and is here to demonstrate the fact that this update can be rather infrequent
8589
// the actual rate limits are tracked via the RateLimitCooldown objects defined above
8690
m_UpdateRunner.Subscribe(UpdateLobby, 2f);
@@ -91,12 +95,8 @@ public void BeginTracking(Lobby lobby)
9195
public Task EndTracking()
9296
{
9397
var task = Task.CompletedTask;
94-
if (m_IsTracking)
98+
if (CurrentUnityLobby != null)
9599
{
96-
m_UpdateRunner.Unsubscribe(UpdateLobby);
97-
m_IsTracking = false;
98-
m_HeartbeatTime = 0;
99-
m_JoinedLobbyContentHeartbeat.EndTracking();
100100
CurrentUnityLobby = null;
101101

102102
if (!string.IsNullOrEmpty(m_LocalLobby?.LobbyID))
@@ -108,6 +108,13 @@ public Task EndTracking()
108108
m_LocalLobby?.Reset(m_LocalUser);
109109
}
110110

111+
if (m_IsTracking)
112+
{
113+
m_UpdateRunner.Unsubscribe(UpdateLobby);
114+
m_IsTracking = false;
115+
m_HeartbeatTime = 0;
116+
m_JoinedLobbyContentHeartbeat.EndTracking();
117+
}
111118
return task;
112119
}
113120

0 commit comments

Comments
 (0)