Merge pull request #7 from cxxpxr/main

Unity side refactor
This commit is contained in:
Derek S 2021-03-29 16:01:27 -05:00 committed by GitHub
commit e5cc61cb03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -37,14 +37,22 @@ namespace LightReflectiveMirror
private int _currentMemberId;
private bool _callbacksInitialized = false;
private BiDictionary<int, int> _connectedRelayClients = new BiDictionary<int, int>();
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,18 @@ 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;
_isAuthenticated = false;
diconnectedFromRelay?.Invoke();
}
public override void ClientLateUpdate()
{
clientToServerTransport.ClientLateUpdate();
}
void Disconnected() => diconnectedFromRelay?.Invoke();
public void ConnectToRelay()
{
if (!_connectedToRelay)
@ -85,6 +88,10 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientConnect(serverIP);
}
else
{
Debug.Log("Already connected to relay!");
}
}
void SendHeartbeat()
@ -97,19 +104,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<byte>(_clientSendBuffer, 0, pos));
}
else
{
Debug.Log("You must be connected to Relay to request server list!");
}
}
void DataReceived(ArraySegment<byte> segmentData, int channel)
@ -241,8 +247,6 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientSend(0, new ArraySegment<byte>(_clientSendBuffer, 0, pos));
}
public override bool Available() => _connectedToRelay;
public override void ClientConnect(string address)
{
int hostId = 0;
@ -254,9 +258,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 +269,6 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientSend(0, new System.ArraySegment<byte>(_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 +289,9 @@ namespace LightReflectiveMirror
clientToServerTransport.ClientSend(channelId, new ArraySegment<byte>(_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 +302,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<byte> segment)
{
int pos = 0;
@ -373,9 +354,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 +372,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,