From 9bcba0e00406e4e8df1a0473d3d1a430cc5cf7ba Mon Sep 17 00:00:00 2001 From: Derek S <44935661+Derek-R-S@users.noreply.github.com> Date: Tue, 6 Apr 2021 00:54:22 -0500 Subject: [PATCH] Added cross-relay connectivity --- .../LRMTransport/LRMTransportOverrides.cs | 33 ++++++++++++------- .../LRMTransport/LRMTransportRequests.cs | 30 +++++++++++++++++ .../LightReflectiveMirrorTransport.cs | 19 +++++++++++ 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/UnityTransport/LRMTransport/LRMTransportOverrides.cs b/UnityTransport/LRMTransport/LRMTransportOverrides.cs index 267bb5e..37bef12 100644 --- a/UnityTransport/LRMTransport/LRMTransportOverrides.cs +++ b/UnityTransport/LRMTransport/LRMTransportOverrides.cs @@ -68,20 +68,29 @@ namespace LightReflectiveMirror if (_isClient || _isServer) throw new Exception("Cannot connect while hosting/already connected!"); - int pos = 0; - _directConnected = false; - _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); - _clientSendBuffer.WriteInt(ref pos, _cachedHostID); - _clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); + var room = GetServerForID(_cachedHostID); - if (GetLocalIp() == null) - _clientSendBuffer.WriteString(ref pos, "0.0.0.0"); + if (!useLoadBalancer || room.relayInfo.Address == serverIP) + { + int pos = 0; + _directConnected = false; + _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); + _clientSendBuffer.WriteInt(ref pos, _cachedHostID); + _clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); + + 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)); + { + StartCoroutine(JoinOtherRelayAndMatch(room)); + } } public override void ClientDisconnect() diff --git a/UnityTransport/LRMTransport/LRMTransportRequests.cs b/UnityTransport/LRMTransport/LRMTransportRequests.cs index 6d6a19d..994ea2f 100644 --- a/UnityTransport/LRMTransport/LRMTransportRequests.cs +++ b/UnityTransport/LRMTransport/LRMTransportRequests.cs @@ -58,6 +58,36 @@ namespace LightReflectiveMirror } } + IEnumerator JoinOtherRelayAndMatch(Room room) + { + // Wait for disconnection + DisconnectFromRelay(); + + while (IsAuthenticated()) + yield return new WaitForEndOfFrame(); + + endpointServerPort = room.relayInfo.EndpointPort; + Connect(room.relayInfo.Address, room.relayInfo.Port); + + while (!IsAuthenticated()) + yield return new WaitForEndOfFrame(); + + int pos = 0; + _directConnected = false; + _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); + _clientSendBuffer.WriteInt(ref pos, room.serverId); + _clientSendBuffer.WriteBool(ref pos, _directConnectModule != null); + + 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)); + } + IEnumerator GetServerList() { if (!useLoadBalancer) diff --git a/UnityTransport/LRMTransport/LightReflectiveMirrorTransport.cs b/UnityTransport/LRMTransport/LightReflectiveMirrorTransport.cs index 3dfd34b..90f7416 100644 --- a/UnityTransport/LRMTransport/LightReflectiveMirrorTransport.cs +++ b/UnityTransport/LRMTransport/LightReflectiveMirrorTransport.cs @@ -103,6 +103,14 @@ namespace LightReflectiveMirror clientToServerTransport.ClientConnect(serverIP); } + private void DisconnectFromRelay() + { + if (IsAuthenticated()) + { + clientToServerTransport.ClientDisconnect(); + } + } + void SendHeartbeat() { if (_connectedToRelay) @@ -318,6 +326,17 @@ namespace LightReflectiveMirror } } + Room GetServerForID(int serverID) + { + for(int i = 0; i < relayServerList.Count; i++) + { + if(relayServerList[i].serverId == serverId) + return relayServerList[i]; + } + + throw new Exception("LRM | An attempt was made to connect to a server which does not exist!"); + } + void SendAuthKey() { int pos = 0;