Пример #1
0
        public void InvokeWhenGetVShouldOutParameterBe3()
        {
            var(methodInfo, isOneway, outParameters, codec, version, serviceType) = rpcMetadata.FindRpcMethod("Test", "GetV");
            var req = new Request()
            {
                ServantName          = "Test",
                FuncName             = "GetV",
                Parameters           = new object[] { 1 },
                Mehtod               = methodInfo,
                ServiceType          = serviceType,
                ReturnParameterTypes = outParameters,
                IsOneway             = isOneway
            };
            var resp = req.CreateResponse();

            sut.Invoke(req, resp);
            Assert.Equal(3, req.Parameters[0]);
            Assert.Equal(3, ((Task <object>)resp.ReturnValue).Result);
            Assert.Single(resp.ReturnParameters);
            Assert.Equal(3, resp.ReturnParameters[0]);
        }
Пример #2
0
        public Request DecodeRequest(IByteBuffer input)
        {
            var req =
                JsonConvert.DeserializeObject <Request>(input.ReadString(input.ReadableBytes, Encoding.UTF8));

            input.MarkReaderIndex();
            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();
            for (int i = 0; i < req.ParameterTypes.Length; i++)
            {
                if (req.Parameters[i] == null || !req.ParameterTypes[i].ParameterType.IsValueType)
                {
                    continue;
                }
                req.Parameters[i] = Convert.ChangeType(req.Parameters[i], req.ParameterTypes[i].ParameterType);
            }
            return(req);
        }
Пример #3
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;
                }
            }
        }
Пример #4
0
        public IDictionary <MethodInfo, Func <AspectContext, AspectDelegate, Task> > CreateRpcClientInvokers(IRpcMetadata rpcMetadata)
        {
            var dictionary = new Dictionary <MethodInfo, Func <AspectContext, AspectDelegate, Task> >();

            foreach (var item in rpcMetadata.Clients)
            {
                var attribute = item.GetCustomAttribute <RpcAttribute>();
                foreach (var method in item.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                {
                    var(methodInfo, isOneway, outParameters, codec, version, serviceType) = rpcMetadata.FindRpcMethod(attribute.ServantName, method.Name);
                    var parameterTypes = methodInfo.GetParameters();
                    dictionary.Add(method, async(context, next) =>
                    {
                        var req = new Request()
                        {
                            ServantName          = attribute.ServantName,
                            FuncName             = method.Name,
                            Codec                = codec,
                            IsOneway             = isOneway,
                            ParameterTypes       = parameterTypes,
                            ReturnParameterTypes = outParameters,
                            Parameters           = context.Parameters,
                            Version              = version
                        };
                        req.Context.SetContext(context.AdditionalData);
                        var resp = await clientFactory.SendAsync(req);
                        context.AdditionalData.SetContext(resp.Context);
                        if (isOneway)
                        {
                            await context.Complete();
                        }
                        else
                        {
                            context.ReturnValue       = resp.ReturnValue;
                            resp.ReturnValueType      = method.ReturnParameter;
                            resp.ReturnParameterTypes = outParameters;
                            object[] returnParameters = resp.ReturnParameters;
                            if (returnParameters != null && returnParameters.Length > 0)
                            {
                                var index = 0;
                                foreach (var outP in outParameters)
                                {
                                    if (index >= returnParameters.Length)
                                    {
                                        break;
                                    }

                                    req.Parameters[outP.Position] = returnParameters[index++];
                                }
                            }
                        }
                    });
                }
            }
            return(dictionary);
        }
Пример #5
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);
        }