예제 #1
0
        static void Main(string[] args)
        {
            Initialize();

            DateTime lastDMECheck      = DateTime.UtcNow;
            DateTime lastConfigRefresh = DateTime.UtcNow;

            Console.WriteLine("Starting medius components...");

            Console.WriteLine($"Starting MUIS on port {UniverseInfoServer.Port}.");
            UniverseInfoServer.Start();
            Console.WriteLine($"MUIS started.");

            Console.WriteLine($"Starting MAS on port {AuthenticationServer.Port}.");
            AuthenticationServer.Start();
            Console.WriteLine($"MUIS started.");

            Console.WriteLine($"Starting MLS on port {LobbyServer.Port}.");
            LobbyServer.Start();
            Console.WriteLine($"MUIS started.");

            Console.WriteLine($"Starting MPS on port {ProxyServer.Port}.");
            ProxyServer.Start();
            Console.WriteLine($"MUIS started.");

            Console.WriteLine($"Starting NAT on port {NATServer.Port}.");
            NATServer.Start();
            Console.WriteLine($"MUIS started.");

            //
            Console.WriteLine("Started.");
            while (true)
            {
                // Remove old clients
                for (int i = 0; i < Clients.Count; ++i)
                {
                    if (Clients[i] == null || !Clients[i].IsConnected)
                    {
                        Console.WriteLine($"Destroying Client SK:{Clients[i].SessionKey} Token:{Clients[i].Token} Name:{Clients[i].ClientAccount?.AccountName}");
                        Clients[i]?.Logout();
                        Clients.RemoveAt(i);
                        --i;
                    }
                }

                // Tick
                UniverseInfoServer.Tick();
                AuthenticationServer.Tick();
                LobbyServer.Tick();
                ProxyServer.Tick();
                //NATServer.Tick();

                // Tick channels
                for (int i = 0; i < Channels.Count; ++i)
                {
                    if (Channels[i].ReadyToDestroy)
                    {
                        Console.WriteLine($"Destroying Channel Id:{Channels[i].Id} Name:{Channels[i].Name}");
                        Channels.RemoveAt(i);
                        --i;
                    }
                    else
                    {
                        Channels[i].Tick();
                    }
                }

                // Tick games
                for (int i = 0; i < Games.Count; ++i)
                {
                    if (Games[i].ReadyToDestroy)
                    {
                        Console.WriteLine($"Destroying Game Id:{Games[i].Id} Name:{Games[i].GameName}");
                        Games[i].EndGame();
                        Games.RemoveAt(i);
                        --i;
                    }
                    else
                    {
                        Games[i].Tick();
                    }
                }

                // Check DME
                if (Program.Settings.DmeRestartOnCrash && !string.IsNullOrEmpty(Program.Settings.DmeStartPath) && (DateTime.UtcNow - lastDMECheck).TotalSeconds > 1)
                {
                    EnsureDMERunning(Program.Settings.DmeStartPath);
                    lastDMECheck = DateTime.UtcNow;
                }

                // Reload config
                if ((DateTime.UtcNow - lastConfigRefresh).TotalMilliseconds > Settings.RefreshConfigInterval)
                {
                    RefreshConfig();
                    lastConfigRefresh = DateTime.UtcNow;
                }

                Thread.Sleep(sleepMS);
            }
        }
예제 #2
0
        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();
            }
        }