Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxpxr committed Apr 7, 2021
1 parent 60f30e6 commit 97bd099
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public struct Room
}

public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania }
public enum LRMServerOpCode { Clear, Cache };
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,8 @@ namespace LightReflectiveMirror.LoadBalancing
{

[RestResource]
public class Endpoint
public partial class Endpoint
{
public static string allCachedServers = "[]";
public static string NorthAmericaCachedServers = "[]";
public static string SouthAmericaCachedServers = "[]";
public static string EuropeCachedServers = "[]";
public static string AsiaCachedServers = "[]";
public static string AfricaCachedServers = "[]";
public static string OceaniaCachedServers = "[]";

private static List<Room> northAmericaServers = new();
private static List<Room> southAmericaServers = new();
private static List<Room> europeServers = new();
private static List<Room> africaServers = new();
private static List<Room> asiaServers = new();
private static List<Room> oceaniaServers = new();
private static List<Room> allServers = new();

private LoadBalancerStats _stats
{
get => new()
{
nodeCount = Program.instance.availableRelayServers.Count,
uptime = DateTime.Now - Program.startupTime,
CCU = Program.instance.GetTotalCCU(),
totalServerCount = Program.instance.GetTotalServers(),
};
}

/// <summary>
/// Sent from an LRM server node
/// adds it to the list if authenticated.
Expand All @@ -60,13 +33,13 @@ public async Task ReceiveAuthKey(IHttpContext context)
string gamePort = req.Headers["x-GamePort"];
string publicIP = req.Headers["x-PIP"];
string region = req.Headers["x-Region"];
int regionId = 1;

string address = context.Request.RemoteEndPoint.Address.ToString();
Logger.WriteLogMessage("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]");

// if server is authenticated
if (receivedAuthKey != null && region != null && int.TryParse(region, out regionId) && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
if (receivedAuthKey != null && region != null && int.TryParse(region, out int regionId) &&
address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
{
Logger.WriteLogMessage($"Server accepted: {address}:{gamePort}");

Expand Down Expand Up @@ -101,63 +74,41 @@ public async Task ServerListUpdate(IHttpContext context)
if (!string.IsNullOrEmpty(auth) && auth == Program.conf.AuthKey)
{
var relays = Program.instance.availableRelayServers.ToList();
ClearAllServersLists();
PerformActionToAllServers(LRMServerOpCode.Clear);
List<Room> requestedRooms;

for (int i = 0; i < relays.Count; i++)
{
requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort);
allServers.AddRange(requestedRooms);
_allServers.AddRange(requestedRooms);

switch (relays[i].Key.serverRegion)
{
case (LRMRegions.NorthAmerica):
northAmericaServers.AddRange(requestedRooms);
_northAmericaServers.AddRange(requestedRooms);
break;
case (LRMRegions.SouthAmerica):
southAmericaServers.AddRange(requestedRooms);
_southAmericaServers.AddRange(requestedRooms);
break;
case (LRMRegions.Europe):
europeServers.AddRange(requestedRooms);
_europeServers.AddRange(requestedRooms);
break;
case (LRMRegions.Africa):
africaServers.AddRange(requestedRooms);
_africaServers.AddRange(requestedRooms);
break;
case (LRMRegions.Asia):
asiaServers.AddRange(requestedRooms);
_asiaServers.AddRange(requestedRooms);
break;
case (LRMRegions.Oceania):
oceaniaServers.AddRange(requestedRooms);
_oceaniaServers.AddRange(requestedRooms);
break;
}
}

CacheAllServers();
PerformActionToAllServers(LRMServerOpCode.Cache);
}
}

void CacheAllServers()
{
allCachedServers = JsonConvert.SerializeObject(allServers);
NorthAmericaCachedServers = JsonConvert.SerializeObject(northAmericaServers);
SouthAmericaCachedServers = JsonConvert.SerializeObject(southAmericaServers);
EuropeCachedServers = JsonConvert.SerializeObject(europeServers);
AsiaCachedServers = JsonConvert.SerializeObject(asiaServers);
AfricaCachedServers = JsonConvert.SerializeObject(africaServers);
OceaniaCachedServers = JsonConvert.SerializeObject(oceaniaServers);
}

void ClearAllServersLists()
{
northAmericaServers.Clear();
southAmericaServers.Clear();
europeServers.Clear();
asiaServers.Clear();
africaServers.Clear();
oceaniaServers.Clear();
allServers.Clear();
}

/// <summary>
/// Hooks into from unity side, client will call this to
/// find the least populated server to join
Expand Down Expand Up @@ -189,14 +140,7 @@ public async Task JoinRelay(IHttpContext context)

// respond with the server ip
// if the string is still dummy then theres no servers
if (lowest.Key.address != "Dummy")
{
await context.Response.SendResponseAsync(JsonConvert.SerializeObject(lowest.Key));
}
else
{
await context.Response.SendResponseAsync(HttpStatusCode.InternalServerError);
}
await context.Response.SendResponseAsync(lowest.Key.address != "Dummy" ? JsonConvert.SerializeObject(lowest.Key) : HttpStatusCode.InternalServerError);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightReflectiveMirror.LoadBalancing
{
public partial class Endpoint
{
/// <summary>
/// We can write all server operations in here,
/// to make it more clean.
/// </summary>
/// <param name="operation"></param>
/// <param name="onComplete"></param>
public static void PerformActionToAllServers(LRMServerOpCode operation, Action onComplete = null)
{
switch (operation)
{
case LRMServerOpCode.Clear:
for (int i = 0; i < _allServersToPerformActionOn.Count; i++)
_allServersToPerformActionOn[i].Item1.Clear();
break;

// Removes the old cached string and reserialzes the new one
case LRMServerOpCode.Cache:
for (int i = 0; i < _allServersToPerformActionOn.Count; i++)
{
var tuple = _allServersToPerformActionOn[i];
var serializedData = JsonConvert.SerializeObject(_allServersToPerformActionOn[i].Item1);

_allServersToPerformActionOn.Remove(tuple);
_allServersToPerformActionOn.Add(new Tuple<List<Room>, string>(tuple.Item1, serializedData));
}
break;
default:
break;
}


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightReflectiveMirror.LoadBalancing
{
public partial class Endpoint
{
public static string allCachedServers = "[]";
public static string NorthAmericaCachedServers = "[]";
public static string SouthAmericaCachedServers = "[]";
public static string EuropeCachedServers = "[]";
public static string AsiaCachedServers = "[]";
public static string AfricaCachedServers = "[]";
public static string OceaniaCachedServers = "[]";

private static List<Room> _northAmericaServers = new();
private static List<Room> _southAmericaServers = new();
private static List<Room> _europeServers = new();
private static List<Room> _africaServers = new();
private static List<Room> _asiaServers = new();
private static List<Room> _oceaniaServers = new();
private static List<Room> _allServers = new();

/// <summary>
/// This holds all the servers. It's a bit confusing,
/// but basically if we have a container for them then we
/// can shorten up methods that involve operations with all of them.
/// </summary>
private static List<Tuple<List<Room>, string>> _allServersToPerformActionOn = new()
{
new Tuple<List<Room>, string>(_northAmericaServers, NorthAmericaCachedServers),
new Tuple<List<Room>, string>(_southAmericaServers, SouthAmericaCachedServers),
new Tuple<List<Room>, string>(_europeServers, EuropeCachedServers),
new Tuple<List<Room>, string>(_africaServers, AfricaCachedServers),
new Tuple<List<Room>, string>(_asiaServers, AsiaCachedServers),
new Tuple<List<Room>, string>(_oceaniaServers, OceaniaCachedServers),
new Tuple<List<Room>, string>(_allServers, allCachedServers),
};

private LoadBalancerStats _stats
{
get => new()
{
nodeCount = Program.instance.availableRelayServers.Count,
uptime = DateTime.Now - Program.startupTime,
CCU = Program.instance.GetTotalCCU(),
totalServerCount = Program.instance.GetTotalServers(),
};
}
}
}

0 comments on commit 97bd099

Please sign in to comment.