pinging
This commit is contained in:
parent
e2833d53bb
commit
7ea91c9a90
4 changed files with 44 additions and 12 deletions
|
|
@ -10,6 +10,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
{
|
{
|
||||||
public int ConnectedServerPingRate = 10000;
|
public int ConnectedServerPingRate = 10000;
|
||||||
public string AuthKey = "AuthKey";
|
public string AuthKey = "AuthKey";
|
||||||
public ushort EndpointPort = 8080;
|
public ushort EndpointPort = 7070;
|
||||||
|
public ushort RelayEndpointPort = 8080;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,18 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
public async Task ReceiveAuthKey(IHttpContext context)
|
public async Task ReceiveAuthKey(IHttpContext context)
|
||||||
{
|
{
|
||||||
var req = context.Request.Headers;
|
var req = context.Request.Headers;
|
||||||
|
string receivedAuthKey = req[0];
|
||||||
|
string address = req[1];
|
||||||
|
|
||||||
|
Console.WriteLine("Received auth req [" + req[0] + "] == [" + Program.conf.AuthKey+"]");
|
||||||
|
|
||||||
// if server is authenticated
|
// if server is authenticated
|
||||||
if (req[0] == Program.conf.AuthKey)
|
if (receivedAuthKey == Program.conf.AuthKey)
|
||||||
{
|
{
|
||||||
var address = context.Request.RemoteEndPoint.Address.ToString();
|
|
||||||
await Program.instance.AddServer(address);
|
|
||||||
|
|
||||||
Console.WriteLine("Server accepted: " + address);
|
Console.WriteLine("Server accepted: " + address);
|
||||||
|
|
||||||
|
await Program.instance.AddServer(address);
|
||||||
|
|
||||||
await context.Response.SendResponseAsync(HttpStatusCode.Ok);
|
await context.Response.SendResponseAsync(HttpStatusCode.Ok);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -40,6 +43,8 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
[RestRoute("Get", "/api/join/")]
|
[RestRoute("Get", "/api/join/")]
|
||||||
public async Task JoinRelay(IHttpContext context)
|
public async Task JoinRelay(IHttpContext context)
|
||||||
{
|
{
|
||||||
|
// need to copy over in order to avoid
|
||||||
|
// collection being modified while iterating.
|
||||||
var servers = Program.instance.availableRelayServers.ToList();
|
var servers = Program.instance.availableRelayServers.ToList();
|
||||||
|
|
||||||
if(servers.Count == 0)
|
if(servers.Count == 0)
|
||||||
|
|
@ -48,8 +53,6 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// need to copy over in order to avoid
|
|
||||||
// collection being modified while iterating.
|
|
||||||
KeyValuePair<string, RelayStats> lowest = new("Dummy", new RelayStats { ConnectedClients = int.MaxValue });
|
KeyValuePair<string, RelayStats> lowest = new("Dummy", new RelayStats { ConnectedClients = int.MaxValue });
|
||||||
|
|
||||||
for (int i = 0; i < servers.Count; i++)
|
for (int i = 0; i < servers.Count; i++)
|
||||||
|
|
@ -68,7 +71,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
|
|
||||||
public class EndpointServer
|
public class EndpointServer
|
||||||
{
|
{
|
||||||
public bool Start(ushort port = 8080)
|
public bool Start(ushort port = 7070)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.31025.194
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LRM_LoadBalancer", "LRM_LoadBalancer\LRM_LoadBalancer.csproj", "{B9B714BE-27A8-4815-933D-C122CA431750}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B9B714BE-27A8-4815-933D-C122CA431750}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B9B714BE-27A8-4815-933D-C122CA431750}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {881733E9-B7F1-471A-9651-19257E8C56E7}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
|
|
@ -66,8 +66,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
|
|
||||||
async Task<RelayStats> ManualPingServer(string serverIP)
|
async Task<RelayStats> ManualPingServer(string serverIP)
|
||||||
{
|
{
|
||||||
string url = serverIP + API_PATH;
|
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://"+serverIP);
|
||||||
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -76,11 +75,11 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd());
|
return JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd());
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (WebException ex)
|
||||||
{
|
{
|
||||||
// server doesnt exist anymore probably
|
// server doesnt exist anymore probably
|
||||||
// do more shit here
|
// do more shit here
|
||||||
|
|
||||||
return new RelayStats { PublicRoomCount = -1 };
|
return new RelayStats { PublicRoomCount = -1 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -89,6 +88,8 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
WriteLogMessage("Pinging " + availableRelayServers.Count + " available relays");
|
||||||
|
|
||||||
foreach (var server in availableRelayServers)
|
foreach (var server in availableRelayServers)
|
||||||
{
|
{
|
||||||
string url = server + API_PATH;
|
string url = server + API_PATH;
|
||||||
|
|
@ -100,6 +101,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
|
|
||||||
var reader = new StreamReader(response.GetResponseStream());
|
var reader = new StreamReader(response.GetResponseStream());
|
||||||
|
|
||||||
|
WriteLogMessage("Server " + server.Key + " still exists, keeping in collection.");
|
||||||
availableRelayServers.Remove(server.Key);
|
availableRelayServers.Remove(server.Key);
|
||||||
availableRelayServers.Add(server.Key, JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd()));
|
availableRelayServers.Add(server.Key, JsonConvert.DeserializeObject<RelayStats>(reader.ReadToEnd()));
|
||||||
}
|
}
|
||||||
|
|
@ -108,6 +110,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
||||||
// server doesnt exist anymore probably
|
// server doesnt exist anymore probably
|
||||||
// do more shit here
|
// do more shit here
|
||||||
|
|
||||||
|
WriteLogMessage("Server " + server.Key + " does not exist anymore, removing", ConsoleColor.Red);
|
||||||
availableRelayServers.Remove(server.Key);
|
availableRelayServers.Remove(server.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue