string instead of int for serverid

This commit is contained in:
cxxpxr 2021-04-06 21:02:59 -04:00
parent 3da331e048
commit 3102d7f6d6
3 changed files with 25 additions and 9 deletions

View file

@ -84,7 +84,7 @@ namespace LightReflectiveMirror.LoadBalancing
if (servers.Count == 0) if (servers.Count == 0)
{ {
await context.Response.SendResponseAsync(HttpStatusCode.ServiceUnavailable); await context.Response.SendResponseAsync(HttpStatusCode.NoContent);
return; return;
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Linq;
namespace LightReflectiveMirror namespace LightReflectiveMirror
{ {
@ -78,15 +79,30 @@ namespace LightReflectiveMirror
/// Generates a random server ID. /// Generates a random server ID.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
int GetRandomServerID() string GetRandomServerID()
{ {
Random rand = new Random(); if (!Program.conf.UseLoadBalancer)
int temp = rand.Next(int.MinValue, int.MaxValue); {
const int LENGTH = 5;
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var randomID = "";
while (DoesServerIdExist(temp)) do
temp = rand.Next(int.MinValue, int.MaxValue); {
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 "";
}
} }
/// <summary> /// <summary>
@ -94,7 +110,7 @@ namespace LightReflectiveMirror
/// </summary> /// </summary>
/// <param name="id">The ID to check for</param> /// <param name="id">The ID to check for</param>
/// <returns></returns> /// <returns></returns>
bool DoesServerIdExist(int id) bool DoesServerIdExist(string id)
{ {
for (int i = 0; i < rooms.Count; i++) for (int i = 0; i < rooms.Count; i++)
if (rooms[i].serverId == id) if (rooms[i].serverId == id)

View file

@ -8,7 +8,7 @@ namespace LightReflectiveMirror
[JsonObject(MemberSerialization.OptOut)] [JsonObject(MemberSerialization.OptOut)]
public class Room public class Room
{ {
public int serverId; public string serverId;
public int hostId; public int hostId;
public string serverName; public string serverName;
public string serverData; public string serverData;