This commit is contained in:
cxxpxr 2021-04-07 11:01:40 -04:00
parent 60f30e6b69
commit 97bd0999d3
6 changed files with 113 additions and 69 deletions

View file

@ -53,4 +53,5 @@ namespace LightReflectiveMirror.LoadBalancing
} }
public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania } public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania }
public enum LRMServerOpCode { Clear, Cache };
} }

View file

@ -16,35 +16,8 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
[RestResource] [RestResource]
public class Endpoint public partial class Endpoint
{ {
public static string allCachedServers = "[]";
public static string NorthAmericaCachedServers = "[]";
public static string SouthAmericaCachedServers = "[]";
public static string EuropeCachedServers = "[]";
public static string AsiaCachedServers = "[]";
public static string AfricaCachedServers = "[]";
public static string OceaniaCachedServers = "[]";
private static List<Room> northAmericaServers = new();
private static List<Room> southAmericaServers = new();
private static List<Room> europeServers = new();
private static List<Room> africaServers = new();
private static List<Room> asiaServers = new();
private static List<Room> oceaniaServers = new();
private static List<Room> allServers = new();
private LoadBalancerStats _stats
{
get => new()
{
nodeCount = Program.instance.availableRelayServers.Count,
uptime = DateTime.Now - Program.startupTime,
CCU = Program.instance.GetTotalCCU(),
totalServerCount = Program.instance.GetTotalServers(),
};
}
/// <summary> /// <summary>
/// Sent from an LRM server node /// Sent from an LRM server node
/// adds it to the list if authenticated. /// adds it to the list if authenticated.
@ -60,13 +33,13 @@ namespace LightReflectiveMirror.LoadBalancing
string gamePort = req.Headers["x-GamePort"]; string gamePort = req.Headers["x-GamePort"];
string publicIP = req.Headers["x-PIP"]; string publicIP = req.Headers["x-PIP"];
string region = req.Headers["x-Region"]; string region = req.Headers["x-Region"];
int regionId = 1;
string address = context.Request.RemoteEndPoint.Address.ToString(); string address = context.Request.RemoteEndPoint.Address.ToString();
Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]");
// if server is authenticated // if server is authenticated
if (receivedAuthKey != null && region != null && int.TryParse(region, out regionId) && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey) if (receivedAuthKey != null && region != null && int.TryParse(region, out int regionId) &&
address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
{ {
Logger.WriteLogMessage($"Server accepted: {address}:{gamePort}"); Logger.WriteLogMessage($"Server accepted: {address}:{gamePort}");
@ -101,63 +74,41 @@ namespace LightReflectiveMirror.LoadBalancing
if (!string.IsNullOrEmpty(auth) && auth == Program.conf.AuthKey) if (!string.IsNullOrEmpty(auth) && auth == Program.conf.AuthKey)
{ {
var relays = Program.instance.availableRelayServers.ToList(); var relays = Program.instance.availableRelayServers.ToList();
ClearAllServersLists(); PerformActionToAllServers(LRMServerOpCode.Clear);
List<Room> requestedRooms; List<Room> requestedRooms;
for (int i = 0; i < relays.Count; i++) for (int i = 0; i < relays.Count; i++)
{ {
requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort); requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort);
allServers.AddRange(requestedRooms); _allServers.AddRange(requestedRooms);
switch (relays[i].Key.serverRegion) switch (relays[i].Key.serverRegion)
{ {
case (LRMRegions.NorthAmerica): case (LRMRegions.NorthAmerica):
northAmericaServers.AddRange(requestedRooms); _northAmericaServers.AddRange(requestedRooms);
break; break;
case (LRMRegions.SouthAmerica): case (LRMRegions.SouthAmerica):
southAmericaServers.AddRange(requestedRooms); _southAmericaServers.AddRange(requestedRooms);
break; break;
case (LRMRegions.Europe): case (LRMRegions.Europe):
europeServers.AddRange(requestedRooms); _europeServers.AddRange(requestedRooms);
break; break;
case (LRMRegions.Africa): case (LRMRegions.Africa):
africaServers.AddRange(requestedRooms); _africaServers.AddRange(requestedRooms);
break; break;
case (LRMRegions.Asia): case (LRMRegions.Asia):
asiaServers.AddRange(requestedRooms); _asiaServers.AddRange(requestedRooms);
break; break;
case (LRMRegions.Oceania): case (LRMRegions.Oceania):
oceaniaServers.AddRange(requestedRooms); _oceaniaServers.AddRange(requestedRooms);
break; break;
} }
} }
CacheAllServers(); PerformActionToAllServers(LRMServerOpCode.Cache);
} }
} }
void CacheAllServers()
{
allCachedServers = JsonConvert.SerializeObject(allServers);
NorthAmericaCachedServers = JsonConvert.SerializeObject(northAmericaServers);
SouthAmericaCachedServers = JsonConvert.SerializeObject(southAmericaServers);
EuropeCachedServers = JsonConvert.SerializeObject(europeServers);
AsiaCachedServers = JsonConvert.SerializeObject(asiaServers);
AfricaCachedServers = JsonConvert.SerializeObject(africaServers);
OceaniaCachedServers = JsonConvert.SerializeObject(oceaniaServers);
}
void ClearAllServersLists()
{
northAmericaServers.Clear();
southAmericaServers.Clear();
europeServers.Clear();
asiaServers.Clear();
africaServers.Clear();
oceaniaServers.Clear();
allServers.Clear();
}
/// <summary> /// <summary>
/// Hooks into from unity side, client will call this to /// Hooks into from unity side, client will call this to
/// find the least populated server to join /// find the least populated server to join
@ -189,14 +140,7 @@ namespace LightReflectiveMirror.LoadBalancing
// respond with the server ip // respond with the server ip
// if the string is still dummy then theres no servers // if the string is still dummy then theres no servers
if (lowest.Key.address != "Dummy") await context.Response.SendResponseAsync(lowest.Key.address != "Dummy" ? JsonConvert.SerializeObject(lowest.Key) : HttpStatusCode.InternalServerError);
{
await context.Response.SendResponseAsync(JsonConvert.SerializeObject(lowest.Key));
}
else
{
await context.Response.SendResponseAsync(HttpStatusCode.InternalServerError);
}
} }
/// <summary> /// <summary>

View file

@ -0,0 +1,45 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LightReflectiveMirror.LoadBalancing
{
public partial class Endpoint
{
/// <summary>
/// We can write all server operations in here,
/// to make it more clean.
/// </summary>
/// <param name="operation"></param>
/// <param name="onComplete"></param>
public static void PerformActionToAllServers(LRMServerOpCode operation, Action onComplete = null)
{
switch (operation)
{
case LRMServerOpCode.Clear:
for (int i = 0; i < _allServersToPerformActionOn.Count; i++)
_allServersToPerformActionOn[i].Item1.Clear();
break;
// Removes the old cached string and reserialzes the new one
case LRMServerOpCode.Cache:
for (int i = 0; i < _allServersToPerformActionOn.Count; i++)
{
var tuple = _allServersToPerformActionOn[i];
var serializedData = JsonConvert.SerializeObject(_allServersToPerformActionOn[i].Item1);
_allServersToPerformActionOn.Remove(tuple);
_allServersToPerformActionOn.Add(new Tuple<List<Room>, string>(tuple.Item1, serializedData));
}
break;
default:
break;
}
}
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LightReflectiveMirror.LoadBalancing
{
public partial class Endpoint
{
public static string allCachedServers = "[]";
public static string NorthAmericaCachedServers = "[]";
public static string SouthAmericaCachedServers = "[]";
public static string EuropeCachedServers = "[]";
public static string AsiaCachedServers = "[]";
public static string AfricaCachedServers = "[]";
public static string OceaniaCachedServers = "[]";
private static List<Room> _northAmericaServers = new();
private static List<Room> _southAmericaServers = new();
private static List<Room> _europeServers = new();
private static List<Room> _africaServers = new();
private static List<Room> _asiaServers = new();
private static List<Room> _oceaniaServers = new();
private static List<Room> _allServers = new();
/// <summary>
/// This holds all the servers. It's a bit confusing,
/// but basically if we have a container for them then we
/// can shorten up methods that involve operations with all of them.
/// </summary>
private static List<Tuple<List<Room>, string>> _allServersToPerformActionOn = new()
{
new Tuple<List<Room>, string>(_northAmericaServers, NorthAmericaCachedServers),
new Tuple<List<Room>, string>(_southAmericaServers, SouthAmericaCachedServers),
new Tuple<List<Room>, string>(_europeServers, EuropeCachedServers),
new Tuple<List<Room>, string>(_africaServers, AfricaCachedServers),
new Tuple<List<Room>, string>(_asiaServers, AsiaCachedServers),
new Tuple<List<Room>, string>(_oceaniaServers, OceaniaCachedServers),
new Tuple<List<Room>, string>(_allServers, allCachedServers),
};
private LoadBalancerStats _stats
{
get => new()
{
nodeCount = Program.instance.availableRelayServers.Count,
uptime = DateTime.Now - Program.startupTime,
CCU = Program.instance.GetTotalCCU(),
totalServerCount = Program.instance.GetTotalServers(),
};
}
}
}