Skip to content

Commit ad2fae2

Browse files
committed
Merge branch 'release/GDC2022' into fix/simpler-quitting-flow
2 parents 9cbfc39 + ef6fd8c commit ad2fae2

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

Assets/BossRoom/Scripts/Client/UI/ConnectionStatusMessageUIManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@ void OnConnectStatus(ConnectStatus status)
4242
PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
4343
break;
4444
case ConnectStatus.Success:
45-
m_PopupIdToClose = PopupPanel.ShowPopupPanel("Success!", "Joining Now", isCloseableByUser: false);
45+
m_PopupIdToClose = PopupPanel.ShowPopupPanel("Success!", "Joining Now...", isCloseableByUser: false);
4646
SceneManager.sceneLoaded += ClosePopupOnsceneLoaded;
4747
break;
4848
case ConnectStatus.LoggedInAgain:
4949
PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
5050
break;
5151
case ConnectStatus.GenericDisconnect:
52-
PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost");
52+
PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost.");
5353
break;
5454
case ConnectStatus.Reconnecting:
5555
m_PopupIdToClose = PopupPanel.ShowPopupPanel("Attempting reconnection", "Lost connection to the Host, attempting to reconnect...", isCloseableByUser: false);
5656
break;
57+
case ConnectStatus.HostDisconnected:
58+
PopupPanel.ShowPopupPanel("Disconnected From Host", "The host has ended the game session.");
59+
break;
5760
default:
5861
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
5962
break;

Assets/BossRoom/Scripts/Client/UI/UnityServicesUIHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
2727
var errorMessage = error.Message;
2828
if (error.AffectedService == UnityServiceErrorMessage.Service.Lobby)
2929
{
30-
if ((error.OriginalException as LobbyServiceException).Reason == LobbyExceptionReason.LobbyConflict)
30+
if (error.OriginalException is LobbyServiceException {Reason: LobbyExceptionReason.LobbyConflict})
3131
{
3232
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
3333
errorMessage += "\nSee logs for possible causes and solution.";
@@ -44,4 +44,4 @@ void OnDestroy()
4444
m_Subscriptions.Dispose();
4545
}
4646
}
47-
}
47+
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ClientGameNetPortal : MonoBehaviour
3131
/// Time in seconds before the client considers a lack of server response a timeout
3232
/// </summary>
3333
private const int k_TimeoutDuration = 10;
34+
const int k_NbReconnectAttempts = 1;
3435

3536
/// <summary>
3637
/// This event fires when the client sent out a request to start the client, but failed to hear back after an allotted amount of
@@ -102,6 +103,20 @@ public void OnUserDisconnectRequest()
102103
StopCoroutine(m_TryToReconnectCoroutine);
103104
m_TryToReconnectCoroutine = null;
104105
}
106+
107+
if (!m_Portal.NetManager.IsConnectedClient)
108+
{
109+
// If we are here, it means we will not receive the OnClientDisconnectCallback since we are already disconnected.
110+
// In that case, publish the disconnect reason and clear it now.
111+
m_ConnectStatusPub.Publish(DisconnectReason.Reason);
112+
DisconnectReason.Clear();
113+
}
114+
115+
// only do it here if we are not the host. The host will do it in ServerGameNetPortal
116+
if (!m_Portal.NetManager.IsHost)
117+
{
118+
m_Portal.NetManager.Shutdown();
119+
}
105120
}
106121
}
107122

@@ -142,7 +157,7 @@ private void OnDisconnectOrTimeout(ulong clientID)
142157
//We have to check here in SceneManager if our active scene is the main menu, as if it is, it means we timed out rather than a raw disconnect;
143158
if (SceneManager.GetActiveScene().name != "MainMenu")
144159
{
145-
if (DisconnectReason.Reason == ConnectStatus.UserRequestedDisconnect || NetworkManager.Singleton.IsHost)
160+
if (DisconnectReason.Reason == ConnectStatus.UserRequestedDisconnect || DisconnectReason.Reason == ConnectStatus.HostDisconnected || NetworkManager.Singleton.IsHost)
146161
{
147162
// simply shut down and go back to main menu
148163
NetworkManager.Singleton.Shutdown();
@@ -172,27 +187,24 @@ private IEnumerator TryToReconnect()
172187
{
173188
Debug.Log("Lost connection to host, trying to reconnect...");
174189
int nbTries = 0;
175-
while (nbTries < 3)
190+
while (nbTries < k_NbReconnectAttempts)
176191
{
177192
NetworkManager.Singleton.Shutdown();
178193
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
179-
Debug.Log($"Reconnecting attempt {nbTries + 1}/3...");
194+
Debug.Log($"Reconnecting attempt {nbTries + 1}/{k_NbReconnectAttempts}...");
180195
ConnectClient(null);
181196
yield return new WaitForSeconds(1.1f * k_TimeoutDuration); // wait a bit longer than the timeout duration to make sure we have enough time to stop this coroutine if successful
182197
nbTries++;
183198
}
184199

185-
if (!NetworkManager.Singleton.IsConnectedClient)
200+
// If the coroutine has not been stopped before this, it means we failed to connect during all attempts
201+
Debug.Log("All tries failed, returning to main menu");
202+
NetworkManager.Singleton.Shutdown();
203+
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
204+
if (!DisconnectReason.HasTransitionReason)
186205
{
187-
Debug.Log("All tries failed, returning to main menu");
188-
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
189-
if (!DisconnectReason.HasTransitionReason)
190-
{
191-
DisconnectReason.SetDisconnectReason(ConnectStatus.GenericDisconnect);
192-
}
193-
206+
DisconnectReason.SetDisconnectReason(ConnectStatus.GenericDisconnect);
194207
}
195-
NetworkManager.Singleton.Shutdown();
196208
m_TryToReconnectCoroutine = null;
197209
m_ConnectStatusPub.Publish(DisconnectReason.Reason);
198210
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/GameNetPortal.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using Unity.Multiplayer.Samples.BossRoom.Client;
34
using Unity.Multiplayer.Samples.BossRoom.Server;
45
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
@@ -19,6 +20,7 @@ public enum ConnectStatus
1920
UserRequestedDisconnect, //Intentional Disconnect triggered by the user.
2021
GenericDisconnect, //server disconnected, but no specific reason given.
2122
Reconnecting, //client lost connection and is attempting to reconnect.
23+
HostDisconnected, //Intentional Disconnect from the host
2224
}
2325

2426
public enum OnlineMode
@@ -245,8 +247,6 @@ public void RequestDisconnect()
245247
}
246248
m_ClientPortal.OnUserDisconnectRequest();
247249
m_ServerPortal.OnUserDisconnectRequest();
248-
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
249-
NetManager.Shutdown();
250250
}
251251
}
252252
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public void OnClientSceneChanged(ulong clientId, int sceneIndex)
106106
public void OnUserDisconnectRequest()
107107
{
108108
Clear();
109+
if (m_Portal.NetManager.IsServer)
110+
{
111+
SendServerToAllClientsSetDisconnectReason(ConnectStatus.HostDisconnected);
112+
// wait before shutting down to make sure those messages get sent before the clients disconnect.
113+
StartCoroutine(WaitToShutdown());
114+
}
115+
}
116+
117+
IEnumerator WaitToShutdown()
118+
{
119+
yield return new WaitForSeconds(0.5f);
120+
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
121+
m_Portal.NetManager.Shutdown();
109122
}
110123

111124
private void Clear()
@@ -230,6 +243,16 @@ public void SendServerToClientSetDisconnectReason(ulong clientID, ConnectStatus
230243
writer.WriteValueSafe(status);
231244
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
232245
}
246+
/// <summary>
247+
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the clients.
248+
/// </summary>
249+
/// <param name="status"> The reason for the upcoming disconnect.</param>
250+
public void SendServerToAllClientsSetDisconnectReason(ConnectStatus status)
251+
{
252+
var writer = new FastBufferWriter(sizeof(ConnectStatus), Allocator.Temp);
253+
writer.WriteValueSafe(status);
254+
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessageToAll(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), writer);
255+
}
233256

234257
/// <summary>
235258
/// Responsible for the Server->Client custom message of the connection result.

0 commit comments

Comments
 (0)