Example #1
0
        ///<summary>
        ///Creates a new object that is a copy of the current instance.
        ///</summary>
        ///
        ///<returns>
        ///A new object that is a copy of this instance.
        ///</returns>
        ///<filterpriority>2</filterpriority>
        public object Clone()
        {
            GameServer srv = new GameServer();

            srv.code = code;
            srv.endPoint = endPoint;
            srv.gameType = gameType;
            srv.isActive = isActive;
            srv.map = map;
            srv.playersCount = playersCount;
            srv.serverName = serverName;
            srv.startTime = srv.startTime;

            return srv;
        }
        private void serverInfoListener(object parameters)
        {
            GameServer srv = null;

            object[] param = null;
            Socket handler = null;
            byte[] buf = null;
            try {
                param = (object[]) parameters;
                handler = (Socket) param[0];
                buf = (byte[]) param[1];
            } catch (Exception ex) {
                LogEvent(EventType.ERROR, ex, "server handler has incorrect parameters");
                if (handler != null) {
                    handler.Close(5);
                }
                return;
            }

            uint packetLength, unknown1;
            while (true) {
                using (var mem = new MemoryStream(buf)) {
                    mem.Seek(0, SeekOrigin.Begin);
                    var binRead = new BinaryReader(mem);

                    packetLength = binRead.ReadUInt32();
                    unknown1 = binRead.ReadUInt32();

                    var decoded = new byte[packetLength];

                    Buffer.BlockCopy(buf,
                                     (int) binRead.BaseStream.Position,
                                     decoded, 0, (int) packetLength);
                    Console.WriteLine("recieved packet");
                    decoded = NetworkHelper.PacketDecoding(decoded);
                    NetworkHelper.DumpArray(Console.OpenStandardOutput(), decoded);

                    using (var mstr = new MemoryStream(decoded)) {
                        var br = new BinaryReader(mstr);
                        byte packetID = br.ReadByte();

                        switch (packetID) {
                                /// bad code warning!
                                /// section below contains processing of 2 in series
                            case 0xD1: {
                                //ident server
                                br.BaseStream.Seek(5, SeekOrigin.Begin);
                                byte length = br.ReadByte();
                                br.BaseStream.Seek(1, SeekOrigin.Current);
                                string addr = Encoding.Default.GetString(br.ReadBytes(length));
                                string[] arr = addr.Split(':');
                                var buffer = new byte[0x8e];
                                EndPoint ep = new IPEndPoint(IPAddress.Parse(arr[0]), Int32.Parse(arr[1]));

                                srv = new GameServer();
                                srv.EndPoint = ep;
                                srv.Code = ep.ToString();

                                handler.Send(PrepareMessageToSend(serverWelcome()));

                                break;
                            }
                            case 0xd2: {
                                if (srv == null) {
                                    break;
                                }
                                byte pID = br.ReadByte();

                                byte pcount = br.ReadByte();
                                byte difficulty = br.ReadByte();
                                byte srvType = br.ReadByte();
                                byte mapSize = br.ReadByte();

                                if (pcount == 0xff && difficulty == 0xff && srvType == 0xff && mapSize == 0xff) {
                                    lock (registeredServers) {
                                        handler.Close(5);
                                        registeredServers.Remove(srv);
                                        srv = null;
                                        return;
                                        break;
                                    }
                                }

                                byte ln = br.ReadByte();

                                br.BaseStream.Seek(1, SeekOrigin.Current);
                                string mapName = Encoding.Default.GetString(br.ReadBytes(ln));

                                int idx = -1;
                                if (registeredServers.Contains(srv)) {
                                    idx = registeredServers.IndexOf(srv);
                                }
                                srv.PlayersCount = pcount;
                                srv.ServerType = (ServerType) srvType;
                                srv.Map = new GameMap();
                                srv.Map.Difficulty = (Difficulty) difficulty;
                                srv.Map.Name = mapName;
                                srv.Map.Height = srv.Map.Width = mapSize;

                                if (idx == -1) {
                                    registeredServers.Add(srv);
                                }
                                else {
                                    registeredServers[idx] = srv;
                                }
                                handler.Send(PrepareMessageToSend(null));
                                break;
                            }
                            case 0xd4: // arena results
                            {
                                LogEvent(EventType.DEBUG, "arena results not supported");
                                handler.Send(PrepareMessageToSend(null));

                                break;
                            }
                            case 0xE0: {
                                // chr_recv update character

                                ///structure:
                                /// 0x00 - packet type
                                /// 0x01 - size
                                /// 0x02 - charID 1
                                /// 0x06 - charID 2
                                /// 0x0A - log_size
                                /// 0x0B - chr_size
                                /// 0x12 - characterData
                                /// 0x12 + chrSize - log_name

                                byte size = br.ReadByte();
                                if (size == 1) {
                                    break;
                                }
                                Int32 characterID1 = br.ReadInt32();
                                Int32 characterID2 = br.ReadInt32();
                                Int32 logSize = br.ReadInt32();

                                byte chr_size = br.ReadByte();

                                byte[] chrData = br.ReadBytes(chr_size);
                                string loginIP = Encoding.Default.GetString(br.ReadBytes(logSize));

                                var chr = new HatCharacter(); // stub

                                if (chr == null) {
                                    chr = new HatCharacter();

                                    chr.IDs[0] = characterID1;
                                    chr.IDs[1] = characterID2;

                                    chr.CharacterData = chrData;
                                }
                                else {
                                    chr.CharacterData = chrData;
                                }

                                //saving

                                break;
                            }
                            case 0xcf: // chr_recv return character
                            {
                                ///structure:
                                /// 0x00 - packet type
                                /// 0x01 - size
                                /// 0x02 - charID 1
                                /// 0x06 - charID 2
                                /// 0x0A - log_size
                                /// 0x0B - chr_size
                                /// 0x12 - characterData
                                /// 0x12 + chrSize - log_name

                                byte size = br.ReadByte();
                                Int32 characterID1 = br.ReadInt32();
                                Int32 characterID2 = br.ReadInt32();
                                Int32 logSize = br.ReadInt32();

                                byte chr_size = br.ReadByte();

                                byte[] chrData = br.ReadBytes(chr_size);
                                string loginIP = Encoding.Default.GetString(br.ReadBytes(logSize));

                                var chr = new HatCharacter();

                                if (chr == null) {
                                    chr = new HatCharacter();

                                    chr.IDs[0] = characterID1;
                                    chr.IDs[1] = characterID2;

                                    chr.CharacterData = chrData;
                                }
                                else {
                                    chr.CharacterData = chrData;
                                }

                                //save

                                handler.Send(PrepareMessageToSend(simpleServerEntrance(loginIP)));

                                break;
                            }
                            case 0xd8: // accept character
                            case 0xd9: // reject character
                            case 0xE1: // server reconnect
                            case 0x64:
                            default: //keep alive
                                Console.WriteLine("default response send");

                                handler.Send(PrepareMessageToSend(null));
                                break;
                        }
                    }
                }
                try {
                    handler.Receive(buf);
                } catch {
                    return;
                }
            }
        }
        private void serverInfoListener(object parameters)
        {
            GameServer srv = null;

            object[] param   = null;
            Socket   handler = null;

            byte[] buf = null;
            try {
                param   = (object[])parameters;
                handler = (Socket)param[0];
                buf     = (byte[])param[1];
            } catch (Exception ex) {
                LogEvent(EventType.ERROR, ex, "server handler has incorrect parameters");
                if (handler != null)
                {
                    handler.Close(5);
                }
                return;
            }

            uint packetLength, unknown1;

            while (true)
            {
                using (var mem = new MemoryStream(buf)) {
                    mem.Seek(0, SeekOrigin.Begin);
                    var binRead = new BinaryReader(mem);

                    packetLength = binRead.ReadUInt32();
                    unknown1     = binRead.ReadUInt32();

                    var decoded = new byte[packetLength];

                    Buffer.BlockCopy(buf,
                                     (int)binRead.BaseStream.Position,
                                     decoded, 0, (int)packetLength);
                    Console.WriteLine("recieved packet");
                    decoded = NetworkHelper.PacketDecoding(decoded);
                    NetworkHelper.DumpArray(Console.OpenStandardOutput(), decoded);

                    using (var mstr = new MemoryStream(decoded)) {
                        var  br       = new BinaryReader(mstr);
                        byte packetID = br.ReadByte();

                        switch (packetID)
                        {
                        /// bad code warning!
                        /// section below contains processing of 2 in series
                        case 0xD1: {
                            //ident server
                            br.BaseStream.Seek(5, SeekOrigin.Begin);
                            byte length = br.ReadByte();
                            br.BaseStream.Seek(1, SeekOrigin.Current);
                            string   addr   = Encoding.Default.GetString(br.ReadBytes(length));
                            string[] arr    = addr.Split(':');
                            var      buffer = new byte[0x8e];
                            EndPoint ep     = new IPEndPoint(IPAddress.Parse(arr[0]), Int32.Parse(arr[1]));

                            srv          = new GameServer();
                            srv.EndPoint = ep;
                            srv.Code     = ep.ToString();

                            handler.Send(PrepareMessageToSend(serverWelcome()));

                            break;
                        }

                        case 0xd2: {
                            if (srv == null)
                            {
                                break;
                            }
                            byte pID = br.ReadByte();

                            byte pcount     = br.ReadByte();
                            byte difficulty = br.ReadByte();
                            byte srvType    = br.ReadByte();
                            byte mapSize    = br.ReadByte();

                            if (pcount == 0xff && difficulty == 0xff && srvType == 0xff && mapSize == 0xff)
                            {
                                lock (registeredServers) {
                                    handler.Close(5);
                                    registeredServers.Remove(srv);
                                    srv = null;
                                    return;

                                    break;
                                }
                            }

                            byte ln = br.ReadByte();

                            br.BaseStream.Seek(1, SeekOrigin.Current);
                            string mapName = Encoding.Default.GetString(br.ReadBytes(ln));

                            int idx = -1;
                            if (registeredServers.Contains(srv))
                            {
                                idx = registeredServers.IndexOf(srv);
                            }
                            srv.PlayersCount   = pcount;
                            srv.ServerType     = (ServerType)srvType;
                            srv.Map            = new GameMap();
                            srv.Map.Difficulty = (Difficulty)difficulty;
                            srv.Map.Name       = mapName;
                            srv.Map.Height     = srv.Map.Width = mapSize;

                            if (idx == -1)
                            {
                                registeredServers.Add(srv);
                            }
                            else
                            {
                                registeredServers[idx] = srv;
                            }
                            handler.Send(PrepareMessageToSend(null));
                            break;
                        }

                        case 0xd4:     // arena results
                        {
                            LogEvent(EventType.DEBUG, "arena results not supported");
                            handler.Send(PrepareMessageToSend(null));

                            break;
                        }

                        case 0xE0: {
                            // chr_recv update character

                            ///structure:
                            /// 0x00 - packet type
                            /// 0x01 - size
                            /// 0x02 - charID 1
                            /// 0x06 - charID 2
                            /// 0x0A - log_size
                            /// 0x0B - chr_size
                            /// 0x12 - characterData
                            /// 0x12 + chrSize - log_name

                            byte size = br.ReadByte();
                            if (size == 1)
                            {
                                break;
                            }
                            Int32 characterID1 = br.ReadInt32();
                            Int32 characterID2 = br.ReadInt32();
                            Int32 logSize      = br.ReadInt32();

                            byte chr_size = br.ReadByte();

                            byte[] chrData = br.ReadBytes(chr_size);
                            string loginIP = Encoding.Default.GetString(br.ReadBytes(logSize));

                            var chr = new HatCharacter();     // stub

                            if (chr == null)
                            {
                                chr = new HatCharacter();

                                chr.IDs[0] = characterID1;
                                chr.IDs[1] = characterID2;

                                chr.CharacterData = chrData;
                            }
                            else
                            {
                                chr.CharacterData = chrData;
                            }

                            //saving

                            break;
                        }

                        case 0xcf:     // chr_recv return character
                        {
                            ///structure:
                            /// 0x00 - packet type
                            /// 0x01 - size
                            /// 0x02 - charID 1
                            /// 0x06 - charID 2
                            /// 0x0A - log_size
                            /// 0x0B - chr_size
                            /// 0x12 - characterData
                            /// 0x12 + chrSize - log_name

                            byte  size         = br.ReadByte();
                            Int32 characterID1 = br.ReadInt32();
                            Int32 characterID2 = br.ReadInt32();
                            Int32 logSize      = br.ReadInt32();

                            byte chr_size = br.ReadByte();

                            byte[] chrData = br.ReadBytes(chr_size);
                            string loginIP = Encoding.Default.GetString(br.ReadBytes(logSize));

                            var chr = new HatCharacter();

                            if (chr == null)
                            {
                                chr = new HatCharacter();

                                chr.IDs[0] = characterID1;
                                chr.IDs[1] = characterID2;

                                chr.CharacterData = chrData;
                            }
                            else
                            {
                                chr.CharacterData = chrData;
                            }

                            //save

                            handler.Send(PrepareMessageToSend(simpleServerEntrance(loginIP)));

                            break;
                        }

                        case 0xd8:   // accept character
                        case 0xd9:   // reject character
                        case 0xE1:   // server reconnect
                        case 0x64:
                        default:     //keep alive
                            Console.WriteLine("default response send");

                            handler.Send(PrepareMessageToSend(null));
                            break;
                        }
                    }
                }
                try {
                    handler.Receive(buf);
                } catch {
                    return;
                }
            }
        }