Skip to content

Commit 7ec53d1

Browse files
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
1 parent f9abb3c commit 7ec53d1

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public void OnConnectFinished(ConnectStatus status)
142142
m_TryToReconnectCoroutine = null;
143143
}
144144
m_ConnectStatusPub.Publish(status);
145+
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
146+
{
147+
m_LobbyServiceFacade.BeginTracking();
148+
}
145149
}
146150
}
147151

@@ -177,9 +181,8 @@ void OnDisconnectOrTimeout(ulong clientID)
177181
m_TryToReconnectCoroutine ??= StartCoroutine(TryToReconnect(lobbyCode));
178182
}
179183
}
180-
else if (DisconnectReason.Reason == ConnectStatus.GenericDisconnect || DisconnectReason.Reason == ConnectStatus.Undefined)
184+
else
181185
{
182-
// only call this if generic disconnect. Else if there's a reason, there's already code handling that popup
183186
NetworkTimedOut?.Invoke();
184187
}
185188

@@ -203,7 +206,7 @@ private IEnumerator TryToReconnect(string lobbyCode)
203206
yield return new WaitUntil(() => leavingLobby.IsCompleted);
204207
var joiningLobby = m_LobbyServiceFacade.JoinLobbyAsync("", lobbyCode, onSuccess: lobby =>
205208
{
206-
m_LobbyServiceFacade.BeginTracking(lobby);
209+
m_LobbyServiceFacade.SetRemoteLobby(lobby);
207210
ConnectClient(null);
208211
}
209212
, null);

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

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

200+
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
201+
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
202+
200203
ConnectStatus gameReturnStatus;
201204

202205
// 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
@@ -205,53 +208,42 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
205208
if (m_Portal.NetManager.ConnectedClientsIds.Count >= CharSelectData.k_MaxLobbyPlayers)
206209
{
207210
gameReturnStatus = ConnectStatus.ServerFull;
208-
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
209-
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
210-
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
211-
//and then manually close down the connection.
212-
SendServerToClientConnectResult(clientId, gameReturnStatus);
213-
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
214-
StartCoroutine(WaitToDisconnect(clientId));
215-
return;
216211
}
217-
218-
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
219-
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
220-
221-
int clientScene = connectionPayload.clientScene;
222-
223-
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);
224-
225-
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
226-
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
227-
? ConnectStatus.Success
228-
: ConnectStatus.LoggedInAgain;
229-
230-
//Test for Duplicate Login.
231-
if (gameReturnStatus == ConnectStatus.LoggedInAgain)
212+
else
232213
{
233-
SessionPlayerData? sessionPlayerData =
234-
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.playerId);
235-
236-
ulong oldClientId = sessionPlayerData?.ClientID ?? 0;
237-
// kicking old client to leave only current
238-
SendServerToClientSetDisconnectReason(oldClientId, ConnectStatus.LoggedInAgain);
214+
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);
239215

240-
StartCoroutine(WaitToDisconnect(clientId));
241-
return;
216+
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
217+
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
218+
? ConnectStatus.Success
219+
: ConnectStatus.LoggedInAgain;
242220
}
243221

244222
if (gameReturnStatus == ConnectStatus.Success)
245223
{
224+
int clientScene = connectionPayload.clientScene;
246225
SendServerToClientConnectResult(clientId, gameReturnStatus);
247226

248227
//Populate our dictionaries with the playerData
249228
m_ClientSceneMap[clientId] = clientScene;
250229

251230
connectionApprovedCallback(true, null, true, Vector3.zero, Quaternion.identity);
252-
253231
// connection approval will create a player object for you
254232
}
233+
else
234+
{
235+
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
236+
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
237+
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
238+
//and then manually close down the connection.
239+
SendServerToClientConnectResult(clientId, gameReturnStatus);
240+
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
241+
StartCoroutine(WaitToDisconnect(clientId));
242+
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
243+
{
244+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
245+
}
246+
}
255247
}
256248

257249
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)