Files
flowframes/Code/Data/Servers.cs

56 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.Data
{
class Servers
{
public static Server hetznerEu = new Server { host = "nmkd-hz.de", pattern = "https://dl.*" };
2021-08-31 16:14:05 +02:00
public static Server contaboUs = new Server { host = "nmkd-cb.de", pattern = "https://dl.*" };
public static List<Server> serverList = new List<Server> { hetznerEu, contaboUs };
public static Server closestServer = serverList[0];
public class Server
{
public string host = "";
public string pattern = "*";
public string GetUrl ()
{
return pattern.Replace("*", host);
}
}
public static async Task Init ()
{
Dictionary<string[], long> serversPings = new Dictionary<string[], long>();
foreach(Server server in serverList)
{
try
{
Ping p = new Ping();
PingReply replyEur = p.Send(server.host, 2000);
serversPings[new string[] { server.host, server.pattern }] = replyEur.RoundtripTime;
Logger.Log($"[Servers] Ping to {server.host}: {replyEur.RoundtripTime} ms", true);
}
catch (Exception e)
{
Logger.Log($"[Servers] Failed to ping {server.host}: {e.Message}", true);
serversPings[new string[] { server.host, server.pattern }] = 10000;
}
}
var closest = serversPings.Aggregate((l, r) => l.Value < r.Value ? l : r);
Logger.Log($"[Servers] Closest Server: {closest.Key[0]} ({closest.Value} ms)", true);
closestServer = new Server { host = closest.Key[0], pattern = closest.Key[1] };
}
}
}