Exemple #1
0
        private static void Main(string[] args)
        {
            int port = 0;

            Log.SetLogFile(".\\Logs\\GameLog.log");

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

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

            try
            {
                if (port == 0)
                {
                    try
                    {
                        port = int.Parse(args[0]);
                    }
                    catch
                    {
                        //port = Log.Input("Port[15101]: ", 15101);
                        port = 15101;
                        Log.Inform("Default Game Server Port: {0}", port);
                    }
                }

                Settings.Initialize();

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

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

                GameServer.RemoteEndPoint =
                    new IPEndPoint(IPAddress.Parse(ServerConstants.LISTENER_SERVER_IP), port);                     // TODO: Get actual host.

                Log.Load("Items Initialize");
                ItemFactory.Initialize();
                Log.Success("\r\n");
                Log.Load("Maps Initialize");
                MapFactory.Initialize();
                Log.Success("\r\n");
                MobFactory.InitializeMonsterDrop();
                CashShopFactory.InitializeHatCommodity();
                CashShopFactory.InitializeMantleCommodity();
                CashShopFactory.InitializeBoyDressCommodity();
                CashShopFactory.InitializeGirlDressCommodity();
                CashShopFactory.InitializeBoyHairCommodity();
                CashShopFactory.InitializeGirlHairCommodity();
                CashShopFactory.InitializeFace1Commodity();
                CashShopFactory.InitializeFace2Commodity();
                CashShopFactory.InitializeBoyEyesCommodity();
                CashShopFactory.InitializeGirlEyesCommodity();
                CashShopFactory.InitializePetCommodity();
                CashShopFactory.InitializeAmuletCommodity();
                CashShopFactory.InitializeTalismanCommodity();
                CashShopFactory.InitializeProduceCommodity();

                UdpRemoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerConstants.SERVER_IP), ServerConstants.UDP_PORT);
                UdpListener       = new UdpClient(UdpRemoteEndPoint);
                Log.Inform("Initialized clients UDP listener on {0}. Port {1}", UdpRemoteEndPoint.Address, UdpRemoteEndPoint.Port);

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

                GameServer.IsAlive = true;
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            if (GameServer.IsAlive)
            {
                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(InteroperabilityClient.Main)).Start();
            }
            else
            {
                Log.Inform("Could not start server because of errors.");
            }

            while (GameServer.IsAlive)
            {
                GameServer.AcceptDone.Reset();
                GameServer.Listener.BeginAcceptSocket(new AsyncCallback(GameServer.OnAcceptSocket), null);
                GameServer.AcceptDone.WaitOne();
            }

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

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

            GameServer.Dispose();

            Log.Warn("Server stopped.");

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

                Thread.Sleep(GameServer.AutoRestartTime * 1000);

                goto start;
            }
            else
            {
                Console.Read();
            }
        }