From 633cdefb23b6f12b15c624a625bfe3a7f3873c77 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Mon, 29 Mar 2021 15:52:50 -0400 Subject: [PATCH] Unity side refactor --- .../LightReflectiveMirrorTransport.cs | 82 ++++++++----------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/UnityTransport/LightReflectiveMirrorTransport.cs b/UnityTransport/LightReflectiveMirrorTransport.cs index ab43914..fc88446 100644 --- a/UnityTransport/LightReflectiveMirrorTransport.cs +++ b/UnityTransport/LightReflectiveMirrorTransport.cs @@ -37,14 +37,22 @@ namespace LightReflectiveMirror private int _currentMemberId; private bool _callbacksInitialized = false; private BiDictionary _connectedRelayClients = new BiDictionary(); + + public override bool ClientConnected() => _isClient; + private void OnConnectedToRelay() => _connectedToRelay = true; public bool IsAuthenticated() => _isAuthenticated; + public override bool ServerActive() => _isServer; + public override bool Available() => _connectedToRelay; + public override void ClientEarlyUpdate() => clientToServerTransport.ClientEarlyUpdate(); + public override void ClientLateUpdate() => clientToServerTransport.ClientLateUpdate(); + public override void ClientConnect(Uri uri) => ClientConnect(uri.Host); + public override int GetMaxPacketSize(int channelId = 0) => clientToServerTransport.GetMaxPacketSize(channelId); + public override string ServerGetClientAddress(int connectionId) => _connectedRelayClients.GetBySecond(connectionId).ToString(); private void Awake() { if (clientToServerTransport is LightReflectiveMirrorTransport) - { throw new Exception("Haha real funny... Use a different transport."); - } SetupCallbacks(); @@ -60,23 +68,17 @@ namespace LightReflectiveMirror return; _callbacksInitialized = true; - clientToServerTransport.OnClientConnected = ConnectedToRelay; + clientToServerTransport.OnClientConnected = OnConnectedToRelay; clientToServerTransport.OnClientDataReceived = DataReceived; clientToServerTransport.OnClientDisconnected = Disconnected; } - public override void ClientEarlyUpdate() + void Disconnected() { - clientToServerTransport.ClientEarlyUpdate(); + _connectedToRelay = false; + diconnectedFromRelay?.Invoke(); } - public override void ClientLateUpdate() - { - clientToServerTransport.ClientLateUpdate(); - } - - void Disconnected() => diconnectedFromRelay?.Invoke(); - public void ConnectToRelay() { if (!_connectedToRelay) @@ -85,6 +87,10 @@ namespace LightReflectiveMirror clientToServerTransport.ClientConnect(serverIP); } + else + { + Debug.Log("Already connected to relay!"); + } } void SendHeartbeat() @@ -97,19 +103,18 @@ namespace LightReflectiveMirror } } - void ConnectedToRelay() - { - _connectedToRelay = true; - } - public void RequestServerList() { - if (_isAuthenticated) + if (_isAuthenticated && _connectedToRelay) { int pos = 0; _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.RequestServers); clientToServerTransport.ClientSend(0, new ArraySegment(_clientSendBuffer, 0, pos)); } + else + { + Debug.Log("You must be connected to Relay to request server list!"); + } } void DataReceived(ArraySegment segmentData, int channel) @@ -171,7 +176,7 @@ namespace LightReflectiveMirror break; case OpCodes.ServerListReponse: relayServerList.Clear(); - while(data.ReadBool(ref pos)) + while (data.ReadBool(ref pos)) { relayServerList.Add(new RelayServerInfo() { @@ -241,8 +246,6 @@ namespace LightReflectiveMirror clientToServerTransport.ClientSend(0, new ArraySegment(_clientSendBuffer, 0, pos)); } - public override bool Available() => _connectedToRelay; - public override void ClientConnect(string address) { int hostId = 0; @@ -254,9 +257,7 @@ namespace LightReflectiveMirror } if (_isClient || _isServer) - { throw new Exception("Cannot connect while hosting/already connected!"); - } int pos = 0; _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.JoinServer); @@ -267,13 +268,6 @@ namespace LightReflectiveMirror clientToServerTransport.ClientSend(0, new System.ArraySegment(_clientSendBuffer, 0, pos)); } - public override void ClientConnect(Uri uri) - { - ClientConnect(uri.Host); - } - - public override bool ClientConnected() => _isClient; - public override void ClientDisconnect() { _isClient = false; @@ -294,18 +288,9 @@ namespace LightReflectiveMirror clientToServerTransport.ClientSend(channelId, new ArraySegment(_clientSendBuffer, 0, pos)); } - public override int GetMaxPacketSize(int channelId = 0) - { - return clientToServerTransport.GetMaxPacketSize(channelId); - } - - public override bool ServerActive() => _isServer; - public override bool ServerDisconnect(int connectionId) { - int relayId; - - if(_connectedRelayClients.TryGetBySecond(connectionId, out relayId)) + if (_connectedRelayClients.TryGetBySecond(connectionId, out int relayId)) { int pos = 0; _clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.KickPlayer); @@ -316,11 +301,6 @@ namespace LightReflectiveMirror return false; } - public override string ServerGetClientAddress(int connectionId) - { - return _connectedRelayClients.GetBySecond(connectionId).ToString(); - } - public override void ServerSend(int connectionId, int channelId, ArraySegment segment) { int pos = 0; @@ -339,7 +319,7 @@ namespace LightReflectiveMirror return; } - if(_isClient || _isServer) + if (_isClient || _isServer) { Debug.Log("Cannot host while already hosting or connected!"); return; @@ -373,9 +353,12 @@ namespace LightReflectiveMirror public override Uri ServerUri() { - UriBuilder builder = new UriBuilder(); - builder.Scheme = "LRM"; - builder.Host = serverId.ToString(); + UriBuilder builder = new UriBuilder + { + Scheme = "LRM", + Host = serverId.ToString() + }; + return builder.Uri; } @@ -388,7 +371,6 @@ namespace LightReflectiveMirror clientToServerTransport.Shutdown(); } - public enum OpCodes { Default = 0, RequestID = 1, JoinServer = 2, SendData = 3, GetID = 4, ServerJoined = 5, GetData = 6, CreateRoom = 7, ServerLeft = 8, PlayerDisconnected = 9, RoomCreated = 10,