From c5c2750334b4a3ccabdce5716ed043613a2bb455 Mon Sep 17 00:00:00 2001 From: Derek S <44935661+Derek-R-S@users.noreply.github.com> Date: Fri, 9 Apr 2021 09:20:55 -0500 Subject: [PATCH] Fixed LAN direct connect and joining server via ID without refreshing --- UnityProject/Assets/LRMTestScene.unity | 74 ++++++++++++++++++- .../Transport/LRM/LRMDirectConnectModule.cs | 5 +- .../LRM/LRMTransport/LRMTransportOverrides.cs | 36 +++++---- .../LRM/LRMTransport/LRMTransportRequests.cs | 30 +++++++- .../LRM/LRMTransport/LRMTransportVariables.cs | 1 + .../LightReflectiveMirrorTransport.cs | 14 ++-- .../ProjectSettings/EditorBuildSettings.asset | 5 +- .../ProjectSettings/ProjectSettings.asset | 7 +- 8 files changed, 145 insertions(+), 27 deletions(-) diff --git a/UnityProject/Assets/LRMTestScene.unity b/UnityProject/Assets/LRMTestScene.unity index 43ddb4e..b04e73e 100644 --- a/UnityProject/Assets/LRMTestScene.unity +++ b/UnityProject/Assets/LRMTestScene.unity @@ -195,6 +195,59 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 45, y: 180, z: 0} +--- !u!1 &126344731 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 126344733} + - component: {fileID: 126344732} + m_Layer: 0 + m_Name: LRM - Direct Connect + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &126344732 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 126344731} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6b0fecffa3f624585964b0d0eb21b18e, type: 3} + m_Name: + m_EditorClassIdentifier: + Port: 7777 + NoDelay: 1 + Interval: 10 + FastResend: 2 + CongestionWindow: 0 + SendWindowSize: 4096 + ReceiveWindowSize: 4096 + debugLog: 0 + statisticsGUI: 0 + statisticsLog: 0 +--- !u!4 &126344733 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 126344731} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1282001518} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &171810013 GameObject: m_ObjectHideFlags: 0 @@ -1086,6 +1139,7 @@ GameObject: - component: {fileID: 1282001520} - component: {fileID: 1282001519} - component: {fileID: 1282001521} + - component: {fileID: 1282001522} m_Layer: 0 m_Name: NetworkManager m_TagString: Untagged @@ -1105,6 +1159,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 1884364513} + - {fileID: 126344733} m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -1177,9 +1232,9 @@ MonoBehaviour: diconnectedFromRelay: m_PersistentCalls: m_Calls: [] - useNATPunch: 0 + useNATPunch: 1 NATPunchtroughPort: 1 - useLoadBalancer: 0 + useLoadBalancer: 1 loadBalancerPort: 7070 loadBalancerAddress: 172.105.109.117 serverName: My awesome server! @@ -1191,6 +1246,21 @@ MonoBehaviour: m_Calls: [] serverStatus: Not Started. serverId: + region: 1 +--- !u!114 &1282001522 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1282001517} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9c4cbff877abc42448dd829920c6c233, type: 3} + m_Name: + m_EditorClassIdentifier: + directConnectTransport: {fileID: 126344732} + showDebugLogs: 0 --- !u!1 &1458789072 GameObject: m_ObjectHideFlags: 0 diff --git a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMDirectConnectModule.cs b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMDirectConnectModule.cs index c0fb64b..75318e4 100644 --- a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMDirectConnectModule.cs +++ b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMDirectConnectModule.cs @@ -57,8 +57,11 @@ public class LRMDirectConnectModule : MonoBehaviour public void JoinServer(string ip, int port) { - if(SupportsNATPunch()) + if (SupportsNATPunch()) SetTransportPort(port); + + + directConnectTransport.ClientConnect(ip); } diff --git a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportOverrides.cs b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportOverrides.cs index ee712b5..f2ddc91 100644 --- a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportOverrides.cs +++ b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportOverrides.cs @@ -72,26 +72,34 @@ namespace LightReflectiveMirror var room = GetServerForID(address); - if (!useLoadBalancer || room.relayInfo.Address == serverIP) + if (!useLoadBalancer) { - int pos = 0; - _directConnected = false; - _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); - _clientSendBuffer.WriteString(ref pos, address); - _clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); + if (room.HasValue && room.Value.relayInfo.Address == serverIP) + { + int pos = 0; + _directConnected = false; + _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); + _clientSendBuffer.WriteString(ref pos, address); + _clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); - if (GetLocalIp() == null) - _clientSendBuffer.WriteString(ref pos, "0.0.0.0"); + if (GetLocalIp() == null) + _clientSendBuffer.WriteString(ref pos, "0.0.0.0"); + else + _clientSendBuffer.WriteString(ref pos, GetLocalIp()); + + _isClient = true; + + clientToServerTransport.ClientSend(0, new System.ArraySegment(_clientSendBuffer, 0, pos)); + } else - _clientSendBuffer.WriteString(ref pos, GetLocalIp()); - - _isClient = true; - - clientToServerTransport.ClientSend(0, new System.ArraySegment(_clientSendBuffer, 0, pos)); + { + OnClientDisconnected?.Invoke(); + Debug.LogWarning("LRM | Client tried to join server that doesnt exist!"); + } } else { - StartCoroutine(JoinOtherRelayAndMatch(room)); + StartCoroutine(JoinOtherRelayAndMatch(room, address)); } } diff --git a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportRequests.cs b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportRequests.cs index 7a6164e..ec201e4 100644 --- a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportRequests.cs +++ b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportRequests.cs @@ -58,8 +58,34 @@ namespace LightReflectiveMirror } } - IEnumerator JoinOtherRelayAndMatch(Room room) + IEnumerator JoinOtherRelayAndMatch(Room? roomValue, string ID) { + var room = new Room(); + + // using load balancer, we NEED the server's relay address + if (roomValue.HasValue) + room = roomValue.Value; + else + { + _serverListUpdated = false; + RequestServerList(); + + yield return new WaitUntil(() => _serverListUpdated); + + var foundRoom = GetServerForID(ID); + + if (foundRoom.HasValue) + { + room = foundRoom.Value; + } + else + { + Debug.LogWarning("LRM | Client tried to join a server that does not exist!"); + OnClientDisconnected?.Invoke(); + yield break; + } + } + // Wait for disconnection DisconnectFromRelay(); @@ -166,6 +192,7 @@ namespace LightReflectiveMirror relayServerList?.Clear(); relayServerList = JsonConvert.DeserializeObject>(result); serverListUpdated?.Invoke(); + _serverListUpdated = true; break; } #else @@ -178,6 +205,7 @@ namespace LightReflectiveMirror relayServerList?.Clear(); relayServerList = JsonConvert.DeserializeObject>(result); serverListUpdated?.Invoke(); + _serverListUpdated = true; } #endif } diff --git a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportVariables.cs b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportVariables.cs index 70b0042..5086d16 100644 --- a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportVariables.cs +++ b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LRMTransportVariables.cs @@ -61,6 +61,7 @@ namespace LightReflectiveMirror private BiDictionary _serverProxies = new BiDictionary(); private BiDictionary _connectedRelayClients = new BiDictionary(); private BiDictionary _connectedDirectClients = new BiDictionary(); + private bool _serverListUpdated = false; } public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania } diff --git a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LightReflectiveMirrorTransport.cs b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LightReflectiveMirrorTransport.cs index 3b1c5aa..c0d6a09 100644 --- a/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LightReflectiveMirrorTransport.cs +++ b/UnityProject/Assets/Mirror/Runtime/Transport/LRM/LRMTransport/LightReflectiveMirrorTransport.cs @@ -63,7 +63,6 @@ namespace LightReflectiveMirror private void OnConnectedToRelay() { _connectedToRelay = true; - RequestServerList(); } public void ConnectToRelay() @@ -157,6 +156,7 @@ namespace LightReflectiveMirror case OpCodes.Authenticated: serverStatus = "Authenticated! Good to go!"; _isAuthenticated = true; + RequestServerList(); break; case OpCodes.AuthenticationRequest: serverStatus = "Sent authentication to relay..."; @@ -229,7 +229,12 @@ namespace LightReflectiveMirror } if (useNATPunch && attemptNatPunch) - _directConnectModule.JoinServer("127.0.0.1", _NATIP.Port - 1); + { + if (ip == "127.0.0.1") + _directConnectModule.JoinServer("127.0.0.1", port + 1); + else + _directConnectModule.JoinServer("127.0.0.1", _NATIP.Port - 1); + } else _directConnectModule.JoinServer(ip, port); } @@ -336,7 +341,7 @@ namespace LightReflectiveMirror } } - Room GetServerForID(string serverID) + Room? GetServerForID(string serverID) { for(int i = 0; i < relayServerList.Count; i++) { @@ -344,8 +349,7 @@ namespace LightReflectiveMirror return relayServerList[i]; } - OnClientDisconnected?.Invoke(); - throw new Exception("LRM | An attempt was made to connect to a server which does not exist!"); + return null; } void SendAuthKey() diff --git a/UnityProject/ProjectSettings/EditorBuildSettings.asset b/UnityProject/ProjectSettings/EditorBuildSettings.asset index 0147887..4a59852 100644 --- a/UnityProject/ProjectSettings/EditorBuildSettings.asset +++ b/UnityProject/ProjectSettings/EditorBuildSettings.asset @@ -4,5 +4,8 @@ EditorBuildSettings: m_ObjectHideFlags: 0 serializedVersion: 2 - m_Scenes: [] + m_Scenes: + - enabled: 1 + path: Assets/LRMTestScene.unity + guid: e860d5862486efc438d11e20bc7660aa m_configObjects: {} diff --git a/UnityProject/ProjectSettings/ProjectSettings.asset b/UnityProject/ProjectSettings/ProjectSettings.asset index fd89bc2..11b9e3e 100644 --- a/UnityProject/ProjectSettings/ProjectSettings.asset +++ b/UnityProject/ProjectSettings/ProjectSettings.asset @@ -80,7 +80,7 @@ PlayerSettings: bakeCollisionMeshes: 0 forceSingleInstance: 0 useFlipModelSwapchain: 1 - resizableWindow: 0 + resizableWindow: 1 useMacAppStoreValidation: 0 macAppStoreCategory: public.app-category.games gpuSkinning: 0 @@ -91,7 +91,7 @@ PlayerSettings: xboxEnableFitness: 0 visibleInBackground: 1 allowFullscreenSwitch: 1 - fullscreenMode: 1 + fullscreenMode: 3 xboxSpeechDB: 0 xboxEnableHeadOrientation: 0 xboxEnableGuest: 0 @@ -524,7 +524,8 @@ PlayerSettings: scriptingRuntimeVersion: 1 gcIncremental: 0 gcWBarrierValidation: 0 - apiCompatibilityLevelPerPlatform: {} + apiCompatibilityLevelPerPlatform: + Standalone: 3 m_RenderingPath: 1 m_MobileRenderingPath: 1 metroPackageName: UnityProject