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

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

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

            try
            {
                Settings.Initialize();

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

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

                CharServer.RemoteEndPoint = new IPEndPoint(Settings.GetIPAddress("ExternalIP", "Char"),
                                                           Settings.GetInt("Port", "Char"));

                //MapleData.Initialize();

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

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

            if (CharServer.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 (CharServer.IsAlive)
            {
                CharServer.AcceptDone.Reset();

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

                CharServer.AcceptDone.WaitOne();
            }

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

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

            CharServer.Dispose();

            Log.Warn("Server stopped.");

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

                Thread.Sleep(CharServer.AutoRestartTime * 1000);

                goto start;
            }
            else
            {
                Console.Read();
            }
        }
Example #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 v.{0}.{1}", 10, 10);

            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");
                Log.Inform("Staff will{0}be required to connect through a staff IP.", LoginServer.RequireStaffIP ? " " : " not ");

                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           = 6,
                            QuestExperience      = 6,
                            PartyQuestExperience = 6,

                            Meso = 3,
                            Loot = 2
                        } // TODO: Actual rate load.
                    });
                }

                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();
            }
        }