Skip to content

Commit 385fff2

Browse files
committed
simplifying reconnect state
(cherry picked from commit 2c83355)
1 parent 314f670 commit 385fff2

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

Assets/Scripts/Gameplay/ConnectionManagement/ConnectionState/ReconnectingConnectionState.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ namespace Unity.Multiplayer.Samples.BossRoom
1313
{
1414
public class ReconnectingConnectionState : ConnectionState
1515
{
16-
Coroutine m_ReconnectCoroutine;
17-
string m_LobbyCode = "";
16+
const int k_NbReconnectAttempts = 2;
1817

1918
LobbyServiceFacade m_LobbyServiceFacade;
2019
LocalLobby m_LocalLobby;
2120
IPublisher<ReconnectMessage> m_ReconnectMessagePublisher;
2221

23-
const int k_NbReconnectAttempts = 2;
22+
Coroutine m_ReconnectCoroutine;
23+
string m_LobbyCode = "";
24+
int m_NbAttempts = 0;
2425

2526
public ReconnectingConnectionState(ConnectionManager connectionManager, LobbyServiceFacade lobbyServiceFacade,
2627
LocalLobby localLobby, IPublisher<ReconnectMessage> reconnectMessagePublisher)
@@ -34,7 +35,8 @@ public ReconnectingConnectionState(ConnectionManager connectionManager, LobbySer
3435
public override void Enter()
3536
{
3637
m_LobbyCode = m_LobbyServiceFacade.CurrentUnityLobby != null ? m_LobbyServiceFacade.CurrentUnityLobby.LobbyCode : "";
37-
m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
38+
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
39+
m_NbAttempts = 0;
3840
}
3941

4042
public override void Exit()
@@ -61,6 +63,9 @@ public override void OnClientDisconnect(ulong clientId)
6163
case ConnectStatus.ServerFull:
6264
m_ConnectionManager.ChangeState(ConnectionStateType.Offline);
6365
break;
66+
default:
67+
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
68+
break;
6469
}
6570
}
6671

@@ -72,39 +77,35 @@ public override void OnUserRequestedShutdown()
7277
IEnumerator ReconnectCoroutine()
7378
{
7479
Debug.Log("Lost connection to host, trying to reconnect...");
75-
int nbTries = 0;
76-
while (nbTries < k_NbReconnectAttempts)
77-
{
78-
NetworkManager.Singleton.Shutdown();
7980

80-
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
81-
Debug.Log($"Reconnecting attempt {nbTries + 1}/{k_NbReconnectAttempts}...");
82-
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(nbTries, k_NbReconnectAttempts));
83-
if (!string.IsNullOrEmpty(m_LobbyCode))
81+
NetworkManager.Singleton.Shutdown();
82+
83+
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
84+
Debug.Log($"Reconnecting attempt {m_NbAttempts + 1}/{k_NbReconnectAttempts}...");
85+
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(m_NbAttempts, k_NbReconnectAttempts));
86+
if (!string.IsNullOrEmpty(m_LobbyCode))
87+
{
88+
var leavingLobby = m_LobbyServiceFacade.EndTracking();
89+
yield return new WaitUntil(() => leavingLobby.IsCompleted);
90+
var joiningLobby = m_LobbyServiceFacade.TryJoinLobbyAsync("", m_LobbyCode);
91+
yield return new WaitUntil(() => joiningLobby.IsCompleted);
92+
if (joiningLobby.Result.Success)
8493
{
85-
var leavingLobby = m_LobbyServiceFacade.EndTracking();
86-
yield return new WaitUntil(() => leavingLobby.IsCompleted);
87-
var joiningLobby = m_LobbyServiceFacade.TryJoinLobbyAsync("", m_LobbyCode);
88-
yield return new WaitUntil(() => joiningLobby.IsCompleted);
89-
if (joiningLobby.Result.Success)
90-
{
91-
m_LobbyServiceFacade.SetRemoteLobby(joiningLobby.Result.Lobby);
92-
var joiningRelay = JoinRelayServerAsync();
93-
yield return new WaitUntil(() => joiningRelay.IsCompleted);
94-
}
95-
else
96-
{
97-
Debug.Log("Failed joining lobby.");
98-
}
94+
m_LobbyServiceFacade.SetRemoteLobby(joiningLobby.Result.Lobby);
95+
var joiningRelay = JoinRelayServerAsync();
96+
yield return new WaitUntil(() => joiningRelay.IsCompleted);
9997
}
10098
else
10199
{
102-
ConnectClient();
100+
Debug.Log("Failed joining lobby.");
103101
}
104-
105-
yield return new WaitForSeconds(1.1f * NetworkManager.Singleton.NetworkConfig.ClientConnectionBufferTimeout + ((UnityTransport) NetworkManager.Singleton.NetworkConfig.NetworkTransport).DisconnectTimeoutMS / 1000.0f); // wait a bit longer than the timeout duration to make sure we have enough time to stop this coroutine if successful
106-
nbTries++;
107102
}
103+
else
104+
{
105+
ConnectClient();
106+
}
107+
108+
m_NbAttempts++;
108109
}
109110

110111
async Task JoinRelayServerAsync()

Assets/Scripts/Gameplay/GameplayObjects/Character/PlayerServerCharacter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public static ServerCharacter GetPlayerServerCharacter(ulong ownerClientId)
8181
return playerServerCharacter;
8282
}
8383
}
84-
Debug.LogError($"PlayerServerCharacter owned by client {ownerClientId} not found");
8584
return null;
8685
}
8786
}

0 commit comments

Comments
 (0)