Пример #1
0
        public override byte[] Serialize(object input, TLPropertyAttribute attribute)
        {
            TLBytes bytes       = (TLBytes)input;
            int     len         = bytes.Content.Length;
            int     startOffset = 1;

            List <byte> list = new List <byte>();

            if (len >= 254)
            {
                list.Add(254);
                list.Add((byte)(len & 0xFF));
                list.Add((byte)((len >> 8) & 0xFF));
                list.Add((byte)((len >> 16) & 0xFF));
                startOffset = 4;
            }
            else
            {
                list.Add((byte)len);
            }

            list.AddRange(bytes.Content);

            int offset = (len + startOffset) % 4;

            if (offset != 0)
            {
                int offsetCount = 4 - offset;
                list.AddRange(Enumerable.Repeat((byte)0x00, offsetCount));
            }

            return(list.ToArray());
        }
Пример #2
0
        public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
        {
            int count       = byteList.ReadByte();
            int startOffset = 1;

            if (count >= 254)
            {
                count       = byteList.ReadByte();
                count      += byteList.ReadByte() << 8;
                count      += byteList.ReadByte() << 16;
                startOffset = 4;
            }

            TLBytes bytes = new TLBytes(byteList.Take(count).ToArray());

            byteList.RemoveRange(0, count);

            int offset = (count + startOffset) % 4;

            if (offset != 0)
            {
                int offsetCount = 4 - offset;
                for (int i = 0; i < offsetCount; i++)
                {
                    byteList.ReadByte();
                }
            }

            return(bytes);
        }
Пример #3
0
        public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
        {
            int arrCount = attribute.ArrayLength;

            byte[] arr = byteList.Take(arrCount).ToArray();
            byteList.RemoveRange(0, arrCount);
            return(arr);
        }
Пример #4
0
        public static byte[] Serialize <T>(T value, TLPropertyAttribute attribute = null)
        {
            Type type = typeof(T);

            if (type.BaseType == typeof(TLObject))
            {
                type = typeof(TLObject);
            }

            return(SerializerDictionary[type].Serialize(value, attribute));
        }
Пример #5
0
        public static object Deserialize(List <byte> byteList, Type type, TLPropertyAttribute attribute = null)
        {
            if (type.BaseType == typeof(TLObject))
            {
                return(TLObjectSerializer.Deserialize(byteList, type));
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(TLVector <>))
            {
                type = typeof(TLVector <>);
            }

            return(SerializerDictionary[type].Deserialize(byteList, attribute));
        }
Пример #6
0
        public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
        {
            UInt32 value = (UInt32)TLRootSerializer.Deserialize(byteList, typeof(UInt32));

            if (value == BoolTrue)
            {
                return(true);
            }
            if (value == BoolFalse)
            {
                return(false);
            }

            throw new ArgumentException("Unrecognized bool sequence.");
        }
Пример #7
0
        public override byte[] Serialize(object input, TLPropertyAttribute attribute)
        {
            TLObject obj = (TLObject)input;

            List <byte> list = obj.GetTLProperties()
                               .Select(x => TLRootSerializer.Serialize(x.GetValue(obj), attribute))
                               .SelectMany(x => x)
                               .ToList();

            UInt32 classId = obj.GetClassId();

            if (classId != 0)
            {
                list.InsertRange(0, TLRootSerializer.Serialize(classId));
            }

            return(list.ToArray());
        }
Пример #8
0
        public override byte[] Serialize(object input, TLPropertyAttribute attribute)
        {
            dynamic vector = input;

            UInt32 classId = GetSerializerType().GetClassId();
            int    len     = vector.Content.Count;

            List <byte> byteList = new List <byte>();

            byteList.AddRange(TLRootSerializer.Serialize(classId));
            byteList.AddRange(TLRootSerializer.Serialize(len));

            foreach (dynamic d in vector.Content)
            {
                byteList.AddRange(TLRootSerializer.Serialize(d));
            }

            return(byteList.ToArray());
        }
Пример #9
0
        public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
        {
            UInt32 expectedClassId = GetSerializerType().GetClassId();
            UInt32 classId         = (UInt32)TLRootSerializer.Deserialize(byteList, typeof(UInt32));

            if (expectedClassId != classId)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "{0} =/= {1}", expectedClassId, classId));
            }

            dynamic vector = Activator.CreateInstance(typeof(TLVector <>).MakeGenericType(attribute.VectorType));

            int len = (int)TLRootSerializer.Deserialize(byteList, typeof(Int32));

            for (int i = 0; i < len; i++)
            {
                vector.Content.Add((dynamic)TLRootSerializer.Deserialize(byteList, attribute.VectorType));
            }

            return(vector);
        }
Пример #10
0
 public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
 {
     byte[] arr = byteList.Take(4).ToArray();
     byteList.RemoveRange(0, 4);
     return(BitConverter.ToUInt32(arr, 0));
 }
Пример #11
0
 public override byte[] Serialize(object input, TLPropertyAttribute attribute)
 {
     return(BitConverter.GetBytes((UInt32)input));
 }
Пример #12
0
 public override byte[] Serialize(object input, TLPropertyAttribute attribute)
 {
     return(TLRootSerializer.Serialize((bool)input ? BoolTrue : BoolFalse));
 }
Пример #13
0
 public override byte[] Serialize(object input, TLPropertyAttribute attribute)
 {
     return(TLRootSerializer.Serialize(new TLBytes(Encoding.UTF8.GetBytes((string)input))));
 }
Пример #14
0
 public override byte[] Serialize(object input, TLPropertyAttribute attribute)
 {
     return((byte[])input);
 }
Пример #15
0
 public abstract byte[] Serialize(object input, TLPropertyAttribute attribute);
Пример #16
0
 public abstract object Deserialize(List <byte> byteList, TLPropertyAttribute attribute);
Пример #17
0
 public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
 {
     throw new NotSupportedException("Use static Deserialize<T>(List<byte>) or Deserialize(List<byte>, Type)");
 }
Пример #18
0
        public override object Deserialize(List <byte> byteList, TLPropertyAttribute attribute)
        {
            TLBytes bytes = (TLBytes)TLRootSerializer.Deserialize(byteList, typeof(TLBytes));

            return(Encoding.UTF8.GetString(bytes.Content));
        }