Beispiel #1
0
        public override object GetTargetParameterValue(ParameterInfo parameter, ExecuteContext executeContext)
        {
            ProtobufExecuteContext context = executeContext as ProtobufExecuteContext;

            if (parameter.ParameterType.IsAssignableFrom(context.SubMessage.GetType()))
            {
                return(context.SubMessage);
            }

            if (parameter.HasDefaultValue)
            {
                return(parameter.DefaultValue);
            }
            else if (parameter.ParameterType.IsValueType)
            {
                return(parameter.RawDefaultValue);
            }
            return(null);
        }
        /// <summary>
        /// 请求的数据处理
        /// </summary>
        /// <param name="userToken">用户连接对象</param>
        /// <param name="data">请求数据及响应数据</param>
        /// <param name="actionExecuter"></param>
        /// <returns>是否有数据响应 true代表有相应数据并由data传出 false代表没有相应</returns>
        public bool DataHandle(AsyncUserToken userToken, ref byte[] data, IActionExecuter actionExecuter)
        {
            Request request = Request.Parser.ParseFrom(data);

            ProtobufExecuteContext executeContext = new ProtobufExecuteContext();

            executeContext.ControllerName = request.Controller;
            executeContext.ActionName     = request.Action;
            executeContext.UserToken      = userToken;

            #region 子参数解析
            if (request.SubRequest != null)
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                var      type     = assembly.GetType(Any.GetTypeName(request.SubRequest.TypeUrl));
                if (type != null /*&& request.SubRequest.Is((type as IMessage).Descriptor)*/)
                {
                    var      uppackMethod = request.SubRequest.GetType().GetMethod(nameof(request.SubRequest.Unpack)).MakeGenericMethod(type);
                    IMessage subMessagae  = uppackMethod.Invoke(request.SubRequest, null) as IMessage;
                    executeContext.SubMessage = subMessagae;
                }
            }
            #endregion

            var has = actionExecuter.TryExecuteAction(executeContext, out object obj);
            if (has)
            {
                if (obj == null)
                {
                    return(false);
                }
            }
            else
            {
                //没有找到action
                obj = "无效的请求";
            }

            var respone = MakeRespone(request, obj);
            data = respone.ToByteArray();
            return(true);
        }