From 6f0561818b7be526146c82467b57872c4b81cd78 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Tue, 26 Jul 2022 14:37:15 -0400 Subject: [PATCH 01/21] After this PR https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/pull/2081 reverting MaxPayloadSize to default --- Assets/Prefabs/NetworkingManager.prefab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Prefabs/NetworkingManager.prefab b/Assets/Prefabs/NetworkingManager.prefab index d6e4be8c1..8de0fbe47 100644 --- a/Assets/Prefabs/NetworkingManager.prefab +++ b/Assets/Prefabs/NetworkingManager.prefab @@ -46,7 +46,7 @@ MonoBehaviour: m_EditorClassIdentifier: m_ProtocolType: 0 m_MaxPacketQueueSize: 512 - m_MaxPayloadSize: 32000 + m_MaxPayloadSize: 6144 m_MaxSendQueueSize: 50000000 m_HeartbeatTimeoutMS: 500 m_ConnectTimeoutMS: 1000 From 2a69af74537c980aa0377db3253a6aeb0d81398d Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Thu, 13 Oct 2022 17:18:24 -0400 Subject: [PATCH 02/21] basis works, still need to debug issue with client reconnecting a second time --- Assets/Scripts.meta | 8 - .../ConnectionManagement/ConnectionMethod.cs | 158 ++++++++++++++++++ .../ConnectionMethod.cs.meta | 3 + .../ConnectionState/ClientConnectingState.cs | 87 +++++----- .../ConnectionState/OfflineState.cs | 44 +---- .../ConnectionState/OnlineState.cs | 1 + .../ConnectionState/StartingHostState.cs | 56 +++---- ...Unity.BossRoom.ConnectionManagement.asmdef | 5 +- .../Utilities/Net/UnityRelayUtilities.cs | 69 -------- .../Utilities/Net/UnityRelayUtilities.cs.meta | 3 - 10 files changed, 237 insertions(+), 197 deletions(-) delete mode 100644 Assets/Scripts.meta create mode 100644 Assets/Scripts/ConnectionManagement/ConnectionMethod.cs create mode 100644 Assets/Scripts/ConnectionManagement/ConnectionMethod.cs.meta delete mode 100644 Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs delete mode 100644 Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta diff --git a/Assets/Scripts.meta b/Assets/Scripts.meta deleted file mode 100644 index a24a7e7bc..000000000 --- a/Assets/Scripts.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8e8389b584b3f314a916cbb2dcfe78a9 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs new file mode 100644 index 000000000..1d817b510 --- /dev/null +++ b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs @@ -0,0 +1,158 @@ +using System; +using System.Threading.Tasks; +using Unity.BossRoom.ConnectionManagement; +using Unity.BossRoom.UnityServices.Lobbies; +using Unity.BossRoom.Utils; +using Unity.Netcode.Transports.UTP; +using Unity.Networking.Transport.Relay; +using Unity.Services.Authentication; +using Unity.Services.Core; +using Unity.Services.Relay; +using Unity.Services.Relay.Models; +using UnityEngine; + +namespace ConnectionManagement.ConnectionState +{ + public abstract class ConnectionMethodBase + { + protected ConnectionManager m_ConnectionManager; + readonly ProfileManager m_ProfileManager; + readonly string m_PlayerName; + + public virtual async Task SetupHostConnectionAsync() + { + // Since hosts are clients too, we need to set their payload as well + SetConnectionPayload(GetPlayerId(), m_PlayerName); + // This method is virtual, see child classes for further implementation + } + + public virtual async Task SetupClientConnectionAsync() + { + SetConnectionPayload(GetPlayerId(), m_PlayerName); + // This method is virtual, see child classes for further implementation + } + + public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName) + { + m_ConnectionManager = connectionManager; + m_ProfileManager = profileManager; + m_PlayerName = playerName; + } + + void SetConnectionPayload(string playerId, string playerName) + { + var payload = JsonUtility.ToJson(new ConnectionPayload() + { + playerId = playerId, + playerName = playerName, + isDebug = Debug.isDebugBuild + }); + + var payloadBytes = System.Text.Encoding.UTF8.GetBytes(payload); + + m_ConnectionManager.NetworkManager.NetworkConfig.ConnectionData = payloadBytes; + } + + string GetPlayerId() + { + if (UnityServices.State != ServicesInitializationState.Initialized) + { + return ClientPrefs.GetGuid() + m_ProfileManager.Profile; + } + + return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + m_ProfileManager.Profile; + } + } + + class ConnectionMethodIP : ConnectionMethodBase + { + string m_Ipaddress; + ushort m_Port; + + public ConnectionMethodIP(string ip, ushort port, ConnectionManager connectionManager, ProfileManager profileManager, string playerName) + : base(connectionManager, profileManager, playerName) + { + m_Ipaddress = ip; + m_Port = port; + m_ConnectionManager = connectionManager; + } + + public override async Task SetupClientConnectionAsync() + { + await base.SetupClientConnectionAsync(); + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; + utp.SetConnectionData(m_Ipaddress, m_Port); + } + + public override async Task SetupHostConnectionAsync() + { + await base.SetupHostConnectionAsync(); + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; + utp.SetConnectionData(m_Ipaddress, m_Port); + } + } + + class ConnectionMethodRelay : ConnectionMethodBase + { + LobbyServiceFacade m_LobbyServiceFacade; + LocalLobby m_LocalLobby; + + public ConnectionMethodRelay(LobbyServiceFacade lobbyServiceFacade, LocalLobby localLobby, ConnectionManager connectionManager, ProfileManager profileManager, string playerName) + : base(connectionManager, profileManager, playerName) + { + m_LobbyServiceFacade = lobbyServiceFacade; + m_LocalLobby = localLobby; + m_ConnectionManager = connectionManager; + } + + public override async Task SetupClientConnectionAsync() + { + Debug.Log("Setting up Unity Relay client"); + + await base.SetupClientConnectionAsync(); + + if (m_LobbyServiceFacade.CurrentUnityLobby == null) + { + throw new Exception("Trying to start relay while Lobby isn't set"); + } + + Debug.Log($"Setting Unity Relay client with join code {m_LocalLobby.RelayJoinCode}"); + + // Create client joining allocation from join code + var joinedAllocation = await RelayService.Instance.JoinAllocationAsync(m_LocalLobby.RelayJoinCode); + Debug.Log($"client: {joinedAllocation.ConnectionData[0]} {joinedAllocation.ConnectionData[1]}, " + + $"host: {joinedAllocation.HostConnectionData[0]} {joinedAllocation.HostConnectionData[1]}, " + + $"client: {joinedAllocation.AllocationId}"); + + await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(joinedAllocation.AllocationId.ToString(), m_LocalLobby.RelayJoinCode); + + // Configure UTP with allocation + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; + utp.SetRelayServerData(new RelayServerData(joinedAllocation, OnlineState.k_DtlsConnType)); + } + + public override async Task SetupHostConnectionAsync() + { + Debug.Log("Setting up Unity Relay host"); + + await base.SetupHostConnectionAsync(); + + // Create relay allocation + Allocation hostAllocation = await RelayService.Instance.CreateAllocationAsync(m_ConnectionManager.MaxConnectedPlayers, region: null); + var joinCode = await RelayService.Instance.GetJoinCodeAsync(hostAllocation.AllocationId); + + Debug.Log($"server: connection data: {hostAllocation.ConnectionData[0]} {hostAllocation.ConnectionData[1]}, " + + $"allocation ID:{hostAllocation.AllocationId}, region:{hostAllocation.Region}"); + + m_LocalLobby.RelayJoinCode = joinCode; + + //next line enable lobby and relay services integration + await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices()); + await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(hostAllocation.AllocationIdBytes.ToString(), joinCode); + + // Setup UTP with relay connection info + var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; + utp.SetRelayServerData(new RelayServerData(hostAllocation, OnlineState.k_DtlsConnType)); // This is with DTLS enabled for a secure connection + } + } +} diff --git a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs.meta b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs.meta new file mode 100644 index 000000000..364fe4576 --- /dev/null +++ b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e414e30101f84433b92459054c280675 +timeCreated: 1665689296 \ No newline at end of file diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index 638a3d719..57f77d53a 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -1,9 +1,12 @@ using System; using System.Threading.Tasks; +using ConnectionManagement.ConnectionState; using Unity.BossRoom.UnityServices.Lobbies; using Unity.Multiplayer.Samples.BossRoom; using Unity.Multiplayer.Samples.Utilities; using Unity.Netcode.Transports.UTP; +using Unity.Networking.Transport.Relay; +using Unity.Services.Relay; using UnityEngine; using VContainer; @@ -20,6 +23,36 @@ class ClientConnectingState : OnlineState protected LobbyServiceFacade m_LobbyServiceFacade; [Inject] protected LocalLobby m_LocalLobby; + ConnectionMethodBase m_ConnectionMethod; + + public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod) + { + m_ConnectionMethod = baseConnectionMethod; + return this; + } + + internal async Task ConnectClientAsync() + { + try + { + // Setup NGO with current connection method + await m_ConnectionMethod.SetupClientConnectionAsync(); + + // NGO's StartClient launches everything + if (!m_ConnectionManager.NetworkManager.StartClient()) + { + throw new Exception("NetworkManager StartClient failed"); + } + + SceneLoaderWrapper.Instance.AddOnSceneEventCallback(); + m_ConnectionManager.RegisterCustomMessages(); + } + catch (Exception) + { + StartingClientFailedAsync(); + throw; + } + } public override void Enter() { @@ -37,6 +70,12 @@ public override void OnClientConnected(ulong _) } public override void OnClientDisconnect(ulong _) + { + // client ID is necessarily ours here + StartingClientFailedAsync(); + } + + protected void StartingClientFailedAsync() { m_ConnectStatusPublisher.Publish(ConnectStatus.StartClientFailed); m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline); @@ -47,53 +86,5 @@ public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason) m_ConnectStatusPublisher.Publish(disconnectReason); m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason); } - - protected async Task ConnectClientAsync() - { - bool success = true; - if (m_LobbyServiceFacade.CurrentUnityLobby != null) - { - success = await JoinRelayServerAsync(); - } - - if (success) - { - success = m_ConnectionManager.NetworkManager.StartClient(); - } - - if (success) - { - SceneLoaderWrapper.Instance.AddOnSceneEventCallback(); - m_ConnectionManager.RegisterCustomMessages(); - } - else - { - OnClientDisconnect(0); - } - } - - async Task JoinRelayServerAsync() - { - Debug.Log($"Setting Unity Relay client with join code {m_LocalLobby.RelayJoinCode}"); - - try - { - var (ipv4Address, port, allocationIdBytes, allocationId, connectionData, hostConnectionData, key) = - await UnityRelayUtilities.JoinRelayServerFromJoinCode(m_LocalLobby.RelayJoinCode); - - await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationId.ToString(), m_LocalLobby.RelayJoinCode); - var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; - utp.SetClientRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, hostConnectionData, isSecure: true); - } - catch (Exception e) - { - Debug.Log($"Relay join failed: {e.Message}"); - //leave the lobby if relay failed for some reason - await m_LobbyServiceFacade.EndTracking(); - return false; - } - - return true; - } } } diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs index 3c36166ca..fd655ae91 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs @@ -1,4 +1,5 @@ using System; +using ConnectionManagement.ConnectionState; using Unity.BossRoom.ConnectionManagement; using Unity.BossRoom.UnityServices.Lobbies; using Unity.BossRoom.Utils; @@ -22,6 +23,8 @@ class OfflineState : ConnectionState LobbyServiceFacade m_LobbyServiceFacade; [Inject] ProfileManager m_ProfileManager; + [Inject] + LocalLobby m_LocalLobby; const string k_MainMenuSceneName = "MainMenu"; @@ -39,55 +42,22 @@ public override void Exit() { } public override void StartClientIP(string playerName, string ipaddress, int port) { - var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; - utp.SetConnectionData(ipaddress, (ushort)port); - SetConnectionPayload(GetPlayerId(), playerName); - m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName))); } public override void StartClientLobby(string playerName) { - SetConnectionPayload(GetPlayerId(), playerName); - m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName))); } public override void StartHostIP(string playerName, string ipaddress, int port) { - var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; - utp.SetConnectionData(ipaddress, (ushort)port); - - SetConnectionPayload(GetPlayerId(), playerName); - m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName))); } public override void StartHostLobby(string playerName) { - SetConnectionPayload(GetPlayerId(), playerName); - m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost); - } - - void SetConnectionPayload(string playerId, string playerName) - { - var payload = JsonUtility.ToJson(new ConnectionPayload() - { - playerId = playerId, - playerName = playerName, - isDebug = Debug.isDebugBuild - }); - - var payloadBytes = System.Text.Encoding.UTF8.GetBytes(payload); - - m_ConnectionManager.NetworkManager.NetworkConfig.ConnectionData = payloadBytes; - } - - string GetPlayerId() - { - if (UnityServices.State != ServicesInitializationState.Initialized) - { - return ClientPrefs.GetGuid() + m_ProfileManager.Profile; - } - - return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + m_ProfileManager.Profile; + m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName))); } } } diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/OnlineState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/OnlineState.cs index 4b4ba7e89..89cf92c71 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/OnlineState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/OnlineState.cs @@ -5,6 +5,7 @@ namespace Unity.BossRoom.ConnectionManagement /// abstract class OnlineState : ConnectionState { + public const string k_DtlsConnType = "dtls"; public override void OnUserRequestedShutdown() { diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs index 774c00a3a..7065f6c1d 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs @@ -1,9 +1,13 @@ using System; +using ConnectionManagement.ConnectionState; using Unity.BossRoom.Infrastructure; using Unity.BossRoom.UnityServices.Lobbies; using Unity.Multiplayer.Samples.BossRoom; using Unity.Netcode; using Unity.Netcode.Transports.UTP; +using Unity.Networking.Transport.Relay; +using Unity.Services.Relay; +using Unity.Services.Relay.Models; using UnityEngine; using VContainer; @@ -19,11 +23,13 @@ class StartingHostState : OnlineState LobbyServiceFacade m_LobbyServiceFacade; [Inject] LocalLobby m_LocalLobby; + ConnectionMethodBase m_ConnectionMethod; - /// - /// How many connections we create a Unity relay allocation for - /// - const int k_MaxUnityRelayConnections = 8; + public StartingHostState Configure(ConnectionMethodBase baseConnectionMethod) + { + m_ConnectionMethod = baseConnectionMethod; + return this; + } public override void Enter() { @@ -36,11 +42,16 @@ public override void OnClientDisconnect(ulong clientId) { if (clientId == m_ConnectionManager.NetworkManager.LocalClientId) { - m_ConnectStatusPublisher.Publish(ConnectStatus.StartHostFailed); - m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline); + StartHostFailed(); } } + void StartHostFailed() + { + m_ConnectStatusPublisher.Publish(ConnectStatus.StartHostFailed); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline); + } + public override void OnServerStarted() { m_ConnectStatusPublisher.Publish(ConnectStatus.Success); @@ -68,36 +79,21 @@ public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest requ async void StartHost() { - if (m_LobbyServiceFacade.CurrentUnityLobby != null) + try { - Debug.Log("Setting up Unity Relay host"); - - try - { - var (ipv4Address, port, allocationIdBytes, connectionData, key, joinCode) = - await UnityRelayUtilities.AllocateRelayServerAndGetJoinCode(k_MaxUnityRelayConnections); - - m_LocalLobby.RelayJoinCode = joinCode; - //next line enabled lobby and relay services integration - await m_LobbyServiceFacade.UpdateLobbyDataAsync(m_LocalLobby.GetDataForUnityServices()); - await m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode); + await m_ConnectionMethod.SetupHostConnectionAsync(); + Debug.Log($"Created relay allocation with join code {m_LocalLobby.RelayJoinCode}"); - // we now need to set the RelayCode somewhere :P - var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; - utp.SetHostRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, isSecure: true); - } - catch (Exception e) + // NGO's StartHost launches everything + if (!m_ConnectionManager.NetworkManager.StartHost()) { - Debug.LogErrorFormat($"{e.Message}"); - throw; + OnClientDisconnect(m_ConnectionManager.NetworkManager.LocalClientId); } - - Debug.Log($"Created relay allocation with join code {m_LocalLobby.RelayJoinCode}"); } - - if (!m_ConnectionManager.NetworkManager.StartHost()) + catch (Exception) { - OnClientDisconnect(m_ConnectionManager.NetworkManager.LocalClientId); + StartHostFailed(); + throw; } } } diff --git a/Assets/Scripts/ConnectionManagement/Unity.BossRoom.ConnectionManagement.asmdef b/Assets/Scripts/ConnectionManagement/Unity.BossRoom.ConnectionManagement.asmdef index 4f0905cd6..0d83eed23 100644 --- a/Assets/Scripts/ConnectionManagement/Unity.BossRoom.ConnectionManagement.asmdef +++ b/Assets/Scripts/ConnectionManagement/Unity.BossRoom.ConnectionManagement.asmdef @@ -11,7 +11,8 @@ "Unity.BossRoom.Utils", "Unity.Services.Lobbies", "Unity.Networking.Transport", - "VContainer" + "VContainer", + "Unity.Services.Relay" ], "includePlatforms": [], "excludePlatforms": [], @@ -22,4 +23,4 @@ "defineConstraints": [], "versionDefines": [], "noEngineReferences": false -} \ No newline at end of file +} diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs deleted file mode 100644 index ca401330d..000000000 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using UnityEngine; -using Unity.Services.Relay; -using Unity.Services.Relay.Models; - -namespace Unity.Multiplayer.Samples.BossRoom -{ - public static class UnityRelayUtilities - { - const string k_KDtlsConnType = "dtls"; - - public static async - Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string - joinCode)> AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null) - { - Allocation allocation; - string joinCode; - - try - { - allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, region); - } - catch (Exception exception) - { - throw new Exception($"Creating allocation request has failed: \n {exception.Message}"); - } - - Debug.Log($"server: connection data: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}, allocation ID:{allocation.AllocationId}, region:{allocation.Region}"); - - try - { - joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); - } - catch (Exception exception) - { - throw new Exception($"Creating join code request has failed: \n {exception.Message}"); - } - - var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType); - return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, - allocation.ConnectionData, allocation.Key, joinCode); - } - - public static async - Task<(string ipv4address, ushort port, byte[] allocationIdBytes, Guid allocationId, byte[] connectionData, byte[] - hostConnectionData, byte[] key)> JoinRelayServerFromJoinCode(string joinCode) - { - JoinAllocation allocation; - try - { - allocation = await RelayService.Instance.JoinAllocationAsync(joinCode); - } - catch (Exception exception) - { - throw new Exception($"Creating join code request has failed: \n {exception.Message}"); - } - - Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}"); - Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}"); - Debug.Log($"client: {allocation.AllocationId}"); - - var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType); - return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, allocation.AllocationId, - allocation.ConnectionData, allocation.HostConnectionData, allocation.Key); - } - } -} diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta deleted file mode 100644 index 5f7c5d785..000000000 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4022140b8f4a4975976098b76e62e4eb -timeCreated: 1629198174 \ No newline at end of file From 29d79b8946128c084414053a20f4d7e9dd3d42e2 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Fri, 14 Oct 2022 17:41:10 -0400 Subject: [PATCH 03/21] setting back max payload size to default value. With recent changes, this now only applies to unreliable messages. With how little NGO uses unreliable messages, this shouldn't need any tweaking anymore. reserializing NetworkingManager, this removes the old max send queue size value as well. --- Assets/Prefabs/NetworkingManager.prefab | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Assets/Prefabs/NetworkingManager.prefab b/Assets/Prefabs/NetworkingManager.prefab index ab4bcbdc9..da7eedb83 100644 --- a/Assets/Prefabs/NetworkingManager.prefab +++ b/Assets/Prefabs/NetworkingManager.prefab @@ -136,8 +136,7 @@ MonoBehaviour: m_EditorClassIdentifier: m_ProtocolType: 0 m_MaxPacketQueueSize: 512 - m_MaxPayloadSize: 32000 - m_MaxSendQueueSize: 50000000 + m_MaxPayloadSize: 6144 m_HeartbeatTimeoutMS: 500 m_ConnectTimeoutMS: 1000 m_MaxConnectAttempts: 10 From 2b50fd9bcae8b63435d0e741514327c858129532 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Fri, 14 Oct 2022 17:44:07 -0400 Subject: [PATCH 04/21] Increasing connection approval timeout to 5 seconds, as 1 second was pretty short for a connection. If there's any packet drops or temporary lag spike, this could timeout the connection when it was actually fine. Still lower than the default 10 seconds. --- Assets/Prefabs/NetworkingManager.prefab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Prefabs/NetworkingManager.prefab b/Assets/Prefabs/NetworkingManager.prefab index da7eedb83..93f7165ce 100644 --- a/Assets/Prefabs/NetworkingManager.prefab +++ b/Assets/Prefabs/NetworkingManager.prefab @@ -108,7 +108,7 @@ MonoBehaviour: SourceHashToOverride: 0 OverridingTargetPrefab: {fileID: 0} TickRate: 30 - ClientConnectionBufferTimeout: 1 + ClientConnectionBufferTimeout: 5 ConnectionApproval: 1 ConnectionData: EnableTimeResync: 0 From 59ab4751b2e30ae29a5e1d32146d56fb9fc503f6 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 17 Oct 2022 10:00:16 -0400 Subject: [PATCH 05/21] making diff clearer for review --- .../ConnectionState/ClientConnectingState.cs | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index 57f77d53a..e7607dd0c 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -31,29 +31,6 @@ public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod return this; } - internal async Task ConnectClientAsync() - { - try - { - // Setup NGO with current connection method - await m_ConnectionMethod.SetupClientConnectionAsync(); - - // NGO's StartClient launches everything - if (!m_ConnectionManager.NetworkManager.StartClient()) - { - throw new Exception("NetworkManager StartClient failed"); - } - - SceneLoaderWrapper.Instance.AddOnSceneEventCallback(); - m_ConnectionManager.RegisterCustomMessages(); - } - catch (Exception) - { - StartingClientFailedAsync(); - throw; - } - } - public override void Enter() { #pragma warning disable 4014 @@ -86,5 +63,29 @@ public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason) m_ConnectStatusPublisher.Publish(disconnectReason); m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason); } + + + internal async Task ConnectClientAsync() + { + try + { + // Setup NGO with current connection method + await m_ConnectionMethod.SetupClientConnectionAsync(); + + // NGO's StartClient launches everything + if (!m_ConnectionManager.NetworkManager.StartClient()) + { + throw new Exception("NetworkManager StartClient failed"); + } + + SceneLoaderWrapper.Instance.AddOnSceneEventCallback(); + m_ConnectionManager.RegisterCustomMessages(); + } + catch (Exception) + { + StartingClientFailedAsync(); + throw; + } + } } } From d3f59da66655656e0ad50b588993f64c3354c38c Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 17 Oct 2022 15:40:25 -0400 Subject: [PATCH 06/21] adding better exception error --- .../ConnectionState/ClientConnectingState.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index e7607dd0c..526c50389 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -81,8 +81,9 @@ internal async Task ConnectClientAsync() SceneLoaderWrapper.Instance.AddOnSceneEventCallback(); m_ConnectionManager.RegisterCustomMessages(); } - catch (Exception) + catch (Exception e) { + Debug.LogException(e); StartingClientFailedAsync(); throw; } From 5bb29946e37131ba4e0114d443dab24807d26b6e Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 17 Oct 2022 15:40:47 -0400 Subject: [PATCH 07/21] using classes instead of structs now that we have managed types available in netvars --- Assets/Scripts/Infrastructure/NetworkGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Scripts/Infrastructure/NetworkGuid.cs b/Assets/Scripts/Infrastructure/NetworkGuid.cs index 05eb28377..6c28d2b3f 100644 --- a/Assets/Scripts/Infrastructure/NetworkGuid.cs +++ b/Assets/Scripts/Infrastructure/NetworkGuid.cs @@ -3,7 +3,7 @@ namespace Unity.BossRoom.Infrastructure { - public struct NetworkGuid : INetworkSerializable + public class NetworkGuid : INetworkSerializable { public ulong FirstHalf; public ulong SecondHalf; From 2913b23cd22363a5fd8c3d540707ad33b1ebca4e Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 17 Oct 2022 16:57:53 -0400 Subject: [PATCH 08/21] fixing null ref when doing reconnection flow --- .../ConnectionState/ClientConnectingState.cs | 3 ++- .../ConnectionState/OfflineState.cs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index 526c50389..12c5173a7 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -48,7 +48,7 @@ public override void OnClientConnected(ulong _) public override void OnClientDisconnect(ulong _) { - // client ID is necessarily ours here + // client ID is for sure ours here StartingClientFailedAsync(); } @@ -83,6 +83,7 @@ internal async Task ConnectClientAsync() } catch (Exception e) { + Debug.LogError("Error connecting client, see following exception"); Debug.LogException(e); StartingClientFailedAsync(); throw; diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs index fd655ae91..63621d4ab 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs @@ -42,22 +42,28 @@ public override void Exit() { } public override void StartClientIP(string playerName, string ipaddress, int port) { - m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName))); + var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName); + m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod)); } public override void StartClientLobby(string playerName) { - m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName))); + var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName); + m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod)); } public override void StartHostIP(string playerName, string ipaddress, int port) { - m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName))); + var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod)); } public override void StartHostLobby(string playerName) { - m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName))); + var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName); + m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod)); } } } From 5f3bc3ee5dad448a0b7538bcc2e4ca78044427e2 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Tue, 18 Oct 2022 16:35:03 -0400 Subject: [PATCH 09/21] reverting removal of utilities --- Assets/Scripts.meta | 8 +++ .../Utilities/Net/UnityRelayUtilities.cs | 69 +++++++++++++++++++ .../Utilities/Net/UnityRelayUtilities.cs.meta | 3 + 3 files changed, 80 insertions(+) create mode 100644 Assets/Scripts.meta create mode 100644 Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs create mode 100644 Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta diff --git a/Assets/Scripts.meta b/Assets/Scripts.meta new file mode 100644 index 000000000..a24a7e7bc --- /dev/null +++ b/Assets/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8e8389b584b3f314a916cbb2dcfe78a9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs new file mode 100644 index 000000000..ca401330d --- /dev/null +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs @@ -0,0 +1,69 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using UnityEngine; +using Unity.Services.Relay; +using Unity.Services.Relay.Models; + +namespace Unity.Multiplayer.Samples.BossRoom +{ + public static class UnityRelayUtilities + { + const string k_KDtlsConnType = "dtls"; + + public static async + Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string + joinCode)> AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null) + { + Allocation allocation; + string joinCode; + + try + { + allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, region); + } + catch (Exception exception) + { + throw new Exception($"Creating allocation request has failed: \n {exception.Message}"); + } + + Debug.Log($"server: connection data: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}, allocation ID:{allocation.AllocationId}, region:{allocation.Region}"); + + try + { + joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); + } + catch (Exception exception) + { + throw new Exception($"Creating join code request has failed: \n {exception.Message}"); + } + + var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType); + return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, + allocation.ConnectionData, allocation.Key, joinCode); + } + + public static async + Task<(string ipv4address, ushort port, byte[] allocationIdBytes, Guid allocationId, byte[] connectionData, byte[] + hostConnectionData, byte[] key)> JoinRelayServerFromJoinCode(string joinCode) + { + JoinAllocation allocation; + try + { + allocation = await RelayService.Instance.JoinAllocationAsync(joinCode); + } + catch (Exception exception) + { + throw new Exception($"Creating join code request has failed: \n {exception.Message}"); + } + + Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}"); + Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}"); + Debug.Log($"client: {allocation.AllocationId}"); + + var dtlsEndpoint = allocation.ServerEndpoints.First(e => e.ConnectionType == k_KDtlsConnType); + return (dtlsEndpoint.Host, (ushort)dtlsEndpoint.Port, allocation.AllocationIdBytes, allocation.AllocationId, + allocation.ConnectionData, allocation.HostConnectionData, allocation.Key); + } + } +} diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta new file mode 100644 index 000000000..5f7c5d785 --- /dev/null +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4022140b8f4a4975976098b76e62e4eb +timeCreated: 1629198174 \ No newline at end of file From b84f7a762e7ea17c205159f30c4e69e64172cce9 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Tue, 18 Oct 2022 17:26:01 -0400 Subject: [PATCH 10/21] deprecated note --- .../Utilities/Net/UnityRelayUtilities.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs index ca401330d..f792fe1d2 100644 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/UnityRelayUtilities.cs @@ -11,9 +11,16 @@ public static class UnityRelayUtilities { const string k_KDtlsConnType = "dtls"; - public static async - Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string - joinCode)> AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null) + /// + /// Deprecated, please see updated ConnectionManager sample code for an example on how to connect to Relay + /// + /// + /// + /// + /// + /// + public static async Task<(string ipv4address, ushort port, byte[] allocationIdBytes, byte[] connectionData, byte[] key, string joinCode)> + AllocateRelayServerAndGetJoinCode(int maxConnections, string region = null) { Allocation allocation; string joinCode; @@ -43,9 +50,15 @@ public static async allocation.ConnectionData, allocation.Key, joinCode); } - public static async - Task<(string ipv4address, ushort port, byte[] allocationIdBytes, Guid allocationId, byte[] connectionData, byte[] - hostConnectionData, byte[] key)> JoinRelayServerFromJoinCode(string joinCode) + /// + /// Deprecated, please see updated ConnectionManager sample code for an example on how to connect to Relay + /// + /// + /// + /// + /// + public static async Task<(string ipv4address, ushort port, byte[] allocationIdBytes, Guid allocationId, byte[] connectionData, byte[] hostConnectionData, byte[] key)> + JoinRelayServerFromJoinCode(string joinCode) { JoinAllocation allocation; try From b7e83c39ba18691b9ca284697325c23c82dba32a Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Wed, 19 Oct 2022 14:05:33 -0400 Subject: [PATCH 11/21] better comments, cleaner easier to read code. --- .../ConnectionManagement/ConnectionMethod.cs | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs index 1d817b510..a7b757732 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs @@ -13,24 +13,19 @@ namespace ConnectionManagement.ConnectionState { + /// + /// ConnectionMethod contains all setup needed to setup NGO to be ready to start a connection, either host or client side. + /// Please override this abstract class to add a new transport or way of connecting. + /// public abstract class ConnectionMethodBase { protected ConnectionManager m_ConnectionManager; readonly ProfileManager m_ProfileManager; - readonly string m_PlayerName; + protected readonly string m_PlayerName; - public virtual async Task SetupHostConnectionAsync() - { - // Since hosts are clients too, we need to set their payload as well - SetConnectionPayload(GetPlayerId(), m_PlayerName); - // This method is virtual, see child classes for further implementation - } + public abstract Task SetupHostConnectionAsync(); - public virtual async Task SetupClientConnectionAsync() - { - SetConnectionPayload(GetPlayerId(), m_PlayerName); - // This method is virtual, see child classes for further implementation - } + public abstract Task SetupClientConnectionAsync(); public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager profileManager, string playerName) { @@ -39,7 +34,7 @@ public ConnectionMethodBase(ConnectionManager connectionManager, ProfileManager m_PlayerName = playerName; } - void SetConnectionPayload(string playerId, string playerName) + protected void SetConnectionPayload(string playerId, string playerName) { var payload = JsonUtility.ToJson(new ConnectionPayload() { @@ -53,7 +48,7 @@ void SetConnectionPayload(string playerId, string playerName) m_ConnectionManager.NetworkManager.NetworkConfig.ConnectionData = payloadBytes; } - string GetPlayerId() + protected string GetPlayerId() { if (UnityServices.State != ServicesInitializationState.Initialized) { @@ -64,6 +59,7 @@ string GetPlayerId() } } + // Simple IP connection setup with UTP class ConnectionMethodIP : ConnectionMethodBase { string m_Ipaddress; @@ -79,19 +75,22 @@ public ConnectionMethodIP(string ip, ushort port, ConnectionManager connectionMa public override async Task SetupClientConnectionAsync() { - await base.SetupClientConnectionAsync(); + SetConnectionPayload(GetPlayerId(), m_PlayerName); var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; utp.SetConnectionData(m_Ipaddress, m_Port); } public override async Task SetupHostConnectionAsync() { - await base.SetupHostConnectionAsync(); + SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport; utp.SetConnectionData(m_Ipaddress, m_Port); } } + /// + /// UTP's Relay connection setup + /// class ConnectionMethodRelay : ConnectionMethodBase { LobbyServiceFacade m_LobbyServiceFacade; @@ -109,7 +108,7 @@ public override async Task SetupClientConnectionAsync() { Debug.Log("Setting up Unity Relay client"); - await base.SetupClientConnectionAsync(); + SetConnectionPayload(GetPlayerId(), m_PlayerName); if (m_LobbyServiceFacade.CurrentUnityLobby == null) { @@ -135,7 +134,7 @@ public override async Task SetupHostConnectionAsync() { Debug.Log("Setting up Unity Relay host"); - await base.SetupHostConnectionAsync(); + SetConnectionPayload(GetPlayerId(), m_PlayerName); // Need to set connection payload for host as well, as host is a client too // Create relay allocation Allocation hostAllocation = await RelayService.Instance.CreateAllocationAsync(m_ConnectionManager.MaxConnectedPlayers, region: null); From 243bdefcd081744d4b6b0eeb8c0290e39c38cf7b Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Wed, 19 Oct 2022 14:14:22 -0400 Subject: [PATCH 12/21] cleanup --- Assets/Scripts/ConnectionManagement/ConnectionMethod.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs index a7b757732..ec251d643 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs @@ -59,7 +59,9 @@ protected string GetPlayerId() } } - // Simple IP connection setup with UTP + /// + /// Simple IP connection setup with UTP + /// class ConnectionMethodIP : ConnectionMethodBase { string m_Ipaddress; From 171a9362427b53e16c5d7fbf9a989d96199305e8 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Wed, 19 Oct 2022 14:18:56 -0400 Subject: [PATCH 13/21] Update changelog update required NGO package version for utilities --- CHANGELOG.md | 8 +++++++- Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md | 2 +- Packages/com.unity.multiplayer.samples.coop/package.json | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a54919a..aad2828c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,13 @@ Additional documentation and release notes are available at [Multiplayer Documen * ### Changed -* +* Updated Boss Room to NGO 1.1.0 + * Now uses managed types for custom INetworkSerializable in NetworkVariables. NetworkGUID is now a class instead of a struct. + * Cleanup Relay and UTP setup. Flow is now simpler, no need for the RelayUtilities anymore. + * This cleansup various setup steps and puts them all in a new "ConnectionMethod.cs". + * MaxSendQueueSize value is removed, reserialized NetworkManager to remove that now useless value. + * Reverted the default value for max payload size, this is no longer useful as NGO is mostly reliable. + * Set connection approval timeout higher, 1 sec is pretty short. If there's a packet drop, some hangups on the network, clients would get timedout too easily. ### Cleanup * Removed unnecessary FindObjectOfType usage inside of ClientCharSelectState (#754) diff --git a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md index ec0a7cb71..a1ccf0bfb 100644 --- a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md +++ b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md @@ -7,7 +7,7 @@ ### Changed * ### Removed -* +* Deprecated Unity Relay Utilities, it should no longer be needed with NGO 1.1.0's new API for setting up Relay ### Fixed * diff --git a/Packages/com.unity.multiplayer.samples.coop/package.json b/Packages/com.unity.multiplayer.samples.coop/package.json index c4f6671cd..eefd8ef10 100644 --- a/Packages/com.unity.multiplayer.samples.coop/package.json +++ b/Packages/com.unity.multiplayer.samples.coop/package.json @@ -9,7 +9,7 @@ "dependencies": { "com.unity.learn.iet-framework": "1.2.1", "com.unity.multiplayer.tools": "1.0.0", - "com.unity.netcode.gameobjects": "1.0.2", + "com.unity.netcode.gameobjects": "1.1.0", "com.unity.services.relay": "1.0.3" } -} +} \ No newline at end of file From 55d13bab0c54cd6c5fa3fb1ff174d313a1bf7f66 Mon Sep 17 00:00:00 2001 From: Sam Bellomo <71790295+SamuelBellomo@users.noreply.github.com> Date: Wed, 19 Oct 2022 16:24:23 -0400 Subject: [PATCH 14/21] adding missing PR ID --- Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md index a1ccf0bfb..8d5fc4a36 100644 --- a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md +++ b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md @@ -7,7 +7,7 @@ ### Changed * ### Removed -* Deprecated Unity Relay Utilities, it should no longer be needed with NGO 1.1.0's new API for setting up Relay +* Deprecated Unity Relay Utilities, it should no longer be needed with NGO 1.1.0's new API for setting up Relay (#708) ### Fixed * From e163ce146a7e80724ed7f0231d646cb551934284 Mon Sep 17 00:00:00 2001 From: Sam Bellomo <71790295+SamuelBellomo@users.noreply.github.com> Date: Wed, 19 Oct 2022 16:25:01 -0400 Subject: [PATCH 15/21] adding missing PR ID --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 167231d6e..bb39a256d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen * ### Changed -* Updated Boss Room to NGO 1.1.0 +* Updated Boss Room to NGO 1.1.0 (#708) * Now uses managed types for custom INetworkSerializable in NetworkVariables. NetworkGUID is now a class instead of a struct. * Cleanup Relay and UTP setup. Flow is now simpler, no need for the RelayUtilities anymore. * This cleansup various setup steps and puts them all in a new "ConnectionMethod.cs". From b6705f4b9d6a711b4039de39e73e2589fc8b739a Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Thu, 20 Oct 2022 11:52:38 -0400 Subject: [PATCH 16/21] removing useless usings --- .../ConnectionState/ClientConnectingState.cs | 4 ---- .../ConnectionManagement/ConnectionState/OfflineState.cs | 3 --- .../ConnectionManagement/ConnectionState/StartingHostState.cs | 4 ---- Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs | 1 - .../Utilities/SceneManagement/LoadingProgressManager.cs | 1 - .../Utilities/SceneManagement/SceneLoaderWrapper.cs | 1 - 6 files changed, 14 deletions(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index 12c5173a7..4142d25f0 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -2,11 +2,7 @@ using System.Threading.Tasks; using ConnectionManagement.ConnectionState; using Unity.BossRoom.UnityServices.Lobbies; -using Unity.Multiplayer.Samples.BossRoom; using Unity.Multiplayer.Samples.Utilities; -using Unity.Netcode.Transports.UTP; -using Unity.Networking.Transport.Relay; -using Unity.Services.Relay; using UnityEngine; using VContainer; diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs index 63621d4ab..499b40497 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs @@ -4,9 +4,6 @@ using Unity.BossRoom.UnityServices.Lobbies; using Unity.BossRoom.Utils; using Unity.Multiplayer.Samples.Utilities; -using Unity.Netcode.Transports.UTP; -using Unity.Services.Authentication; -using Unity.Services.Core; using UnityEngine; using UnityEngine.SceneManagement; using VContainer; diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs index 7065f6c1d..6cbb75190 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs @@ -4,10 +4,6 @@ using Unity.BossRoom.UnityServices.Lobbies; using Unity.Multiplayer.Samples.BossRoom; using Unity.Netcode; -using Unity.Netcode.Transports.UTP; -using Unity.Networking.Transport.Relay; -using Unity.Services.Relay; -using Unity.Services.Relay.Models; using UnityEngine; using VContainer; diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs b/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs index 2edb3ace1..00aa20303 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs @@ -3,7 +3,6 @@ using Unity.BossRoom.Infrastructure; using Unity.Netcode; using UnityEngine; -using UnityEngine.Assertions; namespace Unity.BossRoom.Gameplay.GameplayObjects { diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/LoadingProgressManager.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/LoadingProgressManager.cs index a40a82753..a23fd6b2c 100644 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/LoadingProgressManager.cs +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/LoadingProgressManager.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using Unity.Netcode; using UnityEngine; diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs index 5be25c6ae..0ad640cd3 100644 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using Unity.Netcode; using UnityEngine; using UnityEngine.SceneManagement; From 2fe2683d25ba90a24b6b48709af2a3442342d1c6 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Thu, 20 Oct 2022 11:55:50 -0400 Subject: [PATCH 17/21] namespace adjustements --- Assets/Scripts/ConnectionManagement/ConnectionMethod.cs | 5 ++--- .../ConnectionState/ClientConnectingState.cs | 1 - .../ConnectionManagement/ConnectionState/OfflineState.cs | 1 - .../ConnectionState/StartingHostState.cs | 1 - 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs index ec251d643..4ac0cbac0 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionMethod.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using Unity.BossRoom.ConnectionManagement; using Unity.BossRoom.UnityServices.Lobbies; using Unity.BossRoom.Utils; using Unity.Netcode.Transports.UTP; @@ -11,7 +10,7 @@ using Unity.Services.Relay.Models; using UnityEngine; -namespace ConnectionManagement.ConnectionState +namespace Unity.BossRoom.ConnectionManagement { /// /// ConnectionMethod contains all setup needed to setup NGO to be ready to start a connection, either host or client side. @@ -50,7 +49,7 @@ protected void SetConnectionPayload(string playerId, string playerName) protected string GetPlayerId() { - if (UnityServices.State != ServicesInitializationState.Initialized) + if (Services.Core.UnityServices.State != ServicesInitializationState.Initialized) { return ClientPrefs.GetGuid() + m_ProfileManager.Profile; } diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs index 4142d25f0..2b27fef74 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using ConnectionManagement.ConnectionState; using Unity.BossRoom.UnityServices.Lobbies; using Unity.Multiplayer.Samples.Utilities; using UnityEngine; diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs index 499b40497..34dc047df 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/OfflineState.cs @@ -1,5 +1,4 @@ using System; -using ConnectionManagement.ConnectionState; using Unity.BossRoom.ConnectionManagement; using Unity.BossRoom.UnityServices.Lobbies; using Unity.BossRoom.Utils; diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs index 6cbb75190..eca47b3b6 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs @@ -1,5 +1,4 @@ using System; -using ConnectionManagement.ConnectionState; using Unity.BossRoom.Infrastructure; using Unity.BossRoom.UnityServices.Lobbies; using Unity.Multiplayer.Samples.BossRoom; From dfda37852fd4eadef22d28af70bfbbf040734480 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Thu, 20 Oct 2022 15:27:18 -0400 Subject: [PATCH 18/21] updating manifest and dependencies with 1.1.0 now it's released --- Packages/manifest.json | 2 +- Packages/packages-lock.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Packages/manifest.json b/Packages/manifest.json index cae525a80..689f807a3 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -10,7 +10,7 @@ "com.unity.learn.iet-framework": "2.2.2", "com.unity.memoryprofiler": "0.5.0-preview.1", "com.unity.multiplayer.tools": "1.0.0", - "com.unity.netcode.gameobjects": "1.0.2", + "com.unity.netcode.gameobjects": "1.1.0", "com.unity.performance.profile-analyzer": "1.1.1", "com.unity.postprocessing": "3.2.2", "com.unity.render-pipelines.universal": "12.1.7", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 93d1b45c4..91bca3302 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -122,7 +122,7 @@ "dependencies": { "com.unity.learn.iet-framework": "1.2.1", "com.unity.multiplayer.tools": "1.0.0", - "com.unity.netcode.gameobjects": "1.0.2", + "com.unity.netcode.gameobjects": "1.1.0", "com.unity.services.relay": "1.0.3" } }, @@ -140,12 +140,12 @@ "url": "https://packages.unity.com" }, "com.unity.netcode.gameobjects": { - "version": "1.0.2", + "version": "1.1.0", "depth": 0, "source": "registry", "dependencies": { "com.unity.nuget.mono-cecil": "1.10.1", - "com.unity.transport": "1.2.0" + "com.unity.transport": "1.3.0" }, "url": "https://packages.unity.com" }, @@ -359,7 +359,7 @@ "url": "https://packages.unity.com" }, "com.unity.transport": { - "version": "1.2.0", + "version": "1.3.0", "depth": 1, "source": "registry", "dependencies": { From 521c01952d43d214f61143da9d7bdca0286c9fc5 Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Mon, 24 Oct 2022 14:39:38 -0400 Subject: [PATCH 19/21] initializing number of reconnect attempts before starting the reconnect coroutine --- .../ConnectionState/ClientReconnectingState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs index 8dd2d46f7..4d0ad9e82 100644 --- a/Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs +++ b/Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs @@ -25,9 +25,9 @@ class ClientReconnectingState : ClientConnectingState public override void Enter() { + m_NbAttempts = 0; m_LobbyCode = m_LobbyServiceFacade.CurrentUnityLobby != null ? m_LobbyServiceFacade.CurrentUnityLobby.LobbyCode : ""; m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine()); - m_NbAttempts = 0; } public override void Exit() From 48573dd8c776a20ab73ce50c5819a2d600430c61 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 24 Oct 2022 15:21:46 -0400 Subject: [PATCH 20/21] fixing player prefab being destroyed issue --- Assets/Tests/Runtime/NetworkedMessageChannelTests.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs b/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs index 511dc121c..2652aebb8 100644 --- a/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs +++ b/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs @@ -149,15 +149,18 @@ public IEnumerator NetworkedMessagesAreStillReceivedAfterNetworkManagerShutsDown m_NbMessagesReceived = 0; + m_PlayerPrefab.SetActive(false); // to prevent NM from destroying the prefab on shutdown. This flow isn't great and is hackish, should be reworked // Shutdown the server and clients - NetcodeIntegrationTestHelpers.StopOneClient(m_ServerNetworkManager, false); - NetcodeIntegrationTestHelpers.StopOneClient(m_ClientNetworkManagers[0], false); - NetcodeIntegrationTestHelpers.StopOneClient(m_ClientNetworkManagers[1], false); + m_ServerNetworkManager.Shutdown(); + m_ClientNetworkManagers[0].Shutdown(); + m_ClientNetworkManagers[1].Shutdown(); yield return new WaitWhile(() => m_ServerNetworkManager.ShutdownInProgress); yield return new WaitWhile(() => m_ClientNetworkManagers[0].ShutdownInProgress); yield return new WaitWhile(() => m_ClientNetworkManagers[1].ShutdownInProgress); + m_PlayerPrefab.SetActive(true); // reactivating after destroy prevention + // Restart the server and clients m_ServerNetworkManager.StartHost(); NetcodeIntegrationTestHelpers.StartOneClient(m_ClientNetworkManagers[0]); From 324a5ece7ef63b32c843dbd1e6fed6125fb12783 Mon Sep 17 00:00:00 2001 From: Sam Bellomo <71790295+SamuelBellomo@users.noreply.github.com> Date: Mon, 24 Oct 2022 18:05:04 -0400 Subject: [PATCH 21/21] Removing workaround With latest develop fixes (see PR https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/pull/2275 ) this workaround shouldn't be needed anymore --- Assets/Tests/Runtime/NetworkedMessageChannelTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs b/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs index 2652aebb8..731fd4d57 100644 --- a/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs +++ b/Assets/Tests/Runtime/NetworkedMessageChannelTests.cs @@ -149,7 +149,6 @@ public IEnumerator NetworkedMessagesAreStillReceivedAfterNetworkManagerShutsDown m_NbMessagesReceived = 0; - m_PlayerPrefab.SetActive(false); // to prevent NM from destroying the prefab on shutdown. This flow isn't great and is hackish, should be reworked // Shutdown the server and clients m_ServerNetworkManager.Shutdown(); m_ClientNetworkManagers[0].Shutdown(); @@ -159,8 +158,6 @@ public IEnumerator NetworkedMessagesAreStillReceivedAfterNetworkManagerShutsDown yield return new WaitWhile(() => m_ClientNetworkManagers[0].ShutdownInProgress); yield return new WaitWhile(() => m_ClientNetworkManagers[1].ShutdownInProgress); - m_PlayerPrefab.SetActive(true); // reactivating after destroy prevention - // Restart the server and clients m_ServerNetworkManager.StartHost(); NetcodeIntegrationTestHelpers.StartOneClient(m_ClientNetworkManagers[0]);