예제 #1
0
		private static void Command(RoomServer server){
			string cmd="";
			while(server.IsListening){
				cmd = Console.ReadLine();
				server.OnCommand(cmd);
			}
		}
 public static void OnSendServerInfo(this RoomServer roomServer, Session session)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.Info);
         writer.Write(roomServer.GetChatPort());
         DuelServer srv = roomServer.GetMinServer();
         lock (srv.AsyncLock)
         {
             srv.Count++;
         }
         if (srv != null)
         {
             session.ServerInfo = srv;
             writer.Write(srv.Port);
             writer.Write(srv.NeedAuth);
         }
         else
         {
             writer.Write(0);
             writer.Write((byte)0);
         }
         writer.WriteUnicode(session.Token, 32);
         session.Send(writer.Content);
     }
 }
        public static void OnRoomList(this RoomServer roomServer, Session session, bool nolock = false, bool nostart = false)
        {
#if DEBUG
            Logger.Debug("roomlist");
#endif
            using (PacketWriter wrtier = new PacketWriter(20))
            {
                int count = 0;
                wrtier.Write((byte)RoomMessage.RoomList);
                wrtier.Write(0);
                lock (roomServer.DuelServers)
                {
                    foreach (DuelServer srv in roomServer.DuelServers)
                    {
                        lock (srv.Rooms)
                        {
                            foreach (GameConfig config in srv.Rooms.Values)
                            {
                                count++;
                                wrtier.Write(srv.Port);
                                wrtier.Write(srv.NeedAuth);
                                wrtier.WriteUnicode(config.Name, 20);
                                wrtier.WriteUnicode(config.BanList, 20);
                                wrtier.WriteUnicode(config.RoomString, 20);
                                wrtier.Write(config.IsStart);
                            }
                        }
                    }
                }
                //Override the length
                wrtier.SetPosition(1);
                wrtier.Write(count);
                session.Send(wrtier.Content);
            }
        }
        public static void server_OnPlayerLeave(this RoomServer roomServer, DuelServer server, string name, string room)
        {
            if (server != null && string.IsNullOrEmpty(room))
            {
                lock (server.AsyncLock)
                {
                    server.Count--;
                }
            }
            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            lock (roomServer.Clients)
            {
                Session player = null;
                if (roomServer.Clients.TryGetValue(name, out player))
                {
                    player.RoomName = null;
                }
            }
            using (PacketWriter writer = new PacketWriter(2))
            {
                writer.Write((byte)RoomMessage.PlayerLeave);
                writer.Write(server == null ? 0 : server.Port);
                writer.WriteUnicode(name, 20);
                writer.WriteUnicode(room, 20);
                roomServer.SendAll(writer.Content);
            }
        }
 public static void OnServerClose(this RoomServer roomServer, DuelServer server)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.ServerClose);
         writer.Write(server.Port);
         DuelServer srv = roomServer.GetMinServer();
         lock (srv.AsyncLock)
         {
             srv.Count = 0;
         }
         if (srv != null)
         {
             //Special handling
             writer.Write(srv.Port);
             writer.Write(srv.NeedAuth);
             //session.ServerInfo = srv;
             lock (roomServer.Clients)
             {
                 foreach (Session client in roomServer.Clients.Values)
                 {
                     if (client.ServerInfo != null && client.ServerInfo.Port == server.Port)
                     {
                         client.ServerInfo = srv;
                         client.Send(writer.Content);
                     }
                 }
             }
         }
     }
 }
예제 #6
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);
		}
        public static void OnChatMessage(this RoomServer roomServer, string name, string toname, string msg)
        {
            using (PacketWriter writer = new PacketWriter(2))
            {
                writer.Write((byte)RoomMessage.OnChat);
                writer.Write((short)0x11);
                writer.WriteUnicode(name + ":" + msg);
                if (string.IsNullOrEmpty(toname))
                {
                    Logger.Debug("send to client");
                    roomServer.SendAllClient(writer.Content, name: name);
                }
            }
            using (PacketWriter writer = new PacketWriter(2))
            {
                writer.Write((byte)RoomMessage.Chat);
                writer.WriteUnicode(name, 20);
                writer.WriteUnicode(toname, 20);
                writer.WriteUnicode(msg, msg.Length + 1);
                if (!string.IsNullOrEmpty(toname))
                {
                    lock (roomServer.Clients)
                    {
                        Session sender = null;
                        if (roomServer.Clients.TryGetValue(name, out sender))
                        {
                            sender.Send(writer.Content, true);
                        }
                        else
                        {
#if DEBUG
                            Console.WriteLine("no find " + name);
#endif
                        }
                        if (name != toname)
                        {
                            Session recevicer = null;
                            if (roomServer.Clients.TryGetValue(toname, out recevicer))
                            {
                                recevicer.Send(writer.Content, true);
                            }
                            else
                            {
                                if (sender != null)
                                {
                                    sender.SendError("[err]" + toname + " Not online。");
                                }
#if DEBUG
                                Console.WriteLine("no find " + toname);
#endif
                            }
                        }
                    }
                }
                else
                {
                    roomServer.SendAll(writer.Content, true, true);
                }
            }
        }
예제 #8
0
		public Server(RoomServer server,string fileName,int port, string config="config.txt")
		{
			this.Port= port;
			this.Server=server;
			m_config = config;
			m_fileName = fileName;
			Logger.Debug(fileName+":"+config);
		}
예제 #9
0
 public Server(RoomServer server, string fileName, int port, string config = "config.txt")
 {
     this.Port   = port;
     this.Server = server;
     m_config    = config;
     m_fileName  = fileName;
     Logger.Debug(fileName + ":" + config);
 }
 public static void OnServerStop(this RoomServer roomServer)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.ServerStop);
         roomServer.SendAll(writer.Content);
     }
 }
 public static void OnError(this RoomServer roomServer, string err)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.Error);
         writer.WriteUnicode(err, err.Length + 1);
         roomServer.SendAll(writer.Content);
     }
 }
예제 #12
0
        private static void Command(RoomServer server)
        {
            string cmd = "";

            while (server.IsListening)
            {
                cmd = Console.ReadLine();
                server.OnCommand(cmd);
            }
        }
 public static void server_OnRoomClose(this RoomServer roomServer, DuelServer server, string name)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.RoomClose);
         writer.Write(server.Port);
         writer.WriteUnicode(name, 20);
         roomServer.SendAll(writer.Content);
     }
 }
 private static void SendAll(this RoomServer roomServer, byte[] data, bool isNow = true, bool Force = false)
 {
     lock (roomServer.Clients)
     {
         foreach (Session client in roomServer.Clients.Values)
         {
             if (Force || !client.IsPause)
             {
                 client.Send(data, isNow);
             }
         }
     }
 }
 public static void server_OnRoomCreate(this RoomServer roomServer, DuelServer server, string name, string banlist, string gameinfo)
 {
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.RoomCreate);
         writer.Write(server.Port);
         writer.Write(server.NeedAuth);
         writer.WriteUnicode(name, 20);
         writer.WriteUnicode(banlist, 20);
         writer.WriteUnicode(gameinfo, gameinfo.Length + 1);
         //start duel
         writer.Write(false);
         roomServer.SendAll(writer.Content);
     }
 }
 private static void SendAllClient(this RoomServer roomServer, byte[] data, bool isNow = true, bool Force = false, string name = null)
 {
     lock (roomServer.GameCliens)
     {
         foreach (Session client in roomServer.GameCliens.Values)
         {
             if (client.CanGameChat)
             {
                 if (string.IsNullOrEmpty(name) || (!string.IsNullOrEmpty(name) && client.Name != name))
                 {
                     client.Send(data, isNow);
                 }
             }
         }
     }
 }
 public static void SendRoomList(this RoomServer roomServer, Session session)
 {
     lock (roomServer.DuelServers)
     {
         foreach (DuelServer srv in roomServer.DuelServers)
         {
             lock (srv.Rooms)
             {
                 foreach (GameConfig game in srv.Rooms.Values)
                 {
                     using (PacketWriter packet = new PacketWriter(2))
                     {
                         packet.Write((ushort)RoomMessage.NETWORK_CLIENT_ID);
                         StocHostPacket hp = new StocHostPacket();
                         hp.port = (ushort)srv.Port;
                         hp.host = game.ToHostInfo();
                         hp.name = new char[20];
                         char[] name = game.HasPassword() ? game.RoomString.ToCharArray() : game.Name.ToCharArray();
                         Array.Copy(name, hp.name, Math.Min(20, name.Length));
                         if (game.IsStart)
                         {
                             int i = 20;
                             while (--i > 4)
                             {
                                 hp.name[i] = hp.name[i - 5];
                             }
                             hp.name[0] = '【';
                             hp.name[1] = '对';
                             hp.name[2] = '战';
                             hp.name[3] = '中';
                             hp.name[4] = '】';
                         }
                         packet.Write(StructTransformer.StructToBytes(hp));
                         session.Send(packet.Content, false);
                     }
                 }
             }
             session.PeekSend();
         }
     }
 }
예제 #18
0
 public Session(Connection<Session> client, RoomServer server, int timeout = 15)
 {
     this.Client = client;
     this.Client.Tag = this;
     this.Server = server;
     MyTimer CheckTimer = new MyTimer(1000, timeout);
     CheckTimer.AutoReset = true;
     CheckTimer.Elapsed += delegate
     {
         if (!string.IsNullOrEmpty(Name))
         {
             CheckTimer.Stop();
             CheckTimer.Close();
         }
         if (CheckTimer.CheckStop())
         {
             //超时自动断开
             Close();
             CheckTimer.Close();
         }
     };
 }
예제 #19
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 server_OnPlayerJoin(this RoomServer roomServer, DuelServer server, string name, string room)
 {
     if (string.IsNullOrEmpty(name))
     {
         return;
     }
     lock (roomServer.Clients)
     {
         Session player = null;
         if (roomServer.Clients.TryGetValue(name, out player))
         {
             player.RoomName = room;
         }
     }
     using (PacketWriter writer = new PacketWriter(2))
     {
         writer.Write((byte)RoomMessage.PlayerEnter);
         writer.Write(server.Port);
         writer.WriteUnicode(name, 20);
         writer.WriteUnicode(room, 20);
         roomServer.SendAll(writer.Content);
     }
 }
예제 #21
0
        public Session(Connection <Session> client, RoomServer server, int timeout = 15)
        {
            this.Client     = client;
            this.Client.Tag = this;
            this.Server     = server;
            MyTimer CheckTimer = new MyTimer(1000, timeout);

            CheckTimer.AutoReset = true;
            CheckTimer.Elapsed  += delegate
            {
                if (!string.IsNullOrEmpty(Name))
                {
                    CheckTimer.Stop();
                    CheckTimer.Close();
                }
                if (CheckTimer.CheckStop())
                {
                    //超时自动断开
                    Close();
                    CheckTimer.Close();
                }
            };
        }
예제 #22
0
 public DuelServer(RoomServer server, Connection <DuelServer> client)
 {
     this.Server = server;
     this.Client = client;
 }
예제 #23
0
		public DuelServer(RoomServer server,Connection<DuelServer> client)
		{
			this.Server =server;
			this.Client  = client;
		}
        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;
            }
        }