From e2833d53bb0f1f87e3245b912e3fa74ca157ee7a Mon Sep 17 00:00:00 2001
From: cxxpxr <60411087+cxxpxr@users.noreply.github.com>
Date: Sun, 4 Apr 2021 23:58:23 -0400
Subject: [PATCH] Added join requests
---
.../Endpoint.cs | 36 +++++++++++++++++++
.../Program.cs | 22 ++++++------
2 files changed, 47 insertions(+), 11 deletions(-)
diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs
index ce65f6f..da8bb8c 100644
--- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs
+++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs
@@ -3,6 +3,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
+using System.Collections.Generic;
+using System.Linq;
using System.Threading.Tasks;
namespace LightReflectiveMirror.LoadBalancing
@@ -28,6 +30,40 @@ namespace LightReflectiveMirror.LoadBalancing
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
}
+
+ ///
+ /// Hooks into from unity side, client will call this to
+ /// find the least populated server to join
+ ///
+ ///
+ ///
+ [RestRoute("Get", "/api/join/")]
+ public async Task JoinRelay(IHttpContext context)
+ {
+ var servers = Program.instance.availableRelayServers.ToList();
+
+ if(servers.Count == 0)
+ {
+ await context.Response.SendResponseAsync(HttpStatusCode.ServiceUnavailable);
+ 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++)
+ {
+ if (servers[i].Value.ConnectedClients < lowest.Value.ConnectedClients)
+ {
+ lowest = servers[i];
+ }
+ }
+
+ // respond with the server ip
+ // if the string is still dummy then theres no servers
+ await context.Response.SendResponseAsync(lowest.Key != "Dummy" ? lowest.Key : HttpStatusCode.InternalServerError);
+ }
}
public class EndpointServer
diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs
index b94476c..5e9382d 100644
--- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs
+++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/Program.cs
@@ -12,7 +12,7 @@ namespace LightReflectiveMirror.LoadBalancing
{
///
/// Keeps track of all available relays.
- /// Key is server address, value is CCU/Info.
+ /// Key is server address, value is CCU.
///
public Dictionary availableRelayServers = new();
@@ -58,13 +58,13 @@ namespace LightReflectiveMirror.LoadBalancing
public async Task AddServer(string serverIP)
{
- var stats = await InitialPingServer(serverIP);
+ var stats = await ManualPingServer(serverIP);
if(stats.PublicRoomCount != -1)
availableRelayServers.Add(serverIP, stats);
}
- async Task InitialPingServer(string serverIP)
+ async Task ManualPingServer(string serverIP)
{
string url = serverIP + API_PATH;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
@@ -145,14 +145,14 @@ namespace LightReflectiveMirror.LoadBalancing
Console.WriteLine(message);
}
- [Serializable]
- public struct RelayStats
- {
- public int ConnectedClients;
- public int RoomCount;
- public int PublicRoomCount;
- public TimeSpan Uptime;
- }
+ }
+ [Serializable]
+ public struct RelayStats
+ {
+ public int ConnectedClients;
+ public int RoomCount;
+ public int PublicRoomCount;
+ public TimeSpan Uptime;
}
}