Skip to content

Commit f9d5ad9

Browse files
committed
Simplifying reconnection
1 parent fa2b4ba commit f9d5ad9

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

Assets/Scripts/ConnectionManagement/ConnectionMethod.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public abstract class ConnectionMethodBase
2727

2828
public abstract Task SetupClientConnectionAsync();
2929

30+
public abstract Task<bool> SetupClientReconnectionAsync();
31+
3032
public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName)
3133
{
3234
m_ConnectionManager = connectionManager;
@@ -82,6 +84,12 @@ public override async Task SetupClientConnectionAsync()
8284
utp.SetConnectionData(m_Ipaddress, m_Port);
8385
}
8486

87+
public override Task<bool> SetupClientReconnectionAsync()
88+
{
89+
// Nothing to do here
90+
return Task.FromResult(true);
91+
}
92+
8593
public override async Task SetupHostConnectionAsync()
8694
{
8795
SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too
@@ -132,6 +140,16 @@ public override async Task SetupClientConnectionAsync()
132140
utp.SetRelayServerData(new RelayServerData(joinedAllocation, k_DtlsConnType));
133141
}
134142

143+
public override async Task<bool> SetupClientReconnectionAsync()
144+
{
145+
// When using Lobby with Relay, if a user is disconnected from the Relay server, the server will notify the
146+
// Lobby service and mark the user as disconnected, but will not remove them from the lobby. They then have
147+
// some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on the dashboard),
148+
// after which they will be removed from the lobby completely.
149+
// See https://docs.unity.com/lobby/reconnect-to-lobby.html
150+
return await m_LobbyServiceFacade.ReconnectToLobbyAsync() != null; // return a success if reconnecting to lobby returns a lobby
151+
}
152+
135153
public override async Task SetupHostConnectionAsync()
136154
{
137155
Debug.Log("Setting up Unity Relay host");

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Unity.BossRoom.ConnectionManagement
1111
/// </summary>
1212
class ClientConnectingState : OnlineState
1313
{
14-
ConnectionMethodBase m_ConnectionMethod;
14+
protected ConnectionMethodBase m_ConnectionMethod;
1515

1616
public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod)
1717
{

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,21 @@ IEnumerator ReconnectCoroutine()
109109
Debug.Log($"Reconnecting attempt {m_NbAttempts + 1}/{m_ConnectionManager.NbReconnectAttempts}...");
110110
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(m_NbAttempts, m_ConnectionManager.NbReconnectAttempts));
111111
m_NbAttempts++;
112-
if (m_LobbyServiceFacade.CurrentUnityLobby != null) // Attempting to reconnect to lobby.
113-
{
114-
// When using Lobby with Relay, if a user is disconnected from the Relay server, the server will notify
115-
// the Lobby service and mark the user as disconnected, but will not remove them from the lobby. They
116-
// then have some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on
117-
// the dashboard), after which they will be removed from the lobby completely.
118-
// See https://docs.unity.com/lobby/reconnect-to-lobby.html
119-
var reconnectingToLobby = m_LobbyServiceFacade.ReconnectToLobbyAsync();
120-
yield return new WaitUntil(() => reconnectingToLobby.IsCompleted);
112+
var reconnectingSetupTask = m_ConnectionMethod.SetupClientReconnectionAsync();
113+
yield return new WaitUntil(() => reconnectingSetupTask.IsCompleted);
121114

122-
// If succeeded, attempt to connect to Relay
123-
if (!reconnectingToLobby.IsFaulted && reconnectingToLobby.Result != null)
124-
{
125-
// If this fails, the OnClientDisconnect callback will be invoked by Netcode
126-
var connectingToRelay = ConnectClientAsync();
127-
yield return new WaitUntil(() => connectingToRelay.IsCompleted);
128-
}
129-
else
130-
{
131-
Debug.Log("Failed reconnecting to lobby.");
132-
// Calling OnClientDisconnect to mark this attempt as failed and either start a new one or give up
133-
// and return to the Offline state
134-
OnClientDisconnect(0);
135-
}
136-
}
137-
else // If not using Lobby, simply try to reconnect to the server directly
115+
if (!reconnectingSetupTask.IsFaulted && reconnectingSetupTask.Result)
138116
{
139117
// If this fails, the OnClientDisconnect callback will be invoked by Netcode
140-
var connectingClient = ConnectClientAsync();
141-
yield return new WaitUntil(() => connectingClient.IsCompleted);
118+
var connectingToRelay = ConnectClientAsync();
119+
yield return new WaitUntil(() => connectingToRelay.IsCompleted);
120+
}
121+
else
122+
{
123+
Debug.Log("Failed reconnecting to lobby.");
124+
// Calling OnClientDisconnect to mark this attempt as failed and either start a new one or give up
125+
// and return to the Offline state
126+
OnClientDisconnect(0);
142127
}
143128
}
144129
}

0 commit comments

Comments
 (0)