예제 #1
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);
        }
예제 #2
0
        private void DeserializeV2OrV3(IByteBuffer buffer, TarsConvertOptions options, Response resp)
        {
            while (buffer.IsReadable())
            {
                headHandler.ReadHead(buffer, options);
                switch (options.Tag)
                {
                case 2:
                    resp.PacketType = byteConvert.Deserialize(buffer, options);
                    break;

                case 3:
                    resp.MessageType = intConvert.Deserialize(buffer, options);
                    break;

                case 4:
                    resp.RequestId = intConvert.Deserialize(buffer, options);
                    break;

                case 5:
                    resp.ServantName = stringConvert.Deserialize(buffer, options);
                    break;

                case 6:
                    resp.FuncName = stringConvert.Deserialize(buffer, options);
                    var(method, isOneway, outParameters, codec, version, serviceType) = rpcMetadata.FindRpcMethod(resp.ServantName, resp.FuncName);
                    resp.ReturnValueType      = method.ReturnParameter;
                    resp.ReturnParameterTypes = outParameters;
                    resp.ReturnParameters     = new object[outParameters.Length];
                    break;

                case 7 when options.Version == TarsCodecsVersion.V2:
                {
                    var uni = new UniAttributeV2(convertRoot, headHandler);
                    uni.Deserialize(buffer, options);
                    if (uni.Temp.ContainsKey(string.Empty))
                    {
                        var buf = uni.Temp[string.Empty].Values.First();
                        headHandler.ReadHead(buf, options);
                        resp.ReturnValue = convertRoot.Deserialize(buf, resp.ReturnValueType.ParameterType, options);
                    }
                    else if (resp.ReturnValueType.ParameterType == typeof(Task))
                    {
                        resp.ReturnValue = Task.CompletedTask;
                    }
                    for (int i = 0; i < resp.ReturnParameterTypes.Length; i++)
                    {
                        var pt  = resp.ReturnParameterTypes[i];
                        var buf = uni.Temp[pt.Name].Values.First();
                        headHandler.ReadHead(buf, options);
                        resp.ReturnParameters[i] = convertRoot.Deserialize(buf, pt.ParameterType, options);
                    }
                }
                break;

                case 7 when options.Version == TarsCodecsVersion.V3:
                {
                    var uni = new UniAttributeV3(convertRoot, headHandler);
                    uni.Deserialize(buffer, options);
                    if (uni.Temp.ContainsKey(string.Empty))
                    {
                        var buf = uni.Temp[string.Empty];
                        headHandler.ReadHead(buf, options);
                        resp.ReturnValue = convertRoot.Deserialize(buf, resp.ReturnValueType.ParameterType, options);
                    }
                    else if (resp.ReturnValueType.ParameterType == typeof(Task))
                    {
                        resp.ReturnValue = Task.CompletedTask;
                    }
                    for (int i = 0; i < resp.ReturnParameterTypes.Length; i++)
                    {
                        var pt  = resp.ReturnParameterTypes[i];
                        var buf = uni.Temp[pt.Name];
                        headHandler.ReadHead(buf, options);
                        resp.ReturnParameters[i] = convertRoot.Deserialize(buf, pt.ParameterType, options);
                    }
                }
                break;

                case 8:
                    resp.Timeout = intConvert.Deserialize(buffer, options);
                    break;

                case 9:
                    resp.Context = dictConvert.Deserialize(buffer, options);
                    break;

                case 10:
                    resp.Status = dictConvert.Deserialize(buffer, options);
                    break;
                }
            }
        }
예제 #3
0
        public override Request Deserialize(IByteBuffer buffer, TarsConvertOptions options)
        {
            var req = new Request();
            var tag = options.Tag = 1;

            while (tag != 0 && buffer.IsReadable())
            {
                headHandler.ReadHead(buffer, options);
                tag = options.Tag;
                switch (tag)
                {
                case 1:
                    req.Version     = shortConvert.Deserialize(buffer, options);
                    options.Version = req.Version;
                    break;

                case 2:
                    req.PacketType = byteConvert.Deserialize(buffer, options);
                    break;

                case 3:
                    req.MessageType = intConvert.Deserialize(buffer, options);
                    break;

                case 4:
                    req.RequestId = intConvert.Deserialize(buffer, options);
                    break;

                case 5:
                    req.ServantName = stringConvert.Deserialize(buffer, options);
                    break;

                case 6:
                    req.FuncName = stringConvert.Deserialize(buffer, options);
                    var(method, isOneway, outParameters, codec, version, serviceType) = rpcMetadata.FindRpcMethod(req.ServantName, req.FuncName);
                    req.IsOneway             = isOneway;
                    req.Mehtod               = method;
                    req.ReturnParameterTypes = outParameters;
                    req.ServiceType          = serviceType;
                    req.ParameterTypes       = method.GetParameters();
                    break;

                case 7 when options.Version == TarsCodecsVersion.V1:
                {
                    var contentBuffer = bufferConvert.Deserialize(buffer, options);
                    req.Parameters = new object[req.ParameterTypes.Length];
                    while (contentBuffer.IsReadable())
                    {
                        headHandler.ReadHead(contentBuffer, options);
                        var index = options.Tag - 1;
                        var type  = req.ParameterTypes[index];
                        req.Parameters[index] = convertRoot.Deserialize(contentBuffer, type.ParameterType, options);
                    }
                }
                break;

                case 7 when options.Version == TarsCodecsVersion.V2:
                {
                    var uni = new UniAttributeV2(convertRoot, headHandler);
                    uni.Deserialize(buffer, options);
                    req.Parameters = new object[req.ParameterTypes.Length];
                    foreach (var pt in req.ParameterTypes)
                    {
                        var pBuffer = uni.Temp[pt.Name].Values.First();
                        headHandler.ReadHead(pBuffer, options);
                        req.Parameters[pt.Position] = convertRoot.Deserialize(pBuffer, pt.ParameterType, options);
                    }
                }
                break;

                case 7 when options.Version == TarsCodecsVersion.V3:
                {
                    var uni = new UniAttributeV3(convertRoot, headHandler);
                    uni.Deserialize(buffer, options);
                    req.Parameters = new object[req.ParameterTypes.Length];
                    foreach (var pt in req.ParameterTypes)
                    {
                        var pBuffer = uni.Temp[pt.Name];
                        headHandler.ReadHead(pBuffer, options);
                        req.Parameters[pt.Position] = convertRoot.Deserialize(pBuffer, pt.ParameterType, options);
                    }
                }
                break;

                case 8:
                    req.Timeout = intConvert.Deserialize(buffer, options);
                    break;

                case 9:
                    req.Context = dictConvert.Deserialize(buffer, options);
                    break;

                case 10:
                    req.Status = dictConvert.Deserialize(buffer, options);
                    tag        = 0;
                    break;

                default:
                    break;
                }
            }
            return(req);
        }
예제 #4
0
        public override void Serialize(Request 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);
            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);

            switch (options.Version)
            {
            case TarsCodecsVersion.V3:
            {
                var uni = new UniAttributeV3(convertRoot, headHandler)
                {
                    Temp = new Dictionary <string, IByteBuffer>(obj.ParameterTypes.Length)
                };
                for (int i = 0; i < obj.ParameterTypes.Length; i++)
                {
                    var p = obj.ParameterTypes[i];
                    uni.Put(p.Name, obj.Parameters[i], p.ParameterType, options);
                }

                options.Tag = 7;
                uni.Serialize(buffer, options);
            }
            break;

            case TarsCodecsVersion.V2:
            {
                var uni = new UniAttributeV2(convertRoot, headHandler)
                {
                    Temp = new Dictionary <string, IDictionary <string, IByteBuffer> >(obj.ParameterTypes.Length)
                };
                for (int i = 0; i < obj.ParameterTypes.Length; i++)
                {
                    var p = obj.ParameterTypes[i];
                    uni.Put(p.Name, obj.Parameters[i], p.ParameterType, options);
                }

                options.Tag = 7;
                uni.Serialize(buffer, options);
            }
            break;

            default:
            {
                var contentBuffer = Unpooled.Buffer(128);
                for (int i = 0; i < obj.ParameterTypes.Length; i++)
                {
                    var p = obj.ParameterTypes[i];
                    options.Tag = p.Position + 1;
                    convertRoot.Serialize(obj.Parameters[i], p.ParameterType, contentBuffer, options);
                }

                options.Tag = 7;
                bufferConvert.Serialize(contentBuffer, buffer, options);
            }
            break;
            }

            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);
        }