internal static object QueryInternal(this FastRpcClient fastRpcClient, string title, ICustomTuple customTuple, Type returnType, IRpcSerializer rpcSerializer, string extention = null, bool throwIfErrorResponseCode = false) { if (rpcSerializer == null) { throw new ArgumentNullException(nameof(rpcSerializer)); } if (customTuple == null) { throw new ArgumentNullException(nameof(customTuple)); } var encoding = RpcExtentionSettings.DefaultEncoding; var response = fastRpcClient.Query( encoding.GetBytes(title), encoding.GetBytes(rpcSerializer.Serialize(customTuple)), extention == null ? FrameFormat.EmptyBytes : encoding.GetBytes(extention), throwIfErrorResponseCode); if (returnType == typeof(void)) { return(null); } var responseContent = response.ReadContentString(); if (string.IsNullOrEmpty(responseContent)) { return(null); } return(rpcSerializer.Deserialize(responseContent, returnType)); }
internal static async Task <TResponse> SendReceiveAsync <TRequest, TResponse>(LightweightMethodStub methodStub, TRequest request, IRpcSerializer serializer) where TRequest : class where TResponse : class { TResponse response; var context = new LightweightCallContext(new TestRpcEndPoint(), null, ImmutableArray <KeyValuePair <string, ImmutableArray <byte> > > .Empty, CancellationToken.None); var requestPipe = new Pipe(); var responsePipe = new Pipe(); var duplexPipe = new DirectDuplexPipe(requestPipe.Reader, responsePipe.Writer); await using (var pipeline = new TestPipeline(duplexPipe)) { var payload = new ReadOnlySequence <byte>(serializer.Serialize(request)); var frame = new LightweightRpcFrame(RpcFrameType.UnaryRequest, null, 1, methodStub.OperationName, RpcOperationFlags.None, 0, payload, null); await methodStub.HandleMessage(pipeline, frame, null, context); var readResult = await responsePipe.Reader.ReadAsync(); var buffer = readResult.Buffer; bool hasResponseFrame = LightweightRpcFrame.TryRead(ref buffer, 65536, out var responseFrame) == RpcFrameState.Full; Assert.IsTrue(hasResponseFrame); response = (TResponse)serializer.Deserialize(responseFrame.Payload, typeof(TResponse)); return(response); } }
internal RpcFaultException CreateFaultException(RpcError error, IRpcSerializer serializer) { RpcFaultException?faultException = null; bool matched = false; foreach (var factory in this.faultExceptionFactories) { if (factory.FaultCode == error.ErrorCode) { matched = true; if (factory.FaultDetailsType != null) { if (error.ErrorDetails != null) { object?details = serializer.Deserialize(error.ErrorDetails, factory.FaultDetailsType); faultException = factory.CreateFaultException(error.Message ?? "", details); } } else { faultException = factory.CreateFaultException(error.Message ?? "", null); } break; } } if (!matched && baseHandler != null) { faultException = baseHandler.CreateFaultException(error, serializer); } return(faultException ?? new RpcFaultException(error.ErrorCode ?? "", error.Message ?? "")); }
public static T Deserialize <T>(this IRpcSerializer <T> serializer, byte[]?input) { if (serializer is null) { throw new ArgumentNullException(nameof(serializer)); } return(input != null?serializer.Deserialize(new ReadOnlySequence <byte>(input)) : default);
internal static TResponse GetResponseFromData <TResponse>(IRpcSerializer serializer, byte[] responseData) where TResponse : class { TResponse response = null; if (LightweightRpcFrame.TryRead(responseData, 65536, out var responseFrame) == RpcFrameState.Full) { response = (TResponse)serializer.Deserialize(responseFrame.Payload, typeof(TResponse)); } return(response); }
public string Proceed(string content) { var handlerTarget = ObjectFactory.CreateInstance(HandlerTargetType); var parameterTuple = (ICustomTuple)RpcSerializer.Deserialize(content, HandlerParameterTupleType); var parameterArray = parameterTuple.ToObjectArray(); var result = _fastInvokeHandler.Invoke(handlerTarget, parameterArray); if (result == null) { return(string.Empty); } return(RpcSerializer.Serialize(result)); }