diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs index 71217b11..0ef08d9d 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs @@ -53,4 +53,5 @@ public struct Room } public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania } + public enum LRMServerOpCode { Clear, Cache }; } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs similarity index 75% rename from LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs rename to LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs index 9cafd479..bc73d8a0 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs @@ -16,35 +16,8 @@ namespace LightReflectiveMirror.LoadBalancing { [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 northAmericaServers = new(); - private static List southAmericaServers = new(); - private static List europeServers = new(); - private static List africaServers = new(); - private static List asiaServers = new(); - private static List oceaniaServers = new(); - private static List 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(), - }; - } - /// /// Sent from an LRM server node /// adds it to the list if authenticated. @@ -60,13 +33,13 @@ public async Task ReceiveAuthKey(IHttpContext context) string gamePort = req.Headers["x-GamePort"]; string publicIP = req.Headers["x-PIP"]; string region = req.Headers["x-Region"]; - int regionId = 1; string address = context.Request.RemoteEndPoint.Address.ToString(); Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]"); // 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}"); @@ -101,63 +74,41 @@ public async Task ServerListUpdate(IHttpContext context) if (!string.IsNullOrEmpty(auth) && auth == Program.conf.AuthKey) { var relays = Program.instance.availableRelayServers.ToList(); - ClearAllServersLists(); + PerformActionToAllServers(LRMServerOpCode.Clear); List requestedRooms; for (int i = 0; i < relays.Count; i++) { requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort); - allServers.AddRange(requestedRooms); + _allServers.AddRange(requestedRooms); switch (relays[i].Key.serverRegion) { case (LRMRegions.NorthAmerica): - northAmericaServers.AddRange(requestedRooms); + _northAmericaServers.AddRange(requestedRooms); break; case (LRMRegions.SouthAmerica): - southAmericaServers.AddRange(requestedRooms); + _southAmericaServers.AddRange(requestedRooms); break; case (LRMRegions.Europe): - europeServers.AddRange(requestedRooms); + _europeServers.AddRange(requestedRooms); break; case (LRMRegions.Africa): - africaServers.AddRange(requestedRooms); + _africaServers.AddRange(requestedRooms); break; case (LRMRegions.Asia): - asiaServers.AddRange(requestedRooms); + _asiaServers.AddRange(requestedRooms); break; case (LRMRegions.Oceania): - oceaniaServers.AddRange(requestedRooms); + _oceaniaServers.AddRange(requestedRooms); 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(); - } - /// /// Hooks into from unity side, client will call this to /// find the least populated server to join @@ -189,14 +140,7 @@ public async Task JoinRelay(IHttpContext context) // respond with the server ip // if the string is still dummy then theres no servers - if (lowest.Key.address != "Dummy") - { - await context.Response.SendResponseAsync(JsonConvert.SerializeObject(lowest.Key)); - } - else - { - await context.Response.SendResponseAsync(HttpStatusCode.InternalServerError); - } + await context.Response.SendResponseAsync(lowest.Key.address != "Dummy" ? JsonConvert.SerializeObject(lowest.Key) : HttpStatusCode.InternalServerError); } /// diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs new file mode 100644 index 00000000..1f3f6b09 --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs @@ -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 + { + /// + /// We can write all server operations in here, + /// to make it more clean. + /// + /// + /// + 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, string>(tuple.Item1, serializedData)); + } + break; + default: + break; + } + + + } + } +} diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs new file mode 100644 index 00000000..4f6db50b --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs @@ -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 _northAmericaServers = new(); + private static List _southAmericaServers = new(); + private static List _europeServers = new(); + private static List _africaServers = new(); + private static List _asiaServers = new(); + private static List _oceaniaServers = new(); + private static List _allServers = new(); + + /// + /// 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. + /// + private static List, string>> _allServersToPerformActionOn = new() + { + new Tuple, string>(_northAmericaServers, NorthAmericaCachedServers), + new Tuple, string>(_southAmericaServers, SouthAmericaCachedServers), + new Tuple, string>(_europeServers, EuropeCachedServers), + new Tuple, string>(_africaServers, AfricaCachedServers), + new Tuple, string>(_asiaServers, AsiaCachedServers), + new Tuple, string>(_oceaniaServers, OceaniaCachedServers), + new Tuple, 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(), + }; + } + } +} diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program/Program.cs similarity index 100% rename from LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs rename to LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program/Program.cs diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program/ProgramExtra.cs similarity index 100% rename from LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs rename to LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program/ProgramExtra.cs