Skip to content

Commit d5095f3

Browse files
committed
Moving ConnectionMethod responsibility to ConnectionManager
1 parent f53dd25 commit d5095f3

File tree

6 files changed

+27
-53
lines changed

6 files changed

+27
-53
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Unity.BossRoom.UnityServices.Lobbies;
34
using Unity.BossRoom.Utils;
45
using Unity.Collections;
56
using Unity.Netcode;
@@ -69,6 +70,12 @@ public class ConnectionManager : MonoBehaviour
6970

7071
[Inject]
7172
IObjectResolver m_Resolver;
73+
[Inject]
74+
LobbyServiceFacade m_LobbyServiceFacade;
75+
[Inject]
76+
ProfileManager m_ProfileManager;
77+
[Inject]
78+
LocalLobby m_LocalLobby;
7279

7380
public int MaxConnectedPlayers = 8;
7481

@@ -79,6 +86,9 @@ public class ConnectionManager : MonoBehaviour
7986
internal readonly StartingHostState m_StartingHost = new StartingHostState();
8087
internal readonly HostingState m_Hosting = new HostingState();
8188

89+
ConnectionMethodBase m_ConnectionMethod;
90+
public ConnectionMethodBase ConnectionMethod => m_ConnectionMethod;
91+
8292
void Awake()
8393
{
8494
DontDestroyOnLoad(gameObject);
@@ -149,22 +159,26 @@ void OnTransportFailure()
149159

150160
public void StartClientLobby(string playerName)
151161
{
152-
m_CurrentState.StartClientLobby(playerName);
162+
m_ConnectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, this, m_ProfileManager, playerName);
163+
m_CurrentState.StartClient();
153164
}
154165

155166
public void StartClientIp(string playerName, string ipaddress, int port)
156167
{
157-
m_CurrentState.StartClientIP(playerName, ipaddress, port);
168+
m_ConnectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, this, m_ProfileManager, playerName);
169+
m_CurrentState.StartClient();
158170
}
159171

160172
public void StartHostLobby(string playerName)
161173
{
162-
m_CurrentState.StartHostLobby(playerName);
174+
m_ConnectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, this, m_ProfileManager, playerName);
175+
m_CurrentState.StartHost();
163176
}
164177

165178
public void StartHostIp(string playerName, string ipaddress, int port)
166179
{
167-
m_CurrentState.StartHostIP(playerName, ipaddress, port);
180+
m_ConnectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, this, m_ProfileManager, playerName);
181+
m_CurrentState.StartHost();
168182
}
169183

170184
public void RequestShutdown()

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ namespace Unity.BossRoom.ConnectionManagement
1111
/// </summary>
1212
class ClientConnectingState : OnlineState
1313
{
14-
protected ConnectionMethodBase m_ConnectionMethod;
15-
16-
public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod)
17-
{
18-
m_ConnectionMethod = baseConnectionMethod;
19-
return this;
20-
}
21-
2214
public override void Enter()
2315
{
2416
#pragma warning disable 4014
@@ -61,7 +53,7 @@ internal async Task ConnectClientAsync()
6153
try
6254
{
6355
// Setup NGO with current connection method
64-
await m_ConnectionMethod.SetupClientConnectionAsync();
56+
await m_ConnectionManager.ConnectionMethod.SetupClientConnectionAsync();
6557

6658
// NGO's StartClient launches everything
6759
if (!m_ConnectionManager.NetworkManager.StartClient())

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ IEnumerator ReconnectCoroutine()
106106
Debug.Log($"Reconnecting attempt {m_NbAttempts + 1}/{m_ConnectionManager.NbReconnectAttempts}...");
107107
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(m_NbAttempts, m_ConnectionManager.NbReconnectAttempts));
108108
m_NbAttempts++;
109-
var reconnectingSetupTask = m_ConnectionMethod.SetupClientReconnectionAsync();
109+
var reconnectingSetupTask = m_ConnectionManager.ConnectionMethod.SetupClientReconnectionAsync();
110110
yield return new WaitUntil(() => reconnectingSetupTask.IsCompleted);
111111

112112
if (!reconnectingSetupTask.IsFaulted && reconnectingSetupTask.Result.success)

Assets/Scripts/ConnectionManagement/ConnectionState/ConnectionState.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ public virtual void OnClientDisconnect(ulong clientId) { }
2626

2727
public virtual void OnServerStarted() { }
2828

29-
public virtual void StartClientIP(string playerName, string ipaddress, int port) { }
29+
public virtual void StartClient() { }
3030

31-
public virtual void StartClientLobby(string playerName) { }
32-
33-
public virtual void StartHostIP(string playerName, string ipaddress, int port) { }
34-
35-
public virtual void StartHostLobby(string playerName) { }
31+
public virtual void StartHost() { }
3632

3733
public virtual void OnUserRequestedShutdown() { }
3834

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Unity.BossRoom.ConnectionManagement;
33
using Unity.BossRoom.UnityServices.Lobbies;
4-
using Unity.BossRoom.Utils;
54
using Unity.Multiplayer.Samples.Utilities;
65
using UnityEngine;
76
using UnityEngine.SceneManagement;
@@ -17,10 +16,6 @@ class OfflineState : ConnectionState
1716
{
1817
[Inject]
1918
LobbyServiceFacade m_LobbyServiceFacade;
20-
[Inject]
21-
ProfileManager m_ProfileManager;
22-
[Inject]
23-
LocalLobby m_LocalLobby;
2419

2520
const string k_MainMenuSceneName = "MainMenu";
2621

@@ -36,30 +31,14 @@ public override void Enter()
3631

3732
public override void Exit() { }
3833

39-
public override void StartClientIP(string playerName, string ipaddress, int port)
40-
{
41-
var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName);
42-
m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod);
43-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod));
44-
}
45-
46-
public override void StartClientLobby(string playerName)
47-
{
48-
var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName);
49-
m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod);
50-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod));
51-
}
52-
53-
public override void StartHostIP(string playerName, string ipaddress, int port)
34+
public override void StartClient()
5435
{
55-
var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName);
56-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod));
36+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting);
5737
}
5838

59-
public override void StartHostLobby(string playerName)
39+
public override void StartHost()
6040
{
61-
var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName);
62-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod));
41+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost);
6342
}
6443
}
6544
}

Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ class StartingHostState : OnlineState
1818
LobbyServiceFacade m_LobbyServiceFacade;
1919
[Inject]
2020
LocalLobby m_LocalLobby;
21-
ConnectionMethodBase m_ConnectionMethod;
22-
23-
public StartingHostState Configure(ConnectionMethodBase baseConnectionMethod)
24-
{
25-
m_ConnectionMethod = baseConnectionMethod;
26-
return this;
27-
}
2821

2922
public override void Enter()
3023
{
@@ -76,7 +69,7 @@ async void StartHost()
7669
{
7770
try
7871
{
79-
await m_ConnectionMethod.SetupHostConnectionAsync();
72+
await m_ConnectionManager.ConnectionMethod.SetupHostConnectionAsync();
8073
Debug.Log($"Created relay allocation with join code {m_LocalLobby.RelayJoinCode}");
8174

8275
// NGO's StartHost launches everything

0 commit comments

Comments
 (0)