static async Task StartServerAsync() { DateTime lastConfigRefresh = DateTime.UtcNow; DateTime lastComponentLog = DateTime.UtcNow; Stopwatch sleepSw = new Stopwatch(); #if DEBUG || RELEASE Stopwatch sw = new Stopwatch(); int ticks = 0; #endif Logger.Info("Starting medius components..."); Logger.Info($"Starting MAS on port {AuthenticationServer.Port}."); AuthenticationServer.Start(); Logger.Info($"MAS started."); Logger.Info($"Starting MLS on port {LobbyServer.Port}."); LobbyServer.Start(); Logger.Info($"MLS started."); Logger.Info($"Starting MPS on port {ProxyServer.Port}."); ProxyServer.Start(); Logger.Info($"MPS started."); // Logger.Info("Started."); try { #if DEBUG || RELEASE sw.Start(); #endif while (true) { #if DEBUG || RELEASE ++ticks; if (sw.Elapsed.TotalSeconds > 5f) { // sw.Stop(); float tps = ticks / (float)sw.Elapsed.TotalSeconds; float error = MathF.Abs(Settings.TickRate - tps) / Settings.TickRate; if (error > 0.1f) { Logger.Error($"Average TPS: {tps} is {error * 100}% off of target {Settings.TickRate}"); } sw.Restart(); ticks = 0; } #endif // Attempt to authenticate with the db middleware // We do this every 24 hours to get a fresh new token if ((_lastSuccessfulDbAuth == null || (DateTime.UtcNow - _lastSuccessfulDbAuth.Value).TotalHours > 24)) { if (!await Database.Authenticate()) { // Log and exit when unable to authenticate Logger.Error("Unable to authenticate with the db middleware server"); return; } else { _lastSuccessfulDbAuth = DateTime.UtcNow; } } // sleepSw.Restart(); // Tick await Task.WhenAll(AuthenticationServer.Tick(), LobbyServer.Tick(), ProxyServer.Tick()); // Tick manager Manager.Tick(); // Tick plugins Plugins.Tick(); // if ((DateTime.UtcNow - lastComponentLog).TotalSeconds > 15f) { AuthenticationServer.Log(); LobbyServer.Log(); ProxyServer.Log(); lastComponentLog = DateTime.UtcNow; } // Reload config if ((DateTime.UtcNow - lastConfigRefresh).TotalMilliseconds > Settings.RefreshConfigInterval) { RefreshConfig(); lastConfigRefresh = DateTime.UtcNow; } Thread.Sleep((int)Math.Max(0, sleepMS - sleepSw.ElapsedMilliseconds)); } } finally { await AuthenticationServer.Stop(); await LobbyServer.Stop(); await ProxyServer.Stop(); } }