private bool checkParams(Message req, Message res, MethodInfo method, object[] args, object[] invokeArgs) { ParameterInfo[] pinfo = method.GetParameters(); int count = 0; foreach (ParameterInfo info in pinfo) { if (typeof(Message).IsAssignableFrom(info.ParameterType)) { continue; } count++; } if (count != args.Length) { reply(res, 400, string.Format("Request(Url={0}, Method={1}, Params={2}) Bad Format", req.Url, method.Name, JsonKit.SerializeObject(args))); return(false); } int j = 0; for (int i = 0; i < pinfo.Length; i++) { if (typeof(Message).IsAssignableFrom(pinfo[i].ParameterType)) { invokeArgs[i] = req; continue; } invokeArgs[i] = JsonKit.Convert(args[j++], pinfo[i].ParameterType); } return(true); }
public async Task <object> InvokeAsync(Type type, Message req, int timeoutMillis = 10000) { Message res = await base.InvokeAsync(req, timeoutMillis); object data = parseResult(res); return(JsonKit.Convert(data, type)); }
public async Task <T> InvokeAsync <T>(Message req, int timeoutMillis = 10000) { Message res = await base.InvokeAsync(req, timeoutMillis); object data = parseResult(res); return((T)JsonKit.Convert(data, typeof(T))); }
public dynamic Request(Type realReturnType, Message request) { dynamic resp = rpcClient.InvokeAsync(realReturnType, request).Result; if (realReturnType == typeof(void)) { return(null); } if (realReturnType != resp.GetType() && !typeof(Task).IsAssignableFrom(realReturnType)) { return(JsonKit.Convert(resp, realReturnType)); } return(resp); }
public async Task ProcessAsync(Message request, Message response) { response.Status = 200; if (request.Url == null) { reply(response, 400, "Missing url in request"); return; } string url = request.Url; int length = 0; MethodInstance target = null; string targetPath = null; foreach (var e in this.UrlPath2Method) { string path = e.Key; if (url.StartsWith(path)) { if (path.Length > length) { target = e.Value; targetPath = path; length = path.Length; } } } if (target == null) { reply(response, 404, string.Format("Url={0} Not Found", url)); return; } object[] args = new object[0]; if (request.Body != null) { args = JsonKit.Convert <object[]>(request.Body); } else { args = parseParam(url.Substring(targetPath.Length)); } ParameterInfo[] pinfo = target.Method.GetParameters(); object[] invokeArgs = new object[pinfo.Length]; bool ok = checkParams(request, response, target.Method, args, invokeArgs); if (!ok) { return; } dynamic invoked = target.Method.Invoke(target.Instance, invokeArgs); if (invoked != null && typeof(Task).IsAssignableFrom(invoked.GetType())) { if (target.Method.ReturnType.GenericTypeArguments.Length > 0) { invoked = await invoked; } } if (invoked is Message) { response.Replace((Message)invoked); } else { response.Body = invoked; response.Headers["content-type"] = "application/json; charset=utf8;"; } }