public void SendSerialized(ISerializablePacket Packet) { PacketOutStream Out = PacketProcessor.WritePacket(Packet); byte[] ToSend = Out.ToArray(); SendTCP(ToSend); List <Cache_Info> Entries = CacheMgr.Instance.GetBinCache(Packet.Opcode, false); foreach (Cache_Info Entry in Entries) { SendCache(Entry.Type, (uint)Entry.CacheId); } }
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); } } }
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.Success("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; }
public override void Deserialize(ref PacketInStream Data) { long Opcode = Data.ReadEncoded7Bit(); PacketHandlerDefinition Handler = PacketProcessor.GetPacketHandler(Opcode); ISerializablePacket Packet = Activator.CreateInstance(Handler.GetClass()) as ISerializablePacket; Packet.Opcode = Opcode; ISerializableField Field = null; Log.Debug("Packet", "----------------------> New " + Opcode.ToString("X8")); while ((Field = PacketProcessor.ReadField(ref Data)) != null) { Log.Success("Packet", "------> ++T : " + Field.PacketType); Packet.AddField(Field.Index, Field); } Log.Debug("Packet", "----------------------> End "); Packet.ApplyToFieldInfo(); val = Packet; }
public override bool Serialize(ref PacketOutStream Data) { Log.Success("WriteList", "Serialize : " + val); 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 List <long> ) { List <long> Values = val as List <long>; /*if (Values.Count <= 0) * return false;*/ 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]); } } 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]); } } 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]); } } 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]); } 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); } return(true); } else { return(false); } return(true); }
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); List <Cache_Info> Entries = CacheMgr.Instance.GetBinCache(Pack.Opcode, true); foreach (Cache_Info Entry in Entries) { SendCache(Entry.Type, (uint)Entry.CacheId); } } 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); continue; } ISerializablePacket Pack = PacketProcessor.ReadPacket(ref Packet); if (Pack != null) { Pack.OnRead(this); List <Cache_Info> Entries = CacheMgr.Instance.GetBinCache(Pack.Opcode, true); foreach (Cache_Info Entry in Entries) { SendCache(Entry.Type, (uint)Entry.CacheId); } } } } return(false); }
public override bool Serialize(ref PacketOutStream Data) { return(PacketProcessor.WritePacket(ref Data, (ISerializablePacket)val, false, true, true)); }