From 7ea91c9a9055748739bd85247151ce22746c64e9 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Mon, 5 Apr 2021 13:13:00 -0400 Subject: [PATCH] pinging --- .../Config.cs | 3 ++- .../Endpoint.cs | 17 +++++++------ .../LRM_LoadBalancer.sln | 25 +++++++++++++++++++ .../Program.cs | 11 +++++--- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer.sln diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Config.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Config.cs index 5a72803..639a869 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Config.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Config.cs @@ -10,6 +10,7 @@ namespace LightReflectiveMirror.LoadBalancing { public int ConnectedServerPingRate = 10000; public string AuthKey = "AuthKey"; - public ushort EndpointPort = 8080; + public ushort EndpointPort = 7070; + public ushort RelayEndpointPort = 8080; } } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs index da8bb8c..c7e3808 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs @@ -16,15 +16,18 @@ namespace LightReflectiveMirror.LoadBalancing public async Task ReceiveAuthKey(IHttpContext context) { var req = context.Request.Headers; + string receivedAuthKey = req[0]; + string address = req[1]; + + Console.WriteLine("Received auth req [" + req[0] + "] == [" + Program.conf.AuthKey+"]"); // if server is authenticated - if (req[0] == Program.conf.AuthKey) + if (receivedAuthKey == Program.conf.AuthKey) { - var address = context.Request.RemoteEndPoint.Address.ToString(); - await Program.instance.AddServer(address); - Console.WriteLine("Server accepted: " + address); + await Program.instance.AddServer(address); + await context.Response.SendResponseAsync(HttpStatusCode.Ok); } else @@ -40,6 +43,8 @@ namespace LightReflectiveMirror.LoadBalancing [RestRoute("Get", "/api/join/")] public async Task JoinRelay(IHttpContext context) { + // need to copy over in order to avoid + // collection being modified while iterating. var servers = Program.instance.availableRelayServers.ToList(); if(servers.Count == 0) @@ -48,8 +53,6 @@ namespace LightReflectiveMirror.LoadBalancing return; } - // need to copy over in order to avoid - // collection being modified while iterating. KeyValuePair lowest = new("Dummy", new RelayStats { ConnectedClients = int.MaxValue }); for (int i = 0; i < servers.Count; i++) @@ -68,7 +71,7 @@ namespace LightReflectiveMirror.LoadBalancing public class EndpointServer { - public bool Start(ushort port = 8080) + public bool Start(ushort port = 7070) { try { diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer.sln b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer.sln new file mode 100644 index 0000000..bd96b3b --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31025.194 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LRM_LoadBalancer", "LRM_LoadBalancer\LRM_LoadBalancer.csproj", "{B9B714BE-27A8-4815-933D-C122CA431750}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {881733E9-B7F1-471A-9651-19257E8C56E7} + EndGlobalSection +EndGlobal diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs index 5e9382d..e15b721 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs @@ -66,8 +66,7 @@ namespace LightReflectiveMirror.LoadBalancing async Task ManualPingServer(string serverIP) { - string url = serverIP + API_PATH; - HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); + HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://"+serverIP); try { @@ -76,11 +75,11 @@ namespace LightReflectiveMirror.LoadBalancing return JsonConvert.DeserializeObject(reader.ReadToEnd()); } - catch (Exception ex) + catch (WebException ex) { // server doesnt exist anymore probably // do more shit here - + return new RelayStats { PublicRoomCount = -1 }; } } @@ -89,6 +88,8 @@ namespace LightReflectiveMirror.LoadBalancing { while (true) { + WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays"); + foreach (var server in availableRelayServers) { string url = server + API_PATH; @@ -100,6 +101,7 @@ namespace LightReflectiveMirror.LoadBalancing var reader = new StreamReader(response.GetResponseStream()); + WriteLogMessage("Server " + server.Key + " still exists, keeping in collection."); availableRelayServers.Remove(server.Key); availableRelayServers.Add(server.Key, JsonConvert.DeserializeObject(reader.ReadToEnd())); } @@ -108,6 +110,7 @@ namespace LightReflectiveMirror.LoadBalancing // server doesnt exist anymore probably // do more shit here + WriteLogMessage("Server " + server.Key + " does not exist anymore, removing", ConsoleColor.Red); availableRelayServers.Remove(server.Key); } }