Skip to content

Commit 725f17a

Browse files
committed
flow for client hosted works
1 parent 7ea034a commit 725f17a

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public class ConnectionPayload
5454
/// </summary>
5555
public class ConnectionManager : MonoBehaviour
5656
{
57+
public enum ServerType : byte
58+
{
59+
Undefined = 0,
60+
DedicatedServer,
61+
ClientHostedServer
62+
}
63+
5764
ConnectionState m_CurrentState;
5865

5966
[SerializeField]
@@ -79,7 +86,7 @@ public class ConnectionManager : MonoBehaviour
7986
internal readonly StartingHostState m_StartingHost = new StartingHostState();
8087
internal readonly HostingState m_Hosting = new HostingState();
8188

82-
public bool IsConnectedToHost { get; private set; }
89+
public ServerType IsConnectedToHost { get; set; }
8390

8491
void Awake()
8592
{
@@ -88,7 +95,7 @@ void Awake()
8895

8996
void Start()
9097
{
91-
List<ConnectionState> states = new() { m_Offline, m_ClientConnecting, m_ClientConnected, m_ClientReconnecting, m_DisconnectingWithReason, m_StartingHost, m_Hosting };
98+
List<ConnectionState> states = new() { m_Offline, m_ClientConnecting, m_ClientConnected, m_ClientReconnecting, m_DisconnectingWithReason, m_StartingHost, m_Hosting, m_ServerListening, m_ServerStarting };
9299
foreach (var connectionState in states)
93100
{
94101
m_Resolver.Inject(connectionState);
@@ -114,8 +121,6 @@ internal void ChangeState(ConnectionState nextState)
114121
{
115122
Debug.Log($"Changed connection state from {m_CurrentState.GetType().Name} to {nextState.GetType().Name}.");
116123

117-
IsConnectedToHost = false;
118-
119124
if (m_CurrentState != null)
120125
{
121126
m_CurrentState.Exit();
@@ -215,13 +220,13 @@ public static void SendServerToClientSetDisconnectReason(ulong clientID, Connect
215220
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(nameof(ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
216221
}
217222

218-
public void ReceiveServertoClientSuccessPayload_CustomMessage(ulong clientID, FastBufferReader reader)
223+
internal void ReceiveServertoClientSuccessPayload_CustomMessage(ulong clientID, FastBufferReader reader)
219224
{
220-
reader.ReadValueSafe(out bool isHost);
225+
reader.ReadValueSafe(out ServerType isHost);
221226
IsConnectedToHost = isHost;
222227
}
223228

224-
public static void SendServertoClientSuccessPayload(ulong clientID, bool isHost)
229+
internal static void SendServertoClientSuccessPayload(ulong clientID, ServerType isHost)
225230
{
226231
var writer = new FastBufferWriter(sizeof(bool), Allocator.Temp);
227232
writer.WriteValueSafe(isHost);

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectedState.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class ClientConnectedState : ConnectionState
1010
{
1111
public override void Enter() { }
1212

13-
public override void Exit() { }
13+
public override void Exit()
14+
{
15+
m_ConnectionManager.IsConnectedToHost = ConnectionManager.ServerType.Undefined;
16+
}
1417

1518
public override void OnClientDisconnect(ulong _)
1619
{

Assets/Scripts/ConnectionManagement/ConnectionState/ServerListeningState.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ namespace Unity.Multiplayer.Samples.BossRoom
1111
{
1212
class ServerListeningState : ConnectionState
1313
{
14-
[Inject]
15-
LobbyServiceFacade m_LobbyServiceFacade;
1614
[Inject]
17-
IPublisher<ConnectionEventMessage> m_ConnectionEventPublisher;
15+
protected LobbyServiceFacade m_LobbyServiceFacade;
16+
17+
[Inject]
18+
protected IPublisher<ConnectionEventMessage> m_ConnectionEventPublisher;
1819

1920
// used in ApprovalCheck. This is intended as a bit of light protection against DOS attacks that rely on sending silly big buffers of garbage.
2021
const int k_MaxConnectPayload = 1024;
@@ -101,7 +102,7 @@ public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest requ
101102
response.Position = Vector3.zero;
102103
response.Rotation = Quaternion.identity;
103104

104-
ConnectionManager.SendServertoClientSuccessPayload(clientId, NetworkManager.Singleton.IsHost);
105+
ConnectionManager.SendServertoClientSuccessPayload(clientId, NetworkManager.Singleton.IsHost ? ConnectionManager.ServerType.ClientHostedServer : ConnectionManager.ServerType.DedicatedServer);
105106

106107
return;
107108
}

Assets/Scripts/Gameplay/UI/PostGameUI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Unity.Multiplayer.Samples.Utilities;
66
using Unity.Netcode;
77
using VContainer;
8+
using static Unity.Multiplayer.Samples.BossRoom.ConnectionManager.ServerType;
89

910
namespace Unity.Multiplayer.Samples.BossRoom.Visual
1011
{
@@ -63,7 +64,7 @@ void Start()
6364
SetPostGameUI(networkGameState.NetworkWinState.winState.Value);
6465
}
6566

66-
if (!m_ConnectionManager.IsConnectedToHost && !NetworkManager.Singleton.IsServer)
67+
if (m_ConnectionManager.IsConnectedToHost == DedicatedServer && !NetworkManager.Singleton.IsServer)
6768
{
6869
IEnumerator CountdownToNextGame()
6970
{

0 commit comments

Comments
 (0)