Prevent LLB from registering the same LRM node twice
This commit is contained in:
parent
35bd75bc05
commit
137ff64d17
3 changed files with 37 additions and 14 deletions
|
|
@ -12,5 +12,6 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
public string AuthKey = "AuthKey";
|
||||
public ushort EndpointPort = 7070;
|
||||
public ushort RelayEndpointPort = 8080;
|
||||
public bool ShowDebugLogs = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,15 +33,25 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
|
||||
string address = context.Request.RemoteEndPoint.Address.ToString();
|
||||
|
||||
if(Program.showDebugLogs)
|
||||
Console.WriteLine("Received auth req [" + receivedAuthKey + "] == [" + Program.conf.AuthKey + "]");
|
||||
|
||||
// if server is authenticated
|
||||
if (receivedAuthKey != null && address != null && endpointPort != null && gamePort != null && receivedAuthKey == Program.conf.AuthKey)
|
||||
{
|
||||
if(Program.showDebugLogs)
|
||||
Console.WriteLine($"Server accepted: {address}:{gamePort}");
|
||||
|
||||
try
|
||||
{
|
||||
var _gamePort = Convert.ToUInt16(gamePort);
|
||||
var _endpointPort = Convert.ToUInt16(endpointPort);
|
||||
await Program.instance.AddServer(address, _gamePort, _endpointPort);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await context.Response.SendResponseAsync(HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
await context.Response.SendResponseAsync(HttpStatusCode.Ok);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
public Dictionary<RelayAddress, RelayServerInfo> availableRelayServers = new();
|
||||
|
||||
private int _pingDelay = 10000;
|
||||
public static bool showDebugLogs = false;
|
||||
const string API_PATH = "/api/stats";
|
||||
const string CONFIG_PATH = "config.json";
|
||||
|
||||
|
|
@ -41,6 +42,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
{
|
||||
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText(CONFIG_PATH));
|
||||
_pingDelay = conf.ConnectedServerPingRate;
|
||||
showDebugLogs = conf.ShowDebugLogs;
|
||||
|
||||
if (new EndpointServer().Start(conf.EndpointPort))
|
||||
WriteLogMessage("Endpoint server started successfully", ConsoleColor.Green);
|
||||
|
|
@ -58,10 +60,18 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
|
||||
public async Task AddServer(string serverIP, ushort port, ushort endpointPort)
|
||||
{
|
||||
var relayAddr = new RelayAddress { Port = port, EndpointPort = endpointPort, Address = serverIP };
|
||||
|
||||
if (availableRelayServers.ContainsKey(relayAddr))
|
||||
{
|
||||
WriteLogMessage($"LRM Node {serverIP}:{port} tried to register while already registered!");
|
||||
return;
|
||||
}
|
||||
|
||||
var stats = await ManualPingServer(serverIP, endpointPort);
|
||||
|
||||
if (stats.HasValue)
|
||||
availableRelayServers.Add(new RelayAddress { Port = port, EndpointPort = endpointPort, Address = serverIP }, stats.Value);
|
||||
availableRelayServers.Add(relayAddr, stats.Value);
|
||||
}
|
||||
|
||||
public async Task<RelayServerInfo?> ManualPingServer(string serverIP, ushort port)
|
||||
|
|
@ -103,6 +113,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
{
|
||||
while (true)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays");
|
||||
|
||||
// Create a new list so we can modify the collection in our loop.
|
||||
|
|
@ -119,6 +130,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
var serverStats = wc.DownloadString(url);
|
||||
var deserializedData = JsonConvert.DeserializeObject<RelayServerInfo>(serverStats);
|
||||
|
||||
if (showDebugLogs)
|
||||
WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection.");
|
||||
|
||||
if (availableRelayServers.ContainsKey(keys[i]))
|
||||
|
|
@ -130,6 +142,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
{
|
||||
// server doesnt exist anymore probably
|
||||
// do more shit here
|
||||
if (showDebugLogs)
|
||||
WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red);
|
||||
availableRelayServers.Remove(keys[i]);
|
||||
}
|
||||
|
|
@ -214,5 +227,4 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
|
||||
public RelayAddress relayInfo;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue