Ejemplo n.º 1
0
 public void ConvertToField()
 {
     FieldInfo[] Fields = GetType().GetFields();
     foreach (FieldInfo Info in Fields)
     {
         ISerializableFieldAttribute[] FieldsAttr = Info.GetCustomAttributes(typeof(ISerializableFieldAttribute), true) as ISerializableFieldAttribute[];
         if (FieldsAttr != null && FieldsAttr.Length > 0)
         {
             ISerializableField Field = Activator.CreateInstance(FieldsAttr[0].GetSerializableType()) as ISerializableField;
             Field.Index      = FieldsAttr[0].Index;
             Field.val        = Info.GetValue(this);
             Field.PacketType = PacketProcessor.GetFieldType(Field);
             AddField(Field.Index, Field);
         }
     }
 }
Ejemplo n.º 2
0
        public static byte[] FieldToBytes(ISerializablePacket Packet, string FieldName)
        {
            FieldInfo Info = Packet.GetType().GetField(FieldName);

            ISerializableFieldAttribute[] FieldsAttr = Info.GetCustomAttributes(typeof(ISerializableFieldAttribute), true) as ISerializableFieldAttribute[];
            if (FieldsAttr != null && FieldsAttr.Length > 0)
            {
                ISerializableField Field = Activator.CreateInstance(FieldsAttr[0].GetSerializableType()) as ISerializableField;
                Field.Index      = FieldsAttr[0].Index;
                Field.val        = Info.GetValue(Packet);
                Field.PacketType = PacketProcessor.GetFieldType(Field);

                PacketOutStream Str = new PacketOutStream();
                Field.Serialize(ref Str, true);
                byte[] Result = Str.ToArray();
                return(Result);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public override void Deserialize(ref PacketInStream Data)
        {
            long ListData = Data.ReadEncoded7Bit();
            int  ListType;
            int  ListCount;

            PacketInStream.Decode2Parameters(ListData, out ListType, out ListCount);
            List <ISerializableField> Fields = new List <ISerializableField>();

            Log.Debug("Packet", "------> List : " + ListType + "(" + ListCount + ")");
            for (int i = 0; i < ListCount; ++i)
            {
                ISerializableField Field = PacketProcessor.GetFieldType((EPacketFieldType)ListType);
                if (Field != null)
                {
                    Field.Deserialize(ref Data);
                    Fields.Add(Field);
                }
            }

            val = Fields;
        }
Ejemplo n.º 4
0
        public override void Deserialize(ref PacketInStream Data)
        {
            long Opcode = Data.ReadEncoded7Bit();
            PacketHandlerDefinition Handler = PacketProcessor.GetPacketHandler(Opcode);
            ISerializablePacket     Packet  = Activator.CreateInstance(Handler.GetClass()) as ISerializablePacket;

            ISerializableField Field = null;

            Log.Debug("Packet", "----------------------> New " + Opcode.ToString("X8"));
            Packet.Opcode = Opcode;

            while ((Field = PacketProcessor.ReadField(ref Data)) != null)
            {
                Log.Debug("Packet", "------> ++T : " + Field.PacketType);
                Packet.AddField(Field.Index, Field);
            }

            Log.Debug("Packet", "----------------------> End ");

            Packet.ApplyToFieldInfo();
            val = Packet;
        }
Ejemplo n.º 5
0
        public override bool Serialize(ref PacketOutStream Data, bool Force)
        {
            Log.Debug("WriteList", "Serialize : " + val);

            if (val == null)
            {
                return(false);
            }

            if (val is List <ISerializablePacket> )
            {
                List <ISerializablePacket> Packets = val as List <ISerializablePacket>;

                /*if (Packets.Count <= 0)
                 *  return false;*/

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Packet, Packets.Count);
                Data.WriteEncoded7Bit(ListData);

                foreach (ISerializablePacket Packet in Packets)
                {
                    PacketProcessor.WritePacket(ref Data, Packet, false, true, true);
                }
            }
            else if (val is ISerializablePacket[])
            {
                ISerializablePacket[] Packets = val as ISerializablePacket[];

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Packet, Packets.Length);
                Data.WriteEncoded7Bit(ListData);

                foreach (ISerializablePacket Packet in Packets)
                {
                    PacketProcessor.WritePacket(ref Data, Packet, false, true, true);
                }
            }
            else if (val is List <ulong> )
            {
                List <ulong> Values = val as List <ulong>;

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Unsigned7BitEncoded, Values.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Values.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, EPacketFieldType.Unsigned7BitEncoded, (ulong)Values[i], true);
                }
            }
            else if (val is List <long> )
            {
                List <long> Values = val as List <long>;

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Unsigned7BitEncoded, Values.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Values.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, EPacketFieldType.Unsigned7BitEncoded, (long)Values[i], true);
                }
            }
            else if (val is List <uint> )
            {
                List <uint> Values = val as List <uint>;

                /*if (Values.Count <= 0)
                 *  return false;*/

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Raw4Bytes, Values.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Values.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, EPacketFieldType.Raw4Bytes, (uint)Values[i], true);
                }
            }
            else if (val is List <float> )
            {
                List <float> Values = val as List <float>;

                /*if (Values.Count <= 0)
                 *  return false;*/

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.Raw4Bytes, Values.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Values.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, EPacketFieldType.Raw4Bytes, Values[i], true);
                }
            }
            else if (val is List <string> )
            {
                List <string> Strs = val as List <string>;

                /*if (Strs.Count <= 0)
                 *  return false;*/

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)EPacketFieldType.ByteArray, Strs.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Strs.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, EPacketFieldType.ByteArray, (string)Strs[i], true);
                }

                return(true);
            }
            else if (val is List <ISerializableField> )
            {
                List <ISerializableField> Strs = val as List <ISerializableField>;

                if (Strs.Count <= 0)
                {
                    return(false);
                }

                ISerializableField Field = Strs[0];

                long ListData;
                PacketOutStream.Encode2Parameters(out ListData, (int)Field.PacketType, Strs.Count);
                Data.WriteEncoded7Bit(ListData);

                for (int i = 0; i < Strs.Count; ++i)
                {
                    PacketProcessor.WriteField(ref Data, Strs[i].PacketType, Strs[i].val, true);
                }

                return(true);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 6
0
        public bool ReadPacket()
        {
            if (Received.Count <= 0)
            {
                return(false);
            }

            if (!ReceiveCompress && !Crypted)
            {
                PacketInStream Packet = new PacketInStream(Received.ToArray(), Received.Count);
                long           Size   = Packet.ReadEncoded7Bit();

                if (Size > Packet.Length)
                {
                    return(false);
                }

                Received.RemoveRange(0, (int)(Packet.Position + Size));

                ISerializablePacket Pack = PacketProcessor.ReadPacket(ref Packet);
                if (Pack != null)
                {
                    Pack.OnRead(this);
                }

                return(true);
            }

            if (Crypted)
            {
                byte[] Packet = Received.ToArray();
                Received.Clear();
                Decrypt(Packet);
            }

            if (ReceiveCompress)
            {
                int End = -1;
                while ((End = GetEndCompression(ref Decrypted)) >= 0)
                {
                    byte[] ToUnCompress = new byte[End];
                    Buffer.BlockCopy(Decrypted.ToArray(), 0, ToUnCompress, 0, End);
                    Decrypted.RemoveRange(0, End);

                    byte[] Result = UnCompress(ToUnCompress);

                    Log.Dump("Decrypted", Result, 0, Result.Length);
                    PacketInStream Packet = new PacketInStream(Result, Result.Length);
                    long           Size   = Packet.ReadEncoded7Bit();

                    if (Size > Packet.Length)
                    {
                        Log.Error("ReadPacket", "Size > Packet.Lenght,Size=" + Size + ",Lenght=" + Packet.Length);
                        Packet.Dispose();
                        continue;
                    }

                    ISerializablePacket Pack = PacketProcessor.ReadPacket(ref Packet);
                    if (Pack != null)
                    {
                        Pack.OnRead(this);
                    }

                    Packet.Dispose();
                }
            }

            return(false);
        }