Example #1
0
        public static void ServerLoop()
        {
            AcceptDone = new ManualResetEvent(false);
            Worlds     = new Worlds();
            Clients    = new List <Client>();

            Log.SetLogFile(".\\Logs\\LoginLog.log");
            Log.Entitle("Login Server (CLIENT VERSION {0})", ServerConstants.CLIENT_VERSION);

            try
            {
                Settings.Initialize();

                Database.Test();
                Database.Analyze(false);

                SecurityCode = Settings.GetString("SecurityCode", "Interconnection");
                Log.Inform("Cross-servers code '{0}' assigned.", Log.MaskString(LoginServer.SecurityCode));

                RequireStaffIP   = Settings.GetBool("RequireStaffIP", "Login");
                IsMaintenance    = Settings.GetBool("isMaintenance", "Login");
                PatchVer         = Settings.GetInt("PatchVersion", "Login");
                PatchDownloadUrl = Settings.GetString("PatchDownloadUrl", "Login");

                Log.Inform("Staff will {0} be required to connect through a staff IP.",
                           LoginServer.RequireStaffIP ? " " : " not ");
                Log.Debug("IsMaintenance: {0}", IsMaintenance);
                TcpListener Listener = new TcpListener(IPAddress.Any, Settings.GetInt("Port", "Login"));
                Listener.Start();
                Log.Inform("Initialized clients listener on {0}.", Listener.LocalEndpoint);

                LoginServer.Pinger.Interval = Settings.GetInt("PingInterval");
                LoginServer.Pinger.Start();
                Log.Inform("Clients pinger set to {0} ms.", LoginServer.Pinger.Interval);

                foreach (string world in Settings.GetBlocksFromBlock("Worlds", 1))
                {
                    Worlds.Add(new World()
                    {
                        ID              = Settings.GetByte("ID", world),
                        HostIP          = Settings.GetIPAddress("Host", world),
                        Flag            = Settings.GetEnum <ServerUtilities.ServerFlag>("Flag", world),
                        Channel         = Settings.GetByte("Channel", world),
                        EventMessage    = Settings.GetString("EventMessage", world),
                        DisableCreation = Settings.GetBool("DisableCreation", world),
                        ScrollingHeader = Settings.GetString("ScrollingHeader", world),
                        Rates           = new ServerUtilities.Rates()
                        {
                            Experience           = Settings.GetInt("ExperienceRate", world),
                            QuestExperience      = Settings.GetInt("QuestExperienceRate", world),
                            PartyQuestExperience = Settings.GetInt("PartyQuestExperience", world),

                            Meso = Settings.GetInt("MesoDropRate", world),
                            Loot = Settings.GetInt("LootDropRate", world)
                        }
                    });
                }

                IsAlive = true;

                Log.Success("Server started on thread {0}.", Thread.CurrentThread.ManagedThreadId);

                AppDomain.CurrentDomain.UnhandledException += (s, e) =>
                {
                    Log.Error("Unhandled exception from Server: \n{0}", e.ExceptionObject.ToString());
                };

                new Thread(new ThreadStart(InteroperabilityServer.ServerLoop)).Start();

                while (IsAlive)
                {
                    AcceptDone.Reset();

                    Listener.BeginAcceptSocket((iar) =>
                    {
                        new Client(Listener.EndAcceptSocket(iar));

                        AcceptDone.Set();
                    }, null);

                    AcceptDone.WaitOne();
                }

                InteroperabilityServer.Stop();

                Client[] remainingClients = Clients.ToArray();

                foreach (Client client in remainingClients)
                {
                    client.Dispose();
                }

                Listener.Stop();

                Log.Warn("Login stopped.");
            }
            catch (Exception e)
            {
                Log.Error(e);
                Log.Inform("Could not start server because of errors.");
            }
            finally
            {
                Console.Read();
            }
        }