Skip to content

Commit 977e800

Browse files
feat: message to clients when host ends game session intentionally [MTT-3202] (#594)
* Adding message for when host ends session voluntarily * Also makes sure that RequestDisconnect is only called when a disconnect is actually requested
1 parent fdfaaa9 commit 977e800

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void OnConnectStatus(ConnectStatus status)
4848
case ConnectStatus.GenericDisconnect:
4949
PopupManager.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost.");
5050
break;
51+
case ConnectStatus.HostEndedSession:
52+
PopupManager.ShowPopupPanel("Disconnected From Host", "The host has ended the game session.");
53+
break;
5154
default:
5255
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
5356
break;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Quit()
3030
switch (m_QuitMode)
3131
{
3232
case QuitMode.ReturnToMenu:
33-
m_ApplicationController.LeaveSession();
33+
m_ApplicationController.LeaveSession(true);
3434
break;
3535
case QuitMode.QuitApplication:
3636
m_ApplicationController.QuitGame();

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ private bool OnWantToQuit()
113113
return canQuit;
114114
}
115115

116-
public void LeaveSession()
116+
public void LeaveSession(bool UserRequested)
117117
{
118118
m_LobbyServiceFacade.EndTracking();
119119

120-
// first disconnect then return to menu
121-
var gameNetPortal = GameNetPortal.Instance;
122-
if (gameNetPortal != null)
120+
if (UserRequested)
123121
{
124-
gameNetPortal.RequestDisconnect();
122+
// first disconnect then return to menu
123+
var gameNetPortal = GameNetPortal.Instance;
124+
if (gameNetPortal != null)
125+
{
126+
gameNetPortal.RequestDisconnect();
127+
}
125128
}
126129
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
127130
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public void OnUserDisconnectRequest()
9797
if (m_Portal.NetManager.IsClient)
9898
{
9999
DisconnectReason.SetDisconnectReason(ConnectStatus.UserRequestedDisconnect);
100+
// If we are the server, shutdown will be handled by ServerGameNetPortal
101+
if (!m_Portal.NetManager.IsServer)
102+
{
103+
m_Portal.NetManager.Shutdown();
104+
}
100105
}
101106
}
102107

@@ -132,12 +137,6 @@ private void OnDisconnectOrTimeout(ulong clientID)
132137
// not a host (in which case we know this is about us) or that the clientID is the same as ours if we are the host.
133138
if (!NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsHost && NetworkManager.Singleton.LocalClientId == clientID)
134139
{
135-
var lobbyCode = "";
136-
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
137-
{
138-
lobbyCode = m_LobbyServiceFacade.CurrentUnityLobby.LobbyCode;
139-
}
140-
141140
//On a client disconnect we want to take them back to the main menu.
142141
//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;
143142
if (SceneManager.GetActiveScene().name != "MainMenu")
@@ -149,7 +148,7 @@ private void OnDisconnectOrTimeout(ulong clientID)
149148
//disconnect that happened for some other reason than user UI interaction--should display a message.
150149
DisconnectReason.SetDisconnectReason(ConnectStatus.GenericDisconnect);
151150
}
152-
m_ApplicationController.LeaveSession();
151+
m_ApplicationController.LeaveSession(false);
153152
}
154153
else
155154
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public enum ConnectStatus
2121
LoggedInAgain, //logged in on a separate client, causing this one to be kicked out.
2222
UserRequestedDisconnect, //Intentional Disconnect triggered by the user.
2323
GenericDisconnect, //server disconnected, but no specific reason given.
24-
IncompatibleBuildType, //client build type is incompatible with server.
24+
IncompatibleBuildType, //client build type is incompatible with server.
25+
HostEndedSession, //host intentionally ended the session.
2526
}
2627

2728
public enum OnlineMode
@@ -244,7 +245,6 @@ public void RequestDisconnect()
244245
}
245246
m_ClientPortal.OnUserDisconnectRequest();
246247
m_ServerPortal.OnUserDisconnectRequest();
247-
NetManager.Shutdown();
248248
}
249249

250250
public string GetPlayerId()

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public void OnClientSceneChanged(ulong clientId, int sceneIndex)
126126
/// </summary>
127127
public void OnUserDisconnectRequest()
128128
{
129+
if (m_Portal.NetManager.IsHost)
130+
{
131+
SendServerToAllClientsSetDisconnectReason(ConnectStatus.HostEndedSession);
132+
// Wait before shutting down to make sure clients receive that message before they are disconnected
133+
StartCoroutine(WaitToShutdown());
134+
}
129135
Clear();
130136
}
131137

@@ -233,6 +239,24 @@ static IEnumerator WaitToDenyApproval(NetworkManager.ConnectionApprovedDelegate
233239
connectionApprovedCallback(false, 0, false, null, null);
234240
}
235241

242+
IEnumerator WaitToShutdown()
243+
{
244+
yield return null;
245+
m_Portal.NetManager.Shutdown();
246+
SessionManager<SessionPlayerData>.Instance.OnServerEnded();
247+
}
248+
249+
/// <summary>
250+
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the client.
251+
/// </summary>
252+
/// <param name="status"> The reason for the upcoming disconnect.</param>
253+
static void SendServerToAllClientsSetDisconnectReason(ConnectStatus status)
254+
{
255+
var writer = new FastBufferWriter(sizeof(ConnectStatus), Allocator.Temp);
256+
writer.WriteValueSafe(status);
257+
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessageToAll(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), writer);
258+
}
259+
236260
/// <summary>
237261
/// Sends a DisconnectReason to the indicated client. This should only be done on the server, prior to disconnecting the client.
238262
/// </summary>

0 commit comments

Comments
 (0)