Fixed Load Balancer MasterList

This commit is contained in:
Speidy674 2022-08-23 01:28:33 +02:00
parent 43f1b84c2f
commit 1af452928e
6 changed files with 24 additions and 8 deletions

View file

@ -48,13 +48,10 @@ namespace LightReflectiveMirror.LoadBalancing
public string serverName; public string serverName;
public string serverData; public string serverData;
public bool isPublic; public bool isPublic;
public int currentPlayers { get => clients.Count + 1; }
public int maxPlayers; public int maxPlayers;
[JsonIgnore]
public List<int> clients;
public RelayAddress relayInfo; public RelayAddress relayInfo;
public int currentPlayers;
} }
public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania } public enum LRMRegions { Any, NorthAmerica, SouthAmerica, Europe, Asia, Africa, Oceania }

View file

@ -90,6 +90,7 @@ namespace LightReflectiveMirror.LoadBalancing
Logger.ForceLogMessage("Conflicting Rooms! (That's ok)", ConsoleColor.Yellow); Logger.ForceLogMessage("Conflicting Rooms! (That's ok)", ConsoleColor.Yellow);
} }
Logger.WriteLogMessage($"Try Calling CacheAllServers", ConsoleColor.Cyan);
CacheAllServers(); CacheAllServers();
await context.Response.SendResponseAsync(HttpStatusCode.Ok); await context.Response.SendResponseAsync(HttpStatusCode.Ok);
} }
@ -168,12 +169,18 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
string region = context.Request.Headers["x-Region"]; string region = context.Request.Headers["x-Region"];
if (int.TryParse(region, out int regionID))
if (int.TryParse("Info:" + region, out int regionID))
{ {
Logger.WriteLogMessage("Send Servers, " + _regionRooms[(LRMRegions)regionID].Count, ConsoleColor.Cyan);
Logger.WriteLogMessage("Send Servers(Cached), " + _cachedRegionRooms[(LRMRegions)regionID], ConsoleColor.Cyan);
await context.Response.SendResponseAsync(_cachedRegionRooms[(LRMRegions)regionID]); await context.Response.SendResponseAsync(_cachedRegionRooms[(LRMRegions)regionID]);
return; return;
} }
Logger.WriteLogMessage("Send Servers, "+_regionRooms[LRMRegions.Any].Count,ConsoleColor.Cyan);
Logger.WriteLogMessage("Send Servers(Cached), " + _cachedRegionRooms[LRMRegions.Any], ConsoleColor.Cyan);
// They didnt submit a region header, just give them all servers as they probably are viewing in browser. // They didnt submit a region header, just give them all servers as they probably are viewing in browser.
await context.Response.SendResponseAsync(_cachedRegionRooms[LRMRegions.Any]); await context.Response.SendResponseAsync(_cachedRegionRooms[LRMRegions.Any]);
} }

View file

@ -1,4 +1,6 @@
using Newtonsoft.Json; using LightReflectiveMirror.Debug;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -9,8 +11,16 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
static void CacheAllServers() static void CacheAllServers()
{ {
Logger.WriteLogMessage($"CacheAllServers[{_regionRooms.Count}]", ConsoleColor.Cyan);
foreach (var region in _regionRooms) foreach (var region in _regionRooms)
_cachedRegionRooms[region.Key] = JsonConvert.SerializeObject(region.Value); {
Logger.WriteLogMessage($"CacheAllServers[{region.Key}][{region.Value.Count}]", ConsoleColor.Cyan);
_cachedRegionRooms[region.Key] = JsonConvert.SerializeObject(region.Value,Formatting.Indented);
Logger.WriteLogMessage($"CacheAllServers[{region.Key}][{region.Value.Count}] {_cachedRegionRooms[region.Key]}", ConsoleColor.Cyan);
}
} }
static void ClearAllServersLists() static void ClearAllServersLists()

View file

@ -159,6 +159,7 @@ namespace LightReflectiveMirror.LoadBalancing
/// <returns></returns> /// <returns></returns>
public async Task<List<Room>> RequestServerListFromNode(string serverIP, ushort port) public async Task<List<Room>> RequestServerListFromNode(string serverIP, ushort port)
{ {
Logger.ForceLogMessage($"Request Server List from Node: {serverIP}:{port}", ConsoleColor.Yellow);
using (WebClient wc = new WebClient()) using (WebClient wc = new WebClient())
{ {
try try

View file

@ -38,7 +38,7 @@ namespace LightReflectiveMirror
supportsDirectConnect = hostIP != null && useDirectConnect, supportsDirectConnect = hostIP != null && useDirectConnect,
port = port, port = port,
useNATPunch = useNatPunch, useNATPunch = useNatPunch,
relayInfo = new RelayAddress { address = Program.publicIP, port = Program.conf.TransportPort, endpointPort = Program.conf.EndpointPort } relayInfo = new RelayAddress { address = Program.publicIP, port = Program.conf.TransportPort, endpointPort = Program.conf.EndpointPort, serverRegion = Program.conf.LoadBalancerRegion }
}; };
rooms.Add(room); rooms.Add(room);

View file

@ -39,5 +39,6 @@ namespace LightReflectiveMirror
public ushort port; public ushort port;
public ushort endpointPort; public ushort endpointPort;
public string address; public string address;
public LRMRegions serverRegion;
} }
} }