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.*" }; public static Server contaboUs = new Server { host = "nmkd-cb.de", pattern = "https://dl.*" }; public static List serverList = new List { 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 serversPings = new Dictionary(); 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] }; } } }