Skip to content

fix: leaving lobby when connection denied #551

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
06d32a0
Added call to leave lobby when disconnecting
LPLafontaineB Mar 7, 2022
c3179c4
Added comments and todo for better fix
LPLafontaineB Mar 7, 2022
4bd7922
Merge branch 'release/GDC2022' into fix/leaving-lobby-when-disconnecting
LPLafontaineB Mar 11, 2022
59ea34c
Moved removal responsibility of player from lobby to host
LPLafontaineB Mar 11, 2022
fa7a9cc
Adding leaving lobby then trying to re-join in reconnect coroutine
LPLafontaineB Mar 11, 2022
11b6578
Squashed commit of the following:
LPLafontaineB Mar 11, 2022
4c728e1
Adding injection to ServerGameNetPortal
LPLafontaineB Mar 11, 2022
eb6cf85
replaced dumb while with less dumb yield waitwhile
LPLafontaineB Mar 11, 2022
7140994
Removed unnecessary callback registering
LPLafontaineB Mar 11, 2022
9d6dd18
Fixed infinite loop issue
LPLafontaineB Mar 11, 2022
0c3d0c8
Made sure client leaves lobby by themselves when disconnecting as wel…
LPLafontaineB Mar 11, 2022
4b5244d
Adding verification that player is still in lobby before removing them
LPLafontaineB Mar 11, 2022
930eda8
Made sure the client leaves the lobby between each reconnection attem…
LPLafontaineB Mar 11, 2022
0c79ecb
set nbReconnectionAttempts to 2
LPLafontaineB Mar 11, 2022
4acbddb
feat: replace guid with auth playerid for session management (#488)
LPLafontaineB Mar 14, 2022
c960453
Merge branch 'feature/cherrypick/player-id-in-session-manager' into f…
LPLafontaineB Mar 14, 2022
5505add
cleaning up
LPLafontaineB Mar 14, 2022
81608c3
Merge branch 'release/GDC2022' into fix/leaving-lobby-when-disconnecting
LPLafontaineB Mar 14, 2022
2862e59
Added todo comment for better way to wait
LPLafontaineB Mar 14, 2022
9ed88b3
reduced number of reconnect attempts to 1
LPLafontaineB Mar 14, 2022
7808a78
client now leaves lobby if their connection gets denied
LPLafontaineB Mar 14, 2022
7b3f7fa
host now removes client from lobby when denying connection
LPLafontaineB Mar 14, 2022
bee9c3a
Moved beginning of lobby tracking to after successful connection thro…
LPLafontaineB Mar 14, 2022
bf23965
Merge branch 'release/GDC2022' into fix/leaving-lobby-when-connection…
LPLafontaineB Mar 14, 2022
0761608
fixing conflicts
LPLafontaineB Mar 14, 2022
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
5 changes: 3 additions & 2 deletions Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void OnFailedLobbyCreateOrJoin()
void OnCreatedLobby(Lobby lobby)
{
m_LocalUser.IsHost = true;
m_LobbyServiceFacade.BeginTracking(lobby);
m_LobbyServiceFacade.SetRemoteLobby(lobby);

m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;

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

void OnJoinedLobby(Lobby remoteLobby)
{
m_LobbyServiceFacade.BeginTracking(remoteLobby);
m_LobbyServiceFacade.SetRemoteLobby(remoteLobby);
m_GameNetPortal.PlayerName = m_LocalUser.DisplayName;

switch (m_LocalLobby.OnlineMode)
Expand Down Expand Up @@ -243,6 +243,7 @@ void UnblockUIAfterLoadingIsComplete()
/// </summary>
void OnNetworkTimeout()
{
m_LobbyServiceFacade.EndTracking();
UnblockUIAfterLoadingIsComplete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public void OnConnectFinished(ConnectStatus status)
m_TryToReconnectCoroutine = null;
}
m_ConnectStatusPub.Publish(status);
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
{
m_LobbyServiceFacade.BeginTracking();
}
}
}

Expand Down Expand Up @@ -180,9 +184,8 @@ private void OnDisconnectOrTimeout(ulong clientID)
m_TryToReconnectCoroutine ??= StartCoroutine(TryToReconnect(lobbyCode));
}
}
else if (DisconnectReason.Reason == ConnectStatus.GenericDisconnect || DisconnectReason.Reason == ConnectStatus.Undefined)
else
{
// only call this if generic disconnect. Else if there's a reason, there's already code handling that popup
NetworkTimedOut?.Invoke();
}

Expand All @@ -206,7 +209,7 @@ private IEnumerator TryToReconnect(string lobbyCode)
yield return new WaitUntil(() => leavingLobby.IsCompleted);
var joiningLobby = m_LobbyServiceFacade.JoinLobbyAsync("", lobbyCode, onSuccess: lobby =>
{
m_LobbyServiceFacade.BeginTracking(lobby);
m_LobbyServiceFacade.SetRemoteLobby(lobby);
ConnectClient(null);
}
, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
return;
}

string payload = System.Text.Encoding.UTF8.GetString(connectionData);
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html

ConnectStatus gameReturnStatus;

// 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
Expand All @@ -201,53 +204,42 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
if (m_Portal.NetManager.ConnectedClientsIds.Count >= CharSelectData.k_MaxLobbyPlayers)
{
gameReturnStatus = ConnectStatus.ServerFull;
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
//and then manually close down the connection.
SendServerToClientConnectResult(clientId, gameReturnStatus);
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
StartCoroutine(WaitToDisconnect(clientId));
return;
}

string payload = System.Text.Encoding.UTF8.GetString(connectionData);
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html

int clientScene = connectionPayload.clientScene;

Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);

gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
? ConnectStatus.Success
: ConnectStatus.LoggedInAgain;

//Test for Duplicate Login.
if (gameReturnStatus == ConnectStatus.LoggedInAgain)
else
{
SessionPlayerData? sessionPlayerData =
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.playerId);

ulong oldClientId = sessionPlayerData?.ClientID ?? 0;
// kicking old client to leave only current
SendServerToClientSetDisconnectReason(oldClientId, ConnectStatus.LoggedInAgain);
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);

StartCoroutine(WaitToDisconnect(clientId));
return;
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
? ConnectStatus.Success
: ConnectStatus.LoggedInAgain;
}

if (gameReturnStatus == ConnectStatus.Success)
{
int clientScene = connectionPayload.clientScene;
SendServerToClientConnectResult(clientId, gameReturnStatus);

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

connectionApprovedCallback(true, null, true, Vector3.zero, Quaternion.identity);

// connection approval will create a player object for you
}
else
{
//TODO-FIXME:Netcode Issue #796. We should be able to send a reason and disconnect without a coroutine delay.
//TODO:Netcode: In the future we expect Netcode to allow us to return more information as part of
//the approval callback, so that we can provide more context on a reject. In the meantime we must provide the extra information ourselves,
//and then manually close down the connection.
SendServerToClientConnectResult(clientId, gameReturnStatus);
SendServerToClientSetDisconnectReason(clientId, gameReturnStatus);
StartCoroutine(WaitToDisconnect(clientId));
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
{
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
}
}
}

IEnumerator WaitToDisconnect(ulong clientId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ public void Dispose()
m_ServiceScope?.Dispose();
}

public void BeginTracking(Lobby lobby)
public void SetRemoteLobby(Lobby lobby)
{
CurrentUnityLobby = lobby;
m_LocalLobby.ApplyRemoteData(lobby);
}

public void BeginTracking()
{
if(!m_IsTracking)
{
m_IsTracking = true;
CurrentUnityLobby = lobby;
m_LocalLobby.ApplyRemoteData(lobby);
// 2s update cadence is arbitrary and is here to demonstrate the fact that this update can be rather infrequent
// the actual rate limits are tracked via the RateLimitCooldown objects defined above
m_UpdateRunner.Subscribe(UpdateLobby, 2f);
Expand All @@ -91,12 +95,8 @@ public void BeginTracking(Lobby lobby)
public Task EndTracking()
{
var task = Task.CompletedTask;
if (m_IsTracking)
if (CurrentUnityLobby != null)
{
m_UpdateRunner.Unsubscribe(UpdateLobby);
m_IsTracking = false;
m_HeartbeatTime = 0;
m_JoinedLobbyContentHeartbeat.EndTracking();
CurrentUnityLobby = null;

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

if (m_IsTracking)
{
m_UpdateRunner.Unsubscribe(UpdateLobby);
m_IsTracking = false;
m_HeartbeatTime = 0;
m_JoinedLobbyContentHeartbeat.EndTracking();
}
return task;
}

Expand Down