Exemple #1
0
        public static ResponseMessage CreateError(string msg, string code)
        {
            var response = new ResponseMessage()
            {
                Success = false, Msg = msg
            };

            response.SetData(typeof(string), code);
            return(response);
        }
Exemple #2
0
        public override object InvokeResult(object rq, Func <Type, object> objectCtor = null)
        {
            var request  = rq as RequestMessage;
            var response = new ResponseMessage();

            try
            {
                var a = serviceHandle.TryGetValue(request.Service, out serviceInfo serviceInfo);
                if (!a)
                {
                    return(ResponseMessage.CreateError("未找到该服务", "404"));
                }

                var methodInfo = serviceInfo.GetMethod(request.Method);
                if (methodInfo == null)
                {
                    return(ResponseMessage.CreateError("未找到该方法" + request.Method, "404"));
                }
                var method = methodInfo.MethodInfo;

                var paramters = request.Args;

                var methodParamters = methodInfo.Parameters;

                var args = new object[methodParamters.Length];

                for (int i = 0; i < methodParamters.Length; i++)
                {
                    var p      = methodParamters[i];
                    var value  = paramters[i];
                    int offSet = 0;
                    args[i] = Core.BinaryFormat.FieldFormat.UnPack(p.ParameterType, value, ref offSet);
                }

                var msgBase = new Core.Remoting.MessageBase()
                {
                    Args = args.ToList(), Method = request.Method, Service = request.Service, Token = request.Token
                };
                var errorInfo = InvokeMessage(msgBase, out object result, out Dictionary <int, object> outs2, out string token, objectCtor);
                if (errorInfo != null)
                {
                    return(ResponseMessage.CreateError(errorInfo.msg, errorInfo.code));
                }
                var generType = method.ReturnType;
                if (methodInfo.IsAsync)
                {
                    generType = method.ReturnType.GenericTypeArguments[0];
                }

                response.SetData(generType, result);
                response.Success = true;

                var outs = new Dictionary <int, byte[]>();
                foreach (var kv in outs2)
                {
                    var type  = methodParamters[kv.Key];
                    var value = kv.Value;
                    outs[kv.Key] = Core.BinaryFormat.FieldFormat.Pack(type.ParameterType, value);
                }

                response.Outs = outs;
                if (!string.IsNullOrEmpty(token))//登录方法后返回新TOKEN
                {
                    response.Token = token;
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Msg     = ex.Message;
                Console.WriteLine(ex.ToString());
                return(ResponseMessage.CreateError(ex.Message + $" 在{request.Service}/{request.Method}", "500"));
            }

            return(response);
        }
Exemple #3
0
        internal ResponseMessage InvokeResult(RequestMessage request)
        {
            var response = new ResponseMessage();

            try
            {
                var a = serviceHandle.TryGetValue(request.Service, out object service);
                if (!a)
                {
                    throw new Exception("未找到该服务");
                }
                if (tokenCheck != null)
                {
                    var tokenArry = request.Token.Split('@');
                    if (tokenArry.Length < 2)
                    {
                        throw new Exception("token不合法");
                    }
                    if (!tokenCheck(tokenArry[0], tokenArry[1]))
                    {
                        throw new Exception("token验证失败");
                    }
                }
                var methodKey = string.Format("{0}.{1}", request.Service, request.Method);
                a = methods.TryGetValue(methodKey, out MethodInfo method);
                if (!a)
                {
                    var serviceType = service.GetType();
                    method = serviceType.GetMethod(request.Method);
                    if (method == null)
                    {
                        throw new Exception("未找到该方法");
                    }
                    methods.TryAdd(methodKey, method);
                }
                var paramters       = request.Args;
                var methodParamters = method.GetParameters();
                var outs            = new Dictionary <string, object>();
                int i = 0;
                foreach (var p in methodParamters)
                {
                    var find = paramters.TryGetValue(p.Name, out object value);
                    if (find && value != null)
                    {
                        if (value.GetType() != p.ParameterType)
                        {
                            var value2 = value.ToJson().ToObject(p.ParameterType);
                            paramters[p.Name] = value2;
                        }
                    }
                    else
                    {
                        paramters[p.Name] = null;
                    }
                    if (p.Attributes == ParameterAttributes.Out)
                    {
                        outs.Add(p.Name, i);
                    }
                    i += 1;
                }
                var args3  = paramters?.Select(b => b.Value)?.ToArray();
                var result = method.Invoke(service, args3);
                foreach (var kv in new Dictionary <string, object>(outs))
                {
                    var value = args3[(int)kv.Value];
                    outs[kv.Key] = value;
                }
                response.SetData(result);
                response.Success = true;
                response.Outs    = outs;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Msg     = ex.Message;
                Console.WriteLine(ex.ToString());
            }

            return(response);
        }