private static void MapResponseResult(IInvocation invocation, List<ParameterInfo> @params, RpcResponse response) { if (response.Exception != null) { throw response.Exception; } if (invocation.Method.ReturnType != typeof(void) && response.ReturnValue != null) { invocation.ReturnValue = response.ReturnValue.ConvertToCorrectTypeValue(invocation.Method.ReturnType); } var outParams = @params.Where(x => x.IsOut).Select(x => x.Name); var missingOutValue = outParams.FirstOrDefault(param => !(response.ChangedParams ?? new Dictionary<string, object>()).ContainsKey(param)); if (missingOutValue != null) { throw new Exception(string.Format("RpcResponse does not contain the modified value for param {0} which is an 'out' param. Probably there's something wrong with the bloody Coordinator", missingOutValue)); } for (var i = 0; i < @params.Count; i++) { if (@params[i].IsOut) { invocation.SetArgumentValue(i, response.ChangedParams[@params[i].Name].ConvertToCorrectTypeValue(@params[i].ParameterType.GetElementType())); } else if (@params[i].ParameterType.IsByRef && response.ChangedParams.ContainsKey(@params[i].Name)) { invocation.SetArgumentValue(i, response.ChangedParams[@params[i].Name].ConvertToCorrectTypeValue(@params[i].ParameterType.GetElementType())); } } }
public void Should_return_and_log_warn_msg_if_the_waitHandlers_does_not_contain_requestId() { // Arrange InternalDependencies.RpcQueueHelper = Substitute.For<IRpcQueueHelper>(); var client = new BurrowRpcClientCoordinator<ISomeService>(null, Substitute.For<IRpcRouteFinder>()); var res = new RpcResponse { RequestId = Guid.NewGuid() }; // Action client.ReceiveResponse(res); // Assert Global.DefaultWatcher.Received(1).WarnFormat(Arg.Any<string>(), Arg.Any<object[]>()); }
public void HandleMesage(RpcRequest msg) { if (msg.UtcExpiryTime != null && msg.UtcExpiryTime < DateTime.UtcNow) { Global.DefaultWatcher.WarnFormat("Msg {0}.{1} from {2} has been expired", msg.DeclaringType, msg.MethodName, msg.ResponseAddress); return; } var response = new RpcResponse { RequestId = msg.Id, }; try { var methodInfo = InternalDependencies.MethodMatcher.Match <T>(msg); if (methodInfo == null) { throw new Exception(string.Format("Could not find a match member of type {0} for method {1} of {2}", msg.MemberType.ToString(), msg.MethodName, msg.DeclaringType)); } object[] parameters = msg.Params.Values.ToArray(); response.ReturnValue = methodInfo.Invoke(_realInstance, parameters); var keys = msg.Params.Keys.ToArray(); for (int i = 0; i < msg.Params.Count; i++) { msg.Params[keys[i]] = parameters[i]; } response.ChangedParams = msg.Params; } catch (Exception ex) { response.Exception = ex; } if (!string.IsNullOrEmpty(msg.ResponseAddress)) { _tunnel.Publish(response, msg.ResponseAddress); } }
public void Should_set_respones_value_to_wait_handler_and_set_the_wait_handler() { // Arrange InternalDependencies.RpcQueueHelper = Substitute.For<IRpcQueueHelper>(); var client = new BurrowRpcClientCoordinator<ISomeService>(null, Substitute.For<IRpcRouteFinder>()); var res = new RpcResponse { RequestId = Guid.NewGuid() }; var handlers = client.GetCachedWaitHandlers(); var wait = new RpcWaitHandler(); handlers.TryAdd(res.RequestId, wait); // Action Task.Factory.StartNew(() => client.ReceiveResponse(res)); // Assert wait.WaitHandle.WaitOne(); Assert.AreEqual(res, wait.Response); }
public static RpcResponse DeserializeResponse(RpcRequest request, string json) { RpcResponse response = JsonConvert.DeserializeObject <RpcResponse>(json); string typename = request.DeclaringType + "," + request.DeclaringAssembly; // = "test.ICalculator,test"; Type dataType = Type.GetType(typename); // request.DeclaringType); MethodInfo mi = dataType.GetMethod(request.MethodName); Type returnType = mi.ReturnType; response.ReturnValue = Sanitize(returnType, response.ReturnValue); /*if (response.ReturnValue.GetType() == typeof (Newtonsoft.Json.Linq.JArray)) { * Newtonsoft.Json.Linq.JArray jarray = (Newtonsoft.Json.Linq.JArray) response.ReturnValue; * string dataJson = jarray.ToString (); * * var data = JsonConvert.DeserializeObject (dataJson, returnType); * response.ReturnValue = data; * int j = 34; * }*/ return(response); }
private static void MapResponseResult(IInvocation invocation, List <ParameterInfo> @params, RpcResponse response) { if (response.Exception != null) { throw response.Exception; } if (invocation.Method.ReturnType != typeof(void) && response.ReturnValue != null) { invocation.ReturnValue = response.ReturnValue.ConvertToCorrectTypeValue(invocation.Method.ReturnType); } var outParams = @params.Where(x => x.IsOut).Select(x => x.Name); var missingOutValue = outParams.FirstOrDefault(param => !(response.ChangedParams ?? new Dictionary <string, object>()).ContainsKey(param)); if (missingOutValue != null) { throw new Exception(string.Format("RpcResponse does not contain the modified value for param {0} which is an 'out' param. Probably there's something wrong with the bloody Coordinator", missingOutValue)); } for (var i = 0; i < @params.Count; i++) { if (@params[i].IsOut) { invocation.SetArgumentValue(i, response.ChangedParams[@params[i].Name].ConvertToCorrectTypeValue(@params[i].ParameterType.GetElementType())); } else if (@params[i].ParameterType.IsByRef && response.ChangedParams.ContainsKey(@params[i].Name)) { invocation.SetArgumentValue(i, response.ChangedParams[@params[i].Name].ConvertToCorrectTypeValue(@params[i].ParameterType.GetElementType())); } } }
public void Intercept(IInvocation invocation) { var method = invocation.Method; var attributes = method.GetCustomAttributes(true).Select(x => x as Attribute).ToArray(); var isAsync = _methodFilters.Any(filter => filter.IsAsync(method, attributes)); _methodFilters.ForEach(filter => filter.CheckValid(method, attributes, isAsync)); var @params = method.GetParameters().ToList(); var args = new Dictionary <string, object>(); for (int i = 0; i < invocation.Arguments.Length; i++) { args.Add(@params[i].Name, invocation.Arguments[i]); } var request = new RpcRequest { Params = args, MemberType = method.MemberType, MethodName = method.Name, MethodSignature = InternalDependencies.MethodMatcher.GetMethodSignature(method), DeclaringType = method.DeclaringType.FullName, DeclaringAssembly = method.DeclaringType.Assembly.FullName.Substring(0, method.DeclaringType.Assembly.FullName.IndexOf(",")) }; var timeToLiveAttribute = attributes.LastOrDefault(x => x is RpcTimeToLiveAttribute); if (timeToLiveAttribute != null) { var att = (RpcTimeToLiveAttribute)timeToLiveAttribute; if (att.Seconds > 0) { request.UtcExpiryTime = DateTime.UtcNow.AddSeconds(att.Seconds); } } string jsonRequest = JsonConvert.SerializeObject(request); //Console.WriteLine("Client Sending Request: {0}", jsonRequest); if (log != null) { log(Direction.Sent, jsonRequest); } client.SendFrame(jsonRequest); string jsonResponse = client.ReceiveFrameString(); if (log != null) { log(Direction.Received, jsonResponse); } //Console.WriteLine("Client Response Received: {0}", jsonResponse); RpcResponse response = JSON.DeserializeResponse(request, jsonResponse); // JsonConvert.DeserializeObject<RpcResponse>(jsonResponse); MapResponseResult(invocation, @params, response); }