Skip to content

Commit

Permalink
Simplifying reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
LPLafontaineB committed Jan 30, 2023
1 parent fa2b4ba commit f9d5ad9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
18 changes: 18 additions & 0 deletions Assets/Scripts/ConnectionManagement/ConnectionMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public abstract class ConnectionMethodBase

public abstract Task SetupClientConnectionAsync();

public abstract Task<bool> SetupClientReconnectionAsync();

public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName)
{
m_ConnectionManager = connectionManager;
Expand Down Expand Up @@ -82,6 +84,12 @@ public override async Task SetupClientConnectionAsync()
utp.SetConnectionData(m_Ipaddress, m_Port);
}

public override Task<bool> SetupClientReconnectionAsync()
{
// Nothing to do here
return Task.FromResult(true);
}

public override async Task SetupHostConnectionAsync()
{
SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too
Expand Down Expand Up @@ -132,6 +140,16 @@ public override async Task SetupClientConnectionAsync()
utp.SetRelayServerData(new RelayServerData(joinedAllocation, k_DtlsConnType));
}

public override async Task<bool> SetupClientReconnectionAsync()
{
// When using Lobby with Relay, if a user is disconnected from the Relay server, the server will notify the
// Lobby service and mark the user as disconnected, but will not remove them from the lobby. They then have
// some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on the dashboard),
// after which they will be removed from the lobby completely.
// See https://docs.unity.com/lobby/reconnect-to-lobby.html
return await m_LobbyServiceFacade.ReconnectToLobbyAsync() != null; // return a success if reconnecting to lobby returns a lobby
}

public override async Task SetupHostConnectionAsync()
{
Debug.Log("Setting up Unity Relay host");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Unity.BossRoom.ConnectionManagement
/// </summary>
class ClientConnectingState : OnlineState
{
ConnectionMethodBase m_ConnectionMethod;
protected ConnectionMethodBase m_ConnectionMethod;

public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,21 @@ IEnumerator ReconnectCoroutine()
Debug.Log($"Reconnecting attempt {m_NbAttempts + 1}/{m_ConnectionManager.NbReconnectAttempts}...");
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(m_NbAttempts, m_ConnectionManager.NbReconnectAttempts));
m_NbAttempts++;
if (m_LobbyServiceFacade.CurrentUnityLobby != null) // Attempting to reconnect to lobby.
{
// When using Lobby with Relay, if a user is disconnected from the Relay server, the server will notify
// the Lobby service and mark the user as disconnected, but will not remove them from the lobby. They
// then have some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on
// the dashboard), after which they will be removed from the lobby completely.
// See https://docs.unity.com/lobby/reconnect-to-lobby.html
var reconnectingToLobby = m_LobbyServiceFacade.ReconnectToLobbyAsync();
yield return new WaitUntil(() => reconnectingToLobby.IsCompleted);
var reconnectingSetupTask = m_ConnectionMethod.SetupClientReconnectionAsync();
yield return new WaitUntil(() => reconnectingSetupTask.IsCompleted);

// If succeeded, attempt to connect to Relay
if (!reconnectingToLobby.IsFaulted && reconnectingToLobby.Result != null)
{
// If this fails, the OnClientDisconnect callback will be invoked by Netcode
var connectingToRelay = ConnectClientAsync();
yield return new WaitUntil(() => connectingToRelay.IsCompleted);
}
else
{
Debug.Log("Failed reconnecting to lobby.");
// Calling OnClientDisconnect to mark this attempt as failed and either start a new one or give up
// and return to the Offline state
OnClientDisconnect(0);
}
}
else // If not using Lobby, simply try to reconnect to the server directly
if (!reconnectingSetupTask.IsFaulted && reconnectingSetupTask.Result)
{
// If this fails, the OnClientDisconnect callback will be invoked by Netcode
var connectingClient = ConnectClientAsync();
yield return new WaitUntil(() => connectingClient.IsCompleted);
var connectingToRelay = ConnectClientAsync();
yield return new WaitUntil(() => connectingToRelay.IsCompleted);
}
else
{
Debug.Log("Failed reconnecting to lobby.");
// Calling OnClientDisconnect to mark this attempt as failed and either start a new one or give up
// and return to the Offline state
OnClientDisconnect(0);
}
}
}
Expand Down

0 comments on commit f9d5ad9

Please sign in to comment.