public async Task <RemoteInvokeResponse> Invoke(RemoteInvokeRequest request) { try { var transport = RemoteInvokeTransport <RemoteInvokeRequest> .CreateRequest(request); var callbackTask = RegisterCallbackAsync(transport.Id); try { var package = new Package { Type = PackageType.Transfer, Data = serializer.Serialize(transport) }; await networkClient.SendAsync(package); } catch (Exception ex) { throw new EasyRpcException("执行远程方法失败", ex); } return(await callbackTask); } catch (Exception ex) { throw new EasyRpcException("执行远程方法失败", ex); } }
private async Task SendAsync(IChannel <Package> channel, RemoteInvokeResponse res, Guid id) { var response = RemoteInvokeTransport <RemoteInvokeResponse> .CreateResponse(id, res); var resData = serializer.Serialize(response); var package = new Package { Type = PackageType.Transfer, Data = resData }; await channel.SendAsync(package); }
private async Task TransportReceived(RemoteInvokeTransport <RemoteInvokeResponse> transport) { if (!resultDictionary.TryGetValue(transport.Id, out var task)) { return; } var res = transport.Content; if (!res.Success) { task.TrySetException(new EasyRpcException(res.Message)); } else { task.SetResult(transport); } await Task.CompletedTask; }
private async Task OnRemoteInvokeRequest(IChannel <Package> channel, RemoteInvokeTransport <RemoteInvokeRequest> request) { logger.LogDebug($"服务端接受到一个数据包:{request.Content}"); var id = request.Id; var invokeRequest = request.Content; var type = RpcServiceLocator.ServiceAssembly.GetType(invokeRequest.FullTypeName); if (type == null) { await SendAsync(channel, RemoteInvokeResponse.CreateFail("没有发现对应服务"), id); return; } var instance = RpcServiceLocator.Instance.GetService(type); if (instance == null) { await SendAsync(channel, RemoteInvokeResponse.CreateFail("没有找到实现类"), id); return; } var method = type.GetMethods().SingleOrDefault(x => { if (x.Name != invokeRequest.MethodName) { return(false); } var identity = x.GetMethodIdentity(); return(identity == invokeRequest.ParameterIdentity); }); if (method == null) { await SendAsync(channel, RemoteInvokeResponse.CreateFail("没有找到具体方法"), id); return; } var parameters = new List <object>(); var methodParameters = method.GetParameters(); for (int i = 0; i < methodParameters.Length; i++) { var data = serializer.Serialize(invokeRequest.Parameters[i]); var param = serializer.Deserialize(data, methodParameters[i].ParameterType); parameters.Add(param); } if (method.IsGenericMethod) { await SendAsync(channel, RemoteInvokeResponse.CreateFail("不支持泛型方法"), id); return; } var result = await InvokeMethod(method, instance, parameters.ToArray()); await SendAsync(channel, result, id); }