Added cross-relay connectivity

This commit is contained in:
Derek S 2021-04-06 00:54:22 -05:00
parent 1e144d97f8
commit 9bcba0e004
3 changed files with 70 additions and 12 deletions

View file

@ -68,6 +68,10 @@ namespace LightReflectiveMirror
if (_isClient || _isServer) if (_isClient || _isServer)
throw new Exception("Cannot connect while hosting/already connected!"); throw new Exception("Cannot connect while hosting/already connected!");
var room = GetServerForID(_cachedHostID);
if (!useLoadBalancer || room.relayInfo.Address == serverIP)
{
int pos = 0; int pos = 0;
_directConnected = false; _directConnected = false;
_clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer);
@ -83,6 +87,11 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientSend(0, new System.ArraySegment<byte>(_clientSendBuffer, 0, pos)); clientToServerTransport.ClientSend(0, new System.ArraySegment<byte>(_clientSendBuffer, 0, pos));
} }
else
{
StartCoroutine(JoinOtherRelayAndMatch(room));
}
}
public override void ClientDisconnect() public override void ClientDisconnect()
{ {

View file

@ -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<byte>(_clientSendBuffer, 0, pos));
}
IEnumerator GetServerList() IEnumerator GetServerList()
{ {
if (!useLoadBalancer) if (!useLoadBalancer)

View file

@ -103,6 +103,14 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientConnect(serverIP); clientToServerTransport.ClientConnect(serverIP);
} }
private void DisconnectFromRelay()
{
if (IsAuthenticated())
{
clientToServerTransport.ClientDisconnect();
}
}
void SendHeartbeat() void SendHeartbeat()
{ {
if (_connectedToRelay) 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() void SendAuthKey()
{ {
int pos = 0; int pos = 0;