From b84448ea1174d310adefb9767043cf646dad3f3d Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Thu, 8 Apr 2021 15:59:50 -0400 Subject: [PATCH] clean up --- .../LRM_LoadBalancer/Endpoint/Endpoint.cs | 54 +++++-------------- .../Endpoint/EndpointExtra.cs | 33 ++++++++++++ .../Endpoint/EndpointVariables.cs | 11 ++-- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs index 2c8b9cb..997aae3 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/Endpoint.cs @@ -36,7 +36,7 @@ namespace LightReflectiveMirror.LoadBalancing string region = req.Headers["x-Region"]; 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 + "]", ConsoleColor.Cyan); // if server is authenticated if (receivedAuthKey != null && region != null && int.TryParse(region, out int regionId) && @@ -86,22 +86,22 @@ namespace LightReflectiveMirror.LoadBalancing switch (relays[i].Key.serverRegion) { default: - case (LRMRegions.NorthAmerica): + case LRMRegions.NorthAmerica: _northAmericaServers.AddRange(requestedRooms); break; - case (LRMRegions.SouthAmerica): + case LRMRegions.SouthAmerica: _southAmericaServers.AddRange(requestedRooms); break; - case (LRMRegions.Europe): + case LRMRegions.Europe: _europeServers.AddRange(requestedRooms); break; - case (LRMRegions.Africa): + case LRMRegions.Africa: _africaServers.AddRange(requestedRooms); break; - case (LRMRegions.Asia): + case LRMRegions.Asia: _asiaServers.AddRange(requestedRooms); break; - case (LRMRegions.Oceania): + case LRMRegions.Oceania: _oceaniaServers.AddRange(requestedRooms); break; } @@ -123,6 +123,7 @@ namespace LightReflectiveMirror.LoadBalancing // need to copy over in order to avoid // collection being modified while iterating. var servers = Program.instance.availableRelayServers.ToList(); + var low = lowest; if (servers.Count == 0) { @@ -130,19 +131,17 @@ namespace LightReflectiveMirror.LoadBalancing return; } - KeyValuePair lowest = new(new RelayAddress { address = "Dummy" }, new RelayServerInfo { connectedClients = int.MaxValue }); - for (int i = 0; i < servers.Count; i++) { - if (servers[i].Value.connectedClients < lowest.Value.connectedClients) + if (servers[i].Value.connectedClients < low.Value.connectedClients) { - lowest = servers[i]; + low = servers[i]; } } // respond with the server ip // if the string is still dummy then theres no servers - await context.Response.SendResponseAsync(lowest.Key.address != "Dummy" ? JsonConvert.SerializeObject(lowest.Key) : HttpStatusCode.InternalServerError); + await context.Response.SendResponseAsync(low.Key.address != "Dummy" ? JsonConvert.SerializeObject(low.Key) : HttpStatusCode.InternalServerError); } /// @@ -209,7 +208,7 @@ namespace LightReflectiveMirror.LoadBalancing #region Startup - public class EndpointServer + public partial class EndpointServer { public bool Start(ushort port = 7070) { @@ -228,9 +227,7 @@ namespace LightReflectiveMirror.LoadBalancing }, (server) => { foreach (string ip in GetLocalIps()) - { server.Prefixes.Add($"http://{ip}:{port}/"); - } }).Build(); server.Router.Options.SendExceptionMessages = true; @@ -244,32 +241,7 @@ namespace LightReflectiveMirror.LoadBalancing } } - private static List GetLocalIps() - { - var host = Dns.GetHostEntry(Dns.GetHostName()); - List bindableIPv4Addresses = new(); - - foreach (var ip in host.AddressList) - { - if (ip.AddressFamily == AddressFamily.InterNetwork) - { - bindableIPv4Addresses.Add(ip.ToString()); - } - } - - bool hasLocal = false; - - for (int i = 0; i < bindableIPv4Addresses.Count; i++) - { - if (bindableIPv4Addresses[i] == "127.0.0.1") - hasLocal = true; - } - - if (!hasLocal) - bindableIPv4Addresses.Add("127.0.0.1"); - - return bindableIPv4Addresses; - } + #endregion } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs index 720ffec..51c8d20 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointExtra.cs @@ -1,4 +1,7 @@ using Newtonsoft.Json; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; namespace LightReflectiveMirror.LoadBalancing { @@ -26,4 +29,34 @@ namespace LightReflectiveMirror.LoadBalancing _allServers.Clear(); } } + + public partial class EndpointServer + { + public static List GetLocalIps() + { + var host = Dns.GetHostEntry(Dns.GetHostName()); + List bindableIPv4Addresses = new(); + + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork) + { + bindableIPv4Addresses.Add(ip.ToString()); + } + } + + bool hasLocal = false; + + for (int i = 0; i < bindableIPv4Addresses.Count; i++) + { + if (bindableIPv4Addresses[i] == "127.0.0.1") + hasLocal = true; + } + + if (!hasLocal) + bindableIPv4Addresses.Add("127.0.0.1"); + + return bindableIPv4Addresses; + } + } } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs index 715d52a..00ecdb7 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint/EndpointVariables.cs @@ -1,13 +1,16 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace LightReflectiveMirror.LoadBalancing { public partial class Endpoint { + /// + /// Used as a control variable for load balancer to + /// give the lowest pop. server + /// + private static readonly KeyValuePair lowest = + new(new() { address = "Dummy" }, new() { connectedClients = int.MaxValue }); + public static string allCachedServers = "[]"; public static string NorthAmericaCachedServers = "[]"; public static string SouthAmericaCachedServers = "[]";