예제 #1
0
        private static void Main(string[] args)
        {
            Log.SetLogFile(".\\Logs\\MessageLog.log");

start:
            MessageServer.Clients = new List <Client>();

            Log.Entitle("Message Server v.{0}.{1}", 10, 10);

            try
            {
                Settings.Initialize();

                MessageServer.AutoRestartTime = 15; // TODO: Get actual restart-time.
                Log.Inform("Automatic restart time set to {0} seconds.", MessageServer.AutoRestartTime);

                Database.Test();
                Database.Analyze(false); // NOTE: The shop server uses mcdb for information like items, etcetera.

                MessageServer.RemoteEndPoint = new IPEndPoint(Settings.GetIPAddress("ExternalIP", "Message"), Settings.GetInt("Port", "Message"));

                //MapleData.Initialize();

                MessageServer.Listener = new TcpListener(IPAddress.Any, MessageServer.RemoteEndPoint.Port);
                MessageServer.Listener.Start();
                Log.Inform("Initialized clients listener on {0}.", MessageServer.Listener.LocalEndpoint);

                MessageServer.IsAlive = true;
            }
            catch (Exception e)
            {
#if DEBUG
                Log.Error(e.ToString());
#else
                Log.Error(e);
#endif
            }

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

                new Thread(new ThreadStart(InteroperabilityClient.Main)).Start();
            }
            else
            {
                Log.Inform("Could not start server because of errors.");
            }

            while (MessageServer.IsAlive)
            {
                MessageServer.AcceptDone.Reset();

                MessageServer.Listener.BeginAcceptSocket(new AsyncCallback(MessageServer.OnAcceptSocket), null);

                MessageServer.AcceptDone.WaitOne();
            }

            Client[] remainingClients = MessageServer.Clients.ToArray();

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

            MessageServer.Dispose();

            Log.Warn("Server stopped.");

            if (MessageServer.AutoRestartTime > 0)
            {
                Log.Inform("Attempting auto-restart in {0} seconds.", MessageServer.AutoRestartTime);

                Thread.Sleep(MessageServer.AutoRestartTime * 1000);

                goto start;
            }
            else
            {
                Console.Read();
            }
        }
예제 #2
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();
            }
        }