fixes yeah
This commit is contained in:
parent
cce933d674
commit
6bb6511188
7 changed files with 46 additions and 41 deletions
|
|
@ -81,6 +81,9 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
private async Task HeartbeatLoop()
|
private async Task HeartbeatLoop()
|
||||||
{
|
{
|
||||||
|
// default heartbeat data
|
||||||
|
byte[] heartbeat = new byte[] { 200 };
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -100,7 +103,7 @@ namespace LightReflectiveMirror
|
||||||
_currentHeartbeatTimer = 0;
|
_currentHeartbeatTimer = 0;
|
||||||
|
|
||||||
for (int i = 0; i < _currentConnections.Count; i++)
|
for (int i = 0; i < _currentConnections.Count; i++)
|
||||||
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 }));
|
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(heartbeat));
|
||||||
|
|
||||||
if (conf.UseLoadBalancer)
|
if (conf.UseLoadBalancer)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ namespace LightReflectiveMirror
|
||||||
const int LENGTH = 5;
|
const int LENGTH = 5;
|
||||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
var randomID = "";
|
var randomID = "";
|
||||||
var random = new Random();
|
var random = _cachedRandom;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ namespace LightReflectiveMirror
|
||||||
private int _maxPacketSize = 0;
|
private int _maxPacketSize = 0;
|
||||||
private Dictionary<int, Room> _cachedClientRooms = new();
|
private Dictionary<int, Room> _cachedClientRooms = new();
|
||||||
private Dictionary<string, Room> _cachedRooms = new();
|
private Dictionary<string, Room> _cachedRooms = new();
|
||||||
|
|
||||||
|
private System.Random _cachedRandom = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum OpCodes
|
public enum OpCodes
|
||||||
|
|
|
||||||
|
|
@ -207,14 +207,6 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if !NET_4_6
|
|
||||||
EditorGUILayout.BeginVertical("Box");
|
|
||||||
EditorGUILayout.HelpBox("For LRM to function properly, it needs the API Compatibility to be at 4.x", MessageType.Error);
|
|
||||||
if (GUILayout.Button("Change to 4.x"))
|
|
||||||
PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Standalone, ApiCompatibilityLevel.NET_4_6);
|
|
||||||
EditorGUILayout.EndVertical();
|
|
||||||
EditorGUILayout.Space();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
currentTab = GUILayout.Toolbar(currentTab, tabs);
|
currentTab = GUILayout.Toolbar(currentTab, tabs);
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
|
|
@ -305,7 +297,8 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("diconnectedFromRelay"));
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("connectedToRelay"));
|
||||||
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("disconnectedFromRelay"));
|
||||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("serverListUpdated"));
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("serverListUpdated"));
|
||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -95,13 +95,13 @@ namespace LightReflectiveMirror
|
||||||
DisconnectFromRelay();
|
DisconnectFromRelay();
|
||||||
|
|
||||||
while (IsAuthenticated())
|
while (IsAuthenticated())
|
||||||
yield return new WaitForEndOfFrame();
|
yield return null;
|
||||||
|
|
||||||
endpointServerPort = room.relayInfo.endpointPort;
|
endpointServerPort = room.relayInfo.endpointPort;
|
||||||
Connect(room.relayInfo.address, room.relayInfo.port);
|
Connect(room.relayInfo.address, room.relayInfo.port);
|
||||||
|
|
||||||
while (!IsAuthenticated())
|
while (!IsAuthenticated())
|
||||||
yield return new WaitForEndOfFrame();
|
yield return null;
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
_directConnected = false;
|
_directConnected = false;
|
||||||
|
|
@ -109,10 +109,9 @@ namespace LightReflectiveMirror
|
||||||
_clientSendBuffer.WriteString(ref pos, room.serverId);
|
_clientSendBuffer.WriteString(ref pos, room.serverId);
|
||||||
_clientSendBuffer.WriteBool(ref pos, _directConnectModule != null);
|
_clientSendBuffer.WriteBool(ref pos, _directConnectModule != null);
|
||||||
|
|
||||||
if (GetLocalIp() == null)
|
string local = GetLocalIp();
|
||||||
_clientSendBuffer.WriteString(ref pos, "0.0.0.0");
|
|
||||||
else
|
_clientSendBuffer.WriteString(ref pos, local ?? "0.0.0.0");
|
||||||
_clientSendBuffer.WriteString(ref pos, GetLocalIp());
|
|
||||||
|
|
||||||
_isClient = true;
|
_isClient = true;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,14 @@ namespace LightReflectiveMirror
|
||||||
public float heartBeatInterval = 3;
|
public float heartBeatInterval = 3;
|
||||||
public bool connectOnAwake = true;
|
public bool connectOnAwake = true;
|
||||||
public string authenticationKey = "Secret Auth Key";
|
public string authenticationKey = "Secret Auth Key";
|
||||||
public UnityEvent diconnectedFromRelay;
|
|
||||||
|
public UnityEvent disconnectedFromRelay;
|
||||||
|
public UnityEvent connectedToRelay;
|
||||||
|
|
||||||
// NAT Puncher variables
|
// NAT Puncher variables
|
||||||
public bool useNATPunch = false;
|
public bool useNATPunch = false;
|
||||||
public int NATPunchtroughPort = -1;
|
public int NATPunchtroughPort = -1;
|
||||||
|
private const int NAT_PUNCH_ATTEMPTS = 3;
|
||||||
|
|
||||||
// LLB variables (LRM Load Balancer)
|
// LLB variables (LRM Load Balancer)
|
||||||
public bool useLoadBalancer = false;
|
public bool useLoadBalancer = false;
|
||||||
|
|
@ -33,6 +36,8 @@ namespace LightReflectiveMirror
|
||||||
public int maxServerPlayers = 10;
|
public int maxServerPlayers = 10;
|
||||||
public bool isPublicServer = true;
|
public bool isPublicServer = true;
|
||||||
|
|
||||||
|
private const string LOCALHOST = "127.0.0.1";
|
||||||
|
|
||||||
// Server list variables
|
// Server list variables
|
||||||
public UnityEvent serverListUpdated;
|
public UnityEvent serverListUpdated;
|
||||||
public List<Room> relayServerList { private set; get; } = new List<Room>();
|
public List<Room> relayServerList { private set; get; } = new List<Room>();
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,9 @@ namespace LightReflectiveMirror
|
||||||
public partial class LightReflectiveMirrorTransport : Transport
|
public partial class LightReflectiveMirrorTransport : Transport
|
||||||
{
|
{
|
||||||
public bool IsAuthenticated() => _isAuthenticated;
|
public bool IsAuthenticated() => _isAuthenticated;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
#if !NET_4_6
|
|
||||||
throw new Exception("LRM | Please switch to .NET 4.x for LRM to function properly!");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (Application.platform == RuntimePlatform.WebGLPlayer)
|
if (Application.platform == RuntimePlatform.WebGLPlayer)
|
||||||
useNATPunch = false;
|
useNATPunch = false;
|
||||||
else
|
else
|
||||||
|
|
@ -56,17 +53,18 @@ namespace LightReflectiveMirror
|
||||||
clientToServerTransport.OnClientError = (e) => Debug.LogException(e);
|
clientToServerTransport.OnClientError = (e) => Debug.LogException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Disconnected()
|
private void Disconnected()
|
||||||
{
|
{
|
||||||
_connectedToRelay = false;
|
_connectedToRelay = false;
|
||||||
_isAuthenticated = false;
|
_isAuthenticated = false;
|
||||||
diconnectedFromRelay?.Invoke();
|
disconnectedFromRelay?.Invoke();
|
||||||
serverStatus = "Disconnected from relay.";
|
serverStatus = "Disconnected from relay.";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnConnectedToRelay()
|
private void OnConnectedToRelay()
|
||||||
{
|
{
|
||||||
_connectedToRelay = true;
|
_connectedToRelay = true;
|
||||||
|
connectedToRelay?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConnectToRelay()
|
public void ConnectToRelay()
|
||||||
|
|
@ -121,7 +119,7 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendHeartbeat()
|
private void SendHeartbeat()
|
||||||
{
|
{
|
||||||
if (_connectedToRelay)
|
if (_connectedToRelay)
|
||||||
{
|
{
|
||||||
|
|
@ -161,7 +159,7 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataReceived(ArraySegment<byte> segmentData, int channel)
|
private void DataReceived(ArraySegment<byte> segmentData, int channel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -178,11 +176,13 @@ namespace LightReflectiveMirror
|
||||||
_isAuthenticated = true;
|
_isAuthenticated = true;
|
||||||
RequestServerList();
|
RequestServerList();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.AuthenticationRequest:
|
case OpCodes.AuthenticationRequest:
|
||||||
// Server requested that we send an authentication request, lets send our auth key.
|
// Server requested that we send an authentication request, lets send our auth key.
|
||||||
serverStatus = "Sent authentication to relay...";
|
serverStatus = "Sent authentication to relay...";
|
||||||
SendAuthKey();
|
SendAuthKey();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.GetData:
|
case OpCodes.GetData:
|
||||||
// Someone sent us a packet from their mirror over the relay
|
// Someone sent us a packet from their mirror over the relay
|
||||||
var recvData = data.ReadBytes(ref pos);
|
var recvData = data.ReadBytes(ref pos);
|
||||||
|
|
@ -198,6 +198,7 @@ namespace LightReflectiveMirror
|
||||||
if (_isClient)
|
if (_isClient)
|
||||||
OnClientDataReceived?.Invoke(new ArraySegment<byte>(recvData), channel);
|
OnClientDataReceived?.Invoke(new ArraySegment<byte>(recvData), channel);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.ServerLeft:
|
case OpCodes.ServerLeft:
|
||||||
// Called when we were kicked, or server was closed.
|
// Called when we were kicked, or server was closed.
|
||||||
if (_isClient)
|
if (_isClient)
|
||||||
|
|
@ -206,6 +207,7 @@ namespace LightReflectiveMirror
|
||||||
OnClientDisconnected?.Invoke();
|
OnClientDisconnected?.Invoke();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.PlayerDisconnected:
|
case OpCodes.PlayerDisconnected:
|
||||||
// Called when another player left the room.
|
// Called when another player left the room.
|
||||||
if (_isServer)
|
if (_isServer)
|
||||||
|
|
@ -219,10 +221,12 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.RoomCreated:
|
case OpCodes.RoomCreated:
|
||||||
// We successfully created the room, the server also gave us the serverId of the room!
|
// We successfully created the room, the server also gave us the serverId of the room!
|
||||||
serverId = data.ReadString(ref pos);
|
serverId = data.ReadString(ref pos);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.ServerJoined:
|
case OpCodes.ServerJoined:
|
||||||
// Called when a player joins the room or when we joined a room.
|
// Called when a player joins the room or when we joined a room.
|
||||||
int clientId = data.ReadInt(ref pos);
|
int clientId = data.ReadInt(ref pos);
|
||||||
|
|
@ -239,6 +243,7 @@ namespace LightReflectiveMirror
|
||||||
_currentMemberId++;
|
_currentMemberId++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.DirectConnectIP:
|
case OpCodes.DirectConnectIP:
|
||||||
// Either a client is trying to join us via NAT Punch, or we are trying to join a host over NAT punch/Direct connect.
|
// Either a client is trying to join us via NAT Punch, or we are trying to join a host over NAT punch/Direct connect.
|
||||||
var ip = data.ReadString(ref pos);
|
var ip = data.ReadString(ref pos);
|
||||||
|
|
@ -264,16 +269,17 @@ namespace LightReflectiveMirror
|
||||||
|
|
||||||
if (useNATPunch && attemptNatPunch)
|
if (useNATPunch && attemptNatPunch)
|
||||||
{
|
{
|
||||||
if (ip == "127.0.0.1")
|
if (ip == LOCALHOST)
|
||||||
_directConnectModule.JoinServer("127.0.0.1", port + 1);
|
_directConnectModule.JoinServer(LOCALHOST, port + 1);
|
||||||
else
|
else
|
||||||
_directConnectModule.JoinServer("127.0.0.1", _NATIP.Port - 1);
|
_directConnectModule.JoinServer(LOCALHOST, _NATIP.Port - 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_directConnectModule.JoinServer(ip, port);
|
_directConnectModule.JoinServer(ip, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpCodes.RequestNATConnection:
|
case OpCodes.RequestNATConnection:
|
||||||
// Called when the LRM node would like us to establish a NAT puncher connection. Its safe to ignore if NAT punch is disabled.
|
// Called when the LRM node would like us to establish a NAT puncher connection. Its safe to ignore if NAT punch is disabled.
|
||||||
if (useNATPunch && GetLocalIp() != null && _directConnectModule != null)
|
if (useNATPunch && GetLocalIp() != null && _directConnectModule != null)
|
||||||
|
|
@ -301,16 +307,14 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!IPAddress.TryParse(serverIP, out IPAddress serverAddr))
|
if (!IPAddress.TryParse(serverIP, out IPAddress serverAddr))
|
||||||
serverAddr = Dns.GetHostEntry(serverIP).AddressList[0];
|
serverAddr = Dns.GetHostEntry(serverIP).AddressList[0];
|
||||||
|
|
||||||
_relayPuncherIP = new IPEndPoint(serverAddr, NATPunchtroughPort);
|
_relayPuncherIP = new IPEndPoint(serverAddr, NATPunchtroughPort);
|
||||||
|
|
||||||
// Send 3 to lower chance of it being dropped or corrupted when received on server.
|
for (int attempts = 0; attempts < NAT_PUNCH_ATTEMPTS; attempts++)
|
||||||
_NATPuncher.Send(initalData, sendPos, _relayPuncherIP);
|
|
||||||
_NATPuncher.Send(initalData, sendPos, _relayPuncherIP);
|
|
||||||
_NATPuncher.Send(initalData, sendPos, _relayPuncherIP);
|
_NATPuncher.Send(initalData, sendPos, _relayPuncherIP);
|
||||||
|
|
||||||
_NATPuncher.BeginReceive(new AsyncCallback(RecvData), _NATPuncher);
|
_NATPuncher.BeginReceive(new AsyncCallback(RecvData), _NATPuncher);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -414,7 +418,7 @@ namespace LightReflectiveMirror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Room? GetServerForID(string serverID)
|
private Room? GetServerForID(string serverID)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < relayServerList.Count; i++)
|
for (int i = 0; i < relayServerList.Count; i++)
|
||||||
{
|
{
|
||||||
|
|
@ -425,7 +429,7 @@ namespace LightReflectiveMirror
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendAuthKey()
|
private void SendAuthKey()
|
||||||
{
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
_clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.AuthenticationResponse);
|
_clientSendBuffer.WriteByte(ref pos, (byte)OpCodes.AuthenticationResponse);
|
||||||
|
|
@ -481,5 +485,4 @@ namespace LightReflectiveMirror
|
||||||
public string address;
|
public string address;
|
||||||
public LRMRegions serverRegion;
|
public LRMRegions serverRegion;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue