Skip to content

Commit 3193cce

Browse files
committed
handling when hosts disconnect voluntarily on clients
1 parent 369cf43 commit 3193cce

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
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/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 4 additions & 3 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 = 3;
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
@@ -150,7 +151,7 @@ private void OnDisconnectOrTimeout(ulong clientID)
150151
//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;
151152
if (SceneManager.GetActiveScene().name != "MainMenu")
152153
{
153-
if (DisconnectReason.Reason == ConnectStatus.UserRequestedDisconnect || NetworkManager.Singleton.IsHost)
154+
if (DisconnectReason.Reason == ConnectStatus.UserRequestedDisconnect || DisconnectReason.Reason == ConnectStatus.HostDisconnected || NetworkManager.Singleton.IsHost)
154155
{
155156
// simply shut down and go back to main menu
156157
NetworkManager.Singleton.Shutdown();
@@ -180,11 +181,11 @@ private IEnumerator TryToReconnect()
180181
{
181182
Debug.Log("Lost connection to host, trying to reconnect...");
182183
int nbTries = 0;
183-
while (nbTries < 3)
184+
while (nbTries < k_NbReconnectAttempts)
184185
{
185186
NetworkManager.Singleton.Shutdown();
186187
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
187-
Debug.Log($"Reconnecting attempt {nbTries + 1}/3...");
188+
Debug.Log($"Reconnecting attempt {nbTries + 1}/{k_NbReconnectAttempts}...");
188189
ConnectClient(null);
189190
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
190191
nbTries++;

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

Lines changed: 16 additions & 0 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
@@ -246,6 +248,20 @@ public void RequestDisconnect()
246248
m_ClientPortal.OnUserDisconnectRequest();
247249
m_ServerPortal.OnUserDisconnectRequest();
248250
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
251+
252+
if (NetManager.IsServer)
253+
{
254+
StartCoroutine(WaitToShutdown());
255+
}
256+
else
257+
{
258+
NetManager.Shutdown();
259+
}
260+
}
261+
262+
IEnumerator WaitToShutdown()
263+
{
264+
yield return new WaitForSeconds(0.5f);
249265
NetManager.Shutdown();
250266
}
251267
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ 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+
}
109113
}
110114

111115
private void Clear()
@@ -230,6 +234,16 @@ public void SendServerToClientSetDisconnectReason(ulong clientID, ConnectStatus
230234
writer.WriteValueSafe(status);
231235
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
232236
}
237+
/// <summary>
238+
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the clients.
239+
/// </summary>
240+
/// <param name="status"> The reason for the upcoming disconnect.</param>
241+
public void SendServerToAllClientsSetDisconnectReason(ConnectStatus status)
242+
{
243+
var writer = new FastBufferWriter(sizeof(ConnectStatus), Allocator.Temp);
244+
writer.WriteValueSafe(status);
245+
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessageToAll(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), writer);
246+
}
233247

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

0 commit comments

Comments
 (0)