Пример #1
0
        public void Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            var contentBuffer = convert.Deserialize <IByteBuffer>(buffer, options);

            headHandler.ReadHead(contentBuffer, options);
            Temp = convert.Deserialize <IDictionary <string, IByteBuffer> >(contentBuffer, options);
        }
Пример #2
0
        public override IDictionary <K, V> Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.Map:
            {
                int size = buffer.ReadInt();
                var dict = new Dictionary <K, V>(size);
                var op   = options.Create();
                for (int i = 0; i < size; ++i)
                {
                    headHandler.ReadHead(buffer, op);
                    var k = convert.Deserialize <K>(buffer, op);
                    headHandler.ReadHead(buffer, op);
                    var v = convert.Deserialize <V>(buffer, op);
                    if (dict.ContainsKey(k))
                    {
                        dict[k] = v;
                    }
                    else
                    {
                        dict.Add(k, v);
                    }
                }
                return(dict);
            }

            default:
                throw new TarsDecodeException($"DictionaryTarsConvert can not deserialize {options}");
            }
        }
Пример #3
0
 public void Put(string name, object obj, Type type, TarsConvertOptions options)
 {
     if (name == null)
     {
         throw new ArgumentException("put key can not be null");
     }
     if (obj != null)
     {
         var buf = Unpooled.Buffer(128);
         options.Tag = 0;
         convert.Serialize(obj, type, buf, options);
         if (buf.ReadableBytes == 0)
         {
             return;
         }
         if (Temp.ContainsKey(name))
         {
             Temp[name] = buf;
         }
         else
         {
             Temp.Add(name, buf);
         }
     }
     else if (Temp.ContainsKey(name))
     {
         Temp.Remove(name);
     }
 }
Пример #4
0
        public override void Serialize(T?obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            var old = options.HasValue;

            options.HasValue = obj.HasValue;
            convert.Serialize(obj.GetValueOrDefault(), buffer, options);
            options.HasValue = old;
        }
Пример #5
0
        private void SerializeV2OrV3(Response obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            options.Tag = 3;
            intConvert.Serialize(obj.MessageType, buffer, options);
            options.Tag = 4;
            intConvert.Serialize(obj.RequestId, buffer, options);
            options.Tag = 5;
            stringConvert.Serialize(obj.ServantName, buffer, options);
            options.Tag = 6;
            stringConvert.Serialize(obj.FuncName, buffer, options);
            if (options.Version == TarsCodecsVersion.V2)
            {
                var uni = new UniAttributeV2(convertRoot, headHandler)
                {
                    Temp = new Dictionary <string, IDictionary <string, IByteBuffer> >()
                };
                var type = obj.ReturnValueType.ParameterType;
                if (obj.ReturnValue != null && type != typeof(void))
                {
                    uni.Put(string.Empty, obj.ReturnValue, obj.ReturnValueType.ParameterType, options);
                }

                for (int i = 0; i < obj.ReturnParameterTypes.Length; i++)
                {
                    var pt = obj.ReturnParameterTypes[i];
                    uni.Put(pt.Name, obj.ReturnParameters[i], pt.ParameterType, options);
                }
                options.Tag = 7;
                uni.Serialize(buffer, options);
            }
            else
            {
                var uni = new UniAttributeV3(convertRoot, headHandler)
                {
                    Temp = new Dictionary <string, IByteBuffer>()
                };
                var type = obj.ReturnValueType.ParameterType;
                if (obj.ReturnValue != null && type != typeof(void))
                {
                    uni.Put(string.Empty, obj.ReturnValue, obj.ReturnValueType.ParameterType, options);
                }
                for (int i = 0; i < obj.ReturnParameterTypes.Length; i++)
                {
                    var pt = obj.ReturnParameterTypes[i];
                    uni.Put(pt.Name, obj.ReturnParameters[i], pt.ParameterType, options);
                }
                options.Tag = 7;
                uni.Serialize(buffer, options);
            }
            options.Tag = 8;
            intConvert.Serialize(obj.Timeout, buffer, options);
            options.Tag = 9;
            dictConvert.Serialize(obj.Context, buffer, options);
            options.Tag = 10;
            dictConvert.Serialize(obj.Status, buffer, options);
        }
Пример #6
0
        public override T?Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            var old = options.HasValue;

            options.HasValue = false;
            var value = convert.Deserialize(buffer, options);

            options.HasValue = old;
            return(options.HasValue ? (T?)value : null);
        }
Пример #7
0
        public void Serialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            var buf    = Unpooled.Buffer(128);
            var oldTag = options.Tag;

            options.Tag = 0;
            convert.Serialize(Temp, buf, options);
            options.Tag = oldTag;
            convert.Serialize(buf, buffer, options);
        }
Пример #8
0
        public override long Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.Long:
                return(buffer.ReadLong());

            default:
                return(convert.Deserialize(buffer, options));
            }
        }
Пример #9
0
        public void ReadHead(IByteBuffer buffer, TarsConvertOptions options)
        {
            byte b        = buffer.ReadByte();
            byte tarsType = (byte)(b & 15);
            int  tag      = ((b & (15 << 4)) >> 4);

            if (tag == 15)
            {
                tag = buffer.ReadByte() & 0x00ff;
            }
            options.Tag      = tag;
            options.TarsType = tarsType;
        }
Пример #10
0
        public override IByteBuffer Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.ByteArray:
                headHandler.ReadHead(buffer, options);
                int size = buffer.ReadInt();
                return(buffer.ReadBytes(size));

            default:
                throw new TarsDecodeException($"ByteBufferTarsConvert can not deserialize {options}");
            }
        }
Пример #11
0
        public override double Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.Double:
                return(buffer.ReadDouble());

            case TarsStructType.Zero:
                return(0x0);

            default:
                throw new TarsDecodeException($"DoubleTarsConvert can not deserialize {options}");
            }
        }
Пример #12
0
 public override void Serialize(T obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 2);
         headHandler.WriteHead(buffer, TarsStructType.StructBegin, options.Tag);
         foreach (var kv in properties)
         {
             options.Tag = kv.Key;
             convert.Serialize(kv.Value.GetValue(obj), kv.Value.GetMemberInfo().PropertyType, buffer, options);
         }
         headHandler.Reserve(buffer, 2);
         headHandler.WriteHead(buffer, TarsStructType.StructEnd, options.Tag);
     }
 }
Пример #13
0
        public override void Serialize(IByteBuffer obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            if (obj == null)
            {
                return;
            }

            int len = obj.ReadableBytes;

            headHandler.Reserve(buffer, 8 + len);
            headHandler.WriteHead(buffer, TarsStructType.ByteArray, options.Tag);
            headHandler.WriteHead(buffer, TarsStructType.Byte, 0);
            buffer.WriteInt(len);
            buffer.WriteBytes(obj);
        }
Пример #14
0
 public override void Serialize(IList <T> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 8);
         headHandler.WriteHead(buffer, TarsStructType.List, options.Tag);
         buffer.WriteInt(obj.Count);
         var op = options.Create();
         op.Tag = 0;
         foreach (var item in obj)
         {
             convert.Serialize(item, buffer, op);
         }
     }
 }
Пример #15
0
 public override void Serialize(IDictionary <K, V> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 8);
         headHandler.WriteHead(buffer, TarsStructType.Map, options.Tag);
         buffer.WriteInt(obj.Count);
         foreach (var kv in obj)
         {
             options.Tag = 0;
             convert.Serialize(kv.Key, buffer, options);
             options.Tag = 1;
             convert.Serialize(kv.Value, buffer, options);
         }
     }
 }
Пример #16
0
 public override void Serialize(int obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     if (obj >= short.MinValue && obj <= short.MaxValue)
     {
         convert.Serialize((short)obj, buffer, options);
     }
     else
     {
         headHandler.Reserve(buffer, 6);
         headHandler.WriteHead(buffer, TarsStructType.Int, options.Tag);
         buffer.WriteInt(obj);
     }
 }
Пример #17
0
 public override void Serialize(long obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     if (obj >= int.MinValue && obj <= int.MaxValue)
     {
         convert.Serialize((int)obj, buffer, options);
     }
     else
     {
         headHandler.Reserve(buffer, 10);
         headHandler.WriteHead(buffer, TarsStructType.Long, options.Tag);
         buffer.WriteLong(obj);
     }
 }
Пример #18
0
 public override void Serialize(double obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     headHandler.Reserve(buffer, 10);
     if (obj == 0)
     {
         headHandler.WriteHead(buffer, TarsStructType.Zero, options.Tag);
     }
     else
     {
         headHandler.WriteHead(buffer, TarsStructType.Double, options.Tag);
         buffer.WriteDouble(obj);
     }
 }
Пример #19
0
        public override Response Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            var resp = new Response();

            headHandler.ReadHead(buffer, options);
            options.Version = resp.Version = shortConvert.Deserialize(buffer, options);
            switch (options.Version)
            {
            case TarsCodecsVersion.V2:
            case TarsCodecsVersion.V3:
                DeserializeV2OrV3(buffer, options, resp);
                break;

            default:
                DeserializeV1(buffer, options, resp);
                break;
            }
            return(resp);
        }
Пример #20
0
        public override void Serialize(Response obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            options.Tag     = 1;
            options.Version = obj.Version;
            shortConvert.Serialize(obj.Version, buffer, options);
            options.Tag = 2;
            byteConvert.Serialize(obj.PacketType, buffer, options);
            switch (options.Version)
            {
            case TarsCodecsVersion.V2:
            case TarsCodecsVersion.V3:
                SerializeV2OrV3(obj, buffer, options);
                break;

            default:
                SerializeV1(obj, buffer, options);
                break;
            }
        }
Пример #21
0
        public override string Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.String1:
            {
                int len = buffer.ReadByte();
                return(buffer.ReadString(len, options.Encoding));
            }

            case TarsStructType.String4:
            {
                int len = buffer.ReadInt();
                return(buffer.ReadString(len, options.Encoding));
            }

            default:
                throw new TarsDecodeException($"StringTarsConvert can not deserialize {options}");
            }
        }
Пример #22
0
 public override T Deserialize(IByteBuffer buffer, TarsConvertOptions options)
 {
     if (options.TarsType == TarsStructType.StructBegin)
     {
         var op = options.Create();
         headHandler.ReadHead(buffer, op);
         var result = new T();
         while (op.TarsType != TarsStructType.StructEnd)
         {
             var p = properties[op.Tag];
             p.SetValue(result, convert.Deserialize(buffer, p.GetMemberInfo().PropertyType, op));
             headHandler.ReadHead(buffer, op);
         }
         return(result);
     }
     else
     {
         throw new TarsDecodeException($"StructTarsConvert can not deserialize {options}");
     }
 }
Пример #23
0
        public override void Serialize(string obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            if (obj == null)
            {
                return;
            }
            var count = options.Encoding.GetByteCount(obj);

            if (count > 255)
            {
                headHandler.WriteHead(buffer, TarsStructType.String4, options.Tag);
                buffer.WriteInt(count);
                buffer.WriteString(obj, options.Encoding);
            }
            else
            {
                headHandler.WriteHead(buffer, TarsStructType.String1, options.Tag);
                buffer.WriteByte(count);
                buffer.WriteString(obj, options.Encoding);
            }
        }
Пример #24
0
        public override IList <T> Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            switch (options.TarsType)
            {
            case TarsStructType.List:
            {
                int size = buffer.ReadInt();
                var list = new List <T>(size);
                var op   = options.Create();
                for (int i = 0; i < size; ++i)
                {
                    headHandler.ReadHead(buffer, op);
                    var t = convert.Deserialize(buffer, op);
                    list.Add(t);
                }
                return(list);
            }

            default:
                throw new TarsDecodeException($"DictionaryTarsConvert can not deserialize {options}");
            }
        }
Пример #25
0
        private void SerializeV1(Response obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            options.Tag = 3;
            intConvert.Serialize(obj.RequestId, buffer, options);
            options.Tag = 4;
            intConvert.Serialize(obj.MessageType, buffer, options);
            options.Tag = 5;
            intConvert.Serialize((int)obj.ResultStatusCode, buffer, options);
            var contentBuffer = Unpooled.Buffer(128);

            if (obj.ResultStatusCode == RpcStatusCode.ServerSuccess)
            {
                var type = obj.ReturnValueType.ParameterType;
                if (type != typeof(void))
                {
                    //0的位置是专门给返回值用的
                    options.Tag = 0;
                    convertRoot.Serialize(obj.ReturnValue, type, contentBuffer, options);
                }
                int outResIndex = 0;
                foreach (var item in obj.ReturnParameterTypes)
                {
                    options.Tag = item.Position + 1;
                    convertRoot.Serialize(obj.ReturnParameters[outResIndex++], item.ParameterType, contentBuffer, options);
                }
            }
            options.Tag = 6;
            bufferConvert.Serialize(contentBuffer, buffer, options);
            options.Tag = 7;
            dictConvert.Serialize(obj.Status, buffer, options);
            if (obj.ResultStatusCode != RpcStatusCode.ServerSuccess)
            {
                options.Tag = 8;
                stringConvert.Serialize(obj.ResultDesc, buffer, options);
            }
            options.Tag = 9;
            dictConvert.Serialize(obj.Context, buffer, options);
        }
Пример #26
0
 public override void Serialize(Dictionary <K, V> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     convert.Serialize(obj as IDictionary <K, V>, buffer, options);
 }
Пример #27
0
 public override Dictionary <K, V> Deserialize(IByteBuffer buffer, TarsConvertOptions options)
 {
     return(convert.Deserialize(buffer, options) as Dictionary <K, V>);
 }
Пример #28
0
 public override void Serialize(Task <T> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     convert.Serialize(obj.Result, buffer, options);
 }
Пример #29
0
 public override Task Deserialize(IByteBuffer buffer, TarsConvertOptions options)
 {
     return(Task.CompletedTask);
 }
Пример #30
0
 public override void Serialize(Task obj, IByteBuffer buffer, TarsConvertOptions options)
 {
 }