Пример #1
0
		/// <summary>
		/// 负责房间列表和在线列表。
		/// <para>客户端登录,验证后,返回聊天服务端(目前本端负责)和(人数最少的)对战服务端的地址,全部的房间列表由本端提供</para>
		/// <para>请求房间列表,返回等待的房间
		/// <para>当房间变化,推送</para></para>
		/// <para>当客户端进入游戏,发送暂停消息,暂停推送,退出游戏,则请求房间列表</para>
		/// </summary>
		/// <param name="args">port GameServer.exe config.txt config1.txt config2.txt ...</param>
		public static void Main(string[] args)
		{
            RoomServer Server = new RoomServer();
            AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
            {
                Server.Stop();
                File.WriteAllText("crash_room_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", e.ExceptionObject.ToString());
                Process.GetCurrentProcess().Kill();
            };
			
			Console.Title = "RoomServer";
			Logger.SetLogLevel(LogLevel.Info);
#if DEBUG
			Logger.SetLogLevel(LogLevel.Debug);
#endif
            //close event
            Console.CancelKeyPress += delegate
            {
                Server.Stop();
            };
            Kernel32.SetConsoleCtrlHandler(new ControlCtrlDelegate((int type)=>{
                Server.Stop();
                return false;
            }), true);


            if (Server.Start()){
				Command(Server);
				Console.WriteLine("server is close");
			}else{
				Console.WriteLine("start fail.");
			}
			Console.ReadKey(true);
		}
Пример #2
0
        /// <summary>
        /// Is responsible for the room and online lists.
        /// <para>Client login validation, return to the chat server (currently the end is responsible for) and (at least) against the server address, full room list provided by the end of</para>
        /// <para>Request the room list, returns the waiting room
        /// <para>As the room changes, push</para></para>
        /// <para>When the client enters the game, send a pause message, push the pause, quit the game, requesting a room list</para>
        /// </summary>
        /// <param name="args">port GameServer.exe config.txt config1.txt config2.txt ...</param>
        public static void Main(string[] args)
        {
            RoomServer Server = new RoomServer();

            AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
            {
                Server.Stop();
                File.WriteAllText("crash_room_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", e.ExceptionObject.ToString());
                Process.GetCurrentProcess().Kill();
            };

            Console.Title = "RoomServer";
            Logger.SetLogLevel(LogLevel.Info);
#if DEBUG
            Logger.SetLogLevel(LogLevel.Debug);
#endif
            //close event
            Console.CancelKeyPress += delegate
            {
                Server.Stop();
            };
            Kernel32.SetConsoleCtrlHandler(new ControlCtrlDelegate((int type) => {
                Server.Stop();
                return(false);
            }), true);


            if (Server.Start())
            {
                Command(Server);
                Console.WriteLine("server is close");
            }
            else
            {
                Console.WriteLine("start fail.");
            }
            Console.ReadKey(true);
        }
        public static void OnCommand(this RoomServer server, string cmd, bool tip = true)
        {
            if (cmd == null)
            {
                return;
            }
            cmd = cmd.Trim();
            string[] args = cmd.Split(new char[] { ' ' }, 2);
            switch (args[0])
            {
            case "say":
                if (args.Length > 1)
                {
                    server.Tip = args[1];
                    server.Message(args[1]);
                    Console.WriteLine(">>say ok");
                }
                else
                {
                    Console.WriteLine(">>say fail");
                }
                break;

            case "server":
                if (args.Length > 1)
                {
                    int i = 0;
                    int.TryParse(args[1], out i);
                    //Service information, number of players and number of rooms
                    server.PrintServer(i);
                }
                else
                {
                    //The number of
                    server.PrintServer();
                }
                break;

            case "hide":
                if (args.Length > 1)
                {
                    int i = 0;
                    int.TryParse(args[1], out i);
                    //Service information, number of players and number of rooms
                    lock (server.Porcess)
                    {
                        if (i < server.Porcess.Count)
                        {
                            ServerProcess p = server.Porcess[i];
                            Console.WriteLine(">>hide " + i + " => " + p.Hide());
                        }
                    }
                }
                else
                {
                    //The number of
                    Console.WriteLine(">>hide %1");
                }
                break;

            case "show":
                if (args.Length > 1)
                {
                    int i = 0;
                    int.TryParse(args[1], out i);
                    //Service information, number of players and number of rooms
                    lock (server.Porcess)
                    {
                        if (i < server.Porcess.Count)
                        {
                            ServerProcess p = server.Porcess[i];
                            Console.WriteLine(">>show " + i + " => " + p.Show());
                        }
                    }
                }
                else
                {
                    //The number of
                    Console.WriteLine(">>show %1");
                }
                break;

            case "close":
                if (args.Length > 1)
                {
                    int i = 0;
                    int.TryParse(args[1], out i);
                    //Service information, number of players and number of rooms
                    lock (server.Porcess){
                        if (i < server.Porcess.Count)
                        {
                            ServerProcess p = server.Porcess[i];
                            Console.WriteLine(">>close " + i + ":" + p.Port);
                            p.Close();
                            server.Porcess.Remove(p);
                        }
                    }
                }
                else
                {
                    //The number of
                    server.Stop();
                }
                break;

            case "help":
                Console.WriteLine(">>server %1 View all server information");
                Console.WriteLine(">>say %1      View all server information");
                Console.WriteLine(">>hide %1     View all server information");
                Console.WriteLine(">>show %1    View all server information ");
                Console.WriteLine(">>close %1   shutdown server");
                break;

            default:
                if (tip)
                {
                    Console.WriteLine(">>no invalid:" + cmd);
                }
                break;
            }
        }