From 3102d7f6d63041ca07ee71e4783d55cedd2f5464 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 21:02:59 -0400 Subject: [PATCH] string instead of int for serverid --- .../LRM_LoadBalancer/Endpoint.cs | 2 +- .../LRM/RelayHandler/RelayHandler.cs | 30 ++++++++++++++----- .../LRM/Room.cs | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index db0963e..1d8b538 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -84,7 +84,7 @@ namespace LightReflectiveMirror.LoadBalancing if (servers.Count == 0) { - await context.Response.SendResponseAsync(HttpStatusCode.ServiceUnavailable); + await context.Response.SendResponseAsync(HttpStatusCode.NoContent); return; } diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs index 0789853..2184e84 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs @@ -1,5 +1,6 @@ using System; using System.Buffers; +using System.Linq; namespace LightReflectiveMirror { @@ -78,15 +79,30 @@ namespace LightReflectiveMirror /// Generates a random server ID. /// /// - int GetRandomServerID() + string GetRandomServerID() { - Random rand = new Random(); - int temp = rand.Next(int.MinValue, int.MaxValue); + if (!Program.conf.UseLoadBalancer) + { + const int LENGTH = 5; + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + var randomID = ""; - while (DoesServerIdExist(temp)) - temp = rand.Next(int.MinValue, int.MaxValue); + do + { + var random = new System.Random(); + randomID = new string(Enumerable.Repeat(chars, LENGTH) + .Select(s => s[random.Next(s.Length)]).ToArray()); + } + while (DoesServerIdExist(randomID)); - return temp; + return randomID; + } + else + { + // ping load balancer here + + return ""; + } } /// @@ -94,7 +110,7 @@ namespace LightReflectiveMirror /// /// The ID to check for /// - bool DoesServerIdExist(int id) + bool DoesServerIdExist(string id) { for (int i = 0; i < rooms.Count; i++) if (rooms[i].serverId == id) diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Room.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Room.cs index 527e8ed..48acb38 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Room.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Room.cs @@ -8,7 +8,7 @@ namespace LightReflectiveMirror [JsonObject(MemberSerialization.OptOut)] public class Room { - public int serverId; + public string serverId; public int hostId; public string serverName; public string serverData;