@@ -13,14 +13,15 @@ namespace Unity.Multiplayer.Samples.BossRoom
13
13
{
14
14
public class ReconnectingConnectionState : ConnectionState
15
15
{
16
- Coroutine m_ReconnectCoroutine ;
17
- string m_LobbyCode = "" ;
16
+ const int k_NbReconnectAttempts = 2 ;
18
17
19
18
LobbyServiceFacade m_LobbyServiceFacade ;
20
19
LocalLobby m_LocalLobby ;
21
20
IPublisher < ReconnectMessage > m_ReconnectMessagePublisher ;
22
21
23
- const int k_NbReconnectAttempts = 2 ;
22
+ Coroutine m_ReconnectCoroutine ;
23
+ string m_LobbyCode = "" ;
24
+ int m_NbAttempts = 0 ;
24
25
25
26
public ReconnectingConnectionState ( ConnectionManager connectionManager , LobbyServiceFacade lobbyServiceFacade ,
26
27
LocalLobby localLobby , IPublisher < ReconnectMessage > reconnectMessagePublisher )
@@ -34,7 +35,8 @@ public ReconnectingConnectionState(ConnectionManager connectionManager, LobbySer
34
35
public override void Enter ( )
35
36
{
36
37
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 ;
38
40
}
39
41
40
42
public override void Exit ( )
@@ -61,6 +63,9 @@ public override void OnClientDisconnect(ulong clientId)
61
63
case ConnectStatus . ServerFull :
62
64
m_ConnectionManager . ChangeState ( ConnectionStateType . Offline ) ;
63
65
break ;
66
+ default :
67
+ m_ReconnectCoroutine = m_ConnectionManager . StartCoroutine ( ReconnectCoroutine ( ) ) ;
68
+ break ;
64
69
}
65
70
}
66
71
@@ -72,39 +77,35 @@ public override void OnUserRequestedShutdown()
72
77
IEnumerator ReconnectCoroutine ( )
73
78
{
74
79
Debug . Log ( "Lost connection to host, trying to reconnect..." ) ;
75
- int nbTries = 0 ;
76
- while ( nbTries < k_NbReconnectAttempts )
77
- {
78
- NetworkManager . Singleton . Shutdown ( ) ;
79
80
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 )
84
93
{
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 ) ;
99
97
}
100
98
else
101
99
{
102
- ConnectClient ( ) ;
100
+ Debug . Log ( "Failed joining lobby." ) ;
103
101
}
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 ++ ;
107
102
}
103
+ else
104
+ {
105
+ ConnectClient ( ) ;
106
+ }
107
+
108
+ m_NbAttempts ++ ;
108
109
}
109
110
110
111
async Task JoinRelayServerAsync ( )
0 commit comments