Skip to content

Commit d32ff9a

Browse files
committed
Revert "Moving ConnectionMethod responsibility to ConnectionManager"
This reverts commit d5095f3.
1 parent d5095f3 commit d32ff9a

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

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

7170
[Inject]
7271
IObjectResolver m_Resolver;
73-
[Inject]
74-
LobbyServiceFacade m_LobbyServiceFacade;
75-
[Inject]
76-
ProfileManager m_ProfileManager;
77-
[Inject]
78-
LocalLobby m_LocalLobby;
7972

8073
public int MaxConnectedPlayers = 8;
8174

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

89-
ConnectionMethodBase m_ConnectionMethod;
90-
public ConnectionMethodBase ConnectionMethod => m_ConnectionMethod;
91-
9282
void Awake()
9383
{
9484
DontDestroyOnLoad(gameObject);
@@ -159,26 +149,22 @@ void OnTransportFailure()
159149

160150
public void StartClientLobby(string playerName)
161151
{
162-
m_ConnectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, this, m_ProfileManager, playerName);
163-
m_CurrentState.StartClient();
152+
m_CurrentState.StartClientLobby(playerName);
164153
}
165154

166155
public void StartClientIp(string playerName, string ipaddress, int port)
167156
{
168-
m_ConnectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, this, m_ProfileManager, playerName);
169-
m_CurrentState.StartClient();
157+
m_CurrentState.StartClientIP(playerName, ipaddress, port);
170158
}
171159

172160
public void StartHostLobby(string playerName)
173161
{
174-
m_ConnectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, this, m_ProfileManager, playerName);
175-
m_CurrentState.StartHost();
162+
m_CurrentState.StartHostLobby(playerName);
176163
}
177164

178165
public void StartHostIp(string playerName, string ipaddress, int port)
179166
{
180-
m_ConnectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, this, m_ProfileManager, playerName);
181-
m_CurrentState.StartHost();
167+
m_CurrentState.StartHostIP(playerName, ipaddress, port);
182168
}
183169

184170
public void RequestShutdown()

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ 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+
1422
public override void Enter()
1523
{
1624
#pragma warning disable 4014
@@ -53,7 +61,7 @@ internal async Task ConnectClientAsync()
5361
try
5462
{
5563
// Setup NGO with current connection method
56-
await m_ConnectionManager.ConnectionMethod.SetupClientConnectionAsync();
64+
await m_ConnectionMethod.SetupClientConnectionAsync();
5765

5866
// NGO's StartClient launches everything
5967
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_ConnectionManager.ConnectionMethod.SetupClientReconnectionAsync();
109+
var reconnectingSetupTask = m_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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ public virtual void OnClientDisconnect(ulong clientId) { }
2626

2727
public virtual void OnServerStarted() { }
2828

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

31-
public virtual void StartHost() { }
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) { }
3236

3337
public virtual void OnUserRequestedShutdown() { }
3438

Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs

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

2025
const string k_MainMenuSceneName = "MainMenu";
2126

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

3237
public override void Exit() { }
3338

34-
public override void StartClient()
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)
3554
{
36-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting);
55+
var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName);
56+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod));
3757
}
3858

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

Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ 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+
}
2128

2229
public override void Enter()
2330
{
@@ -69,7 +76,7 @@ async void StartHost()
6976
{
7077
try
7178
{
72-
await m_ConnectionManager.ConnectionMethod.SetupHostConnectionAsync();
79+
await m_ConnectionMethod.SetupHostConnectionAsync();
7380
Debug.Log($"Created relay allocation with join code {m_LocalLobby.RelayJoinCode}");
7481

7582
// NGO's StartHost launches everything

0 commit comments

Comments
 (0)