Пример #1
0
        /// <summary>
        /// Handle request request and get the response
        /// </summary>
        /// <param name="service">The service which will handle the request.</param>
        /// <param name="request">The request to handle.</param>
        /// <param name="cancellationToken">The cancellation token which can cancel this method.</param>
        /// <returns>The response for the request.</returns>
        private async Task <JsonRpcResponse> GetResponseAsync(IJsonRpcCallService service, JsonRpcRequest request, CancellationToken cancellationToken)
        {
            JsonRpcResponse response = null;

            try
            {
                var rpcCall = service[request.Method];
                if (rpcCall == null)
                {
                    throw new MethodNotFoundException($"Method: {request.Method} not found.");
                }

                object[] arguments;
                if (request.Params.Type == RequestParameterType.RawString)
                {
                    var paramString = (string)request.Params.Value;
                    arguments = await JsonRpcCodec.DecodeArgumentsAsync(paramString, rpcCall.Parameters, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    if (request.Params.Value is Array array)
                    {
                        arguments = array.Cast <object>().ToArray();
                    }
                    else
                    {
                        arguments = new[] { request.Params.Value };
                    }
                }

                //From here we got the response id.
                response = new JsonRpcResponse(request.Id);
                if (arguments.Length == rpcCall.Parameters.Count)
                {
                    try
                    {
                        var result = await rpcCall.Call(arguments).ConfigureAwait(false);

                        if (request.IsNotification)
                        {
                            return(null);
                        }
                        response.WriteResult(result);
                    }
                    catch (Exception ex)
                    {
                        var argumentString = new StringBuilder();
                        argumentString.Append(Environment.NewLine);
                        var index = 0;
                        foreach (var argument in arguments)
                        {
                            argumentString.AppendLine($"[{index}] {argument}");
                        }
                        argumentString.Append(Environment.NewLine);
                        response.WriteResult(new InternalErrorException($"Call method {rpcCall.Name} with args:{argumentString} error :{ex.Format()}"));
                    }
                }
                else
                {
                    throw new InvalidParamsException("Argument count is not matched");
                }
            }
            catch (Exception ex)
            {
                response ??= new JsonRpcResponse();
                if (ex is RpcException rpcException)
                {
                    response.WriteResult(rpcException);
                }
                else
                {
                    var serverError = new InternalErrorException($"Handle request {request} error: {ex.Format()}");
                    response.WriteResult(serverError);
                }
            }
            return(response);
        }
Пример #2
0
        /// <summary>
        /// Handle request request and get the response
        /// </summary>
        /// <param name="service">The service which will handle the request.</param>
        /// <param name="request">The request to handle.</param>
        /// <returns>The response for the request.</returns>
        protected async Task <JsonRpcResponse> GetResponseAsync(JsonRpcService service, JsonRpcRequest request)
        {
            JsonRpcResponse response = null;

            try
            {
                var rpcCall = service.GetRpcCall(request.Method);
                if (rpcCall == null)
                {
                    throw new MethodNotFoundException($"Method: {request.Method} not found.");
                }
                var arguments = await JsonRpcCodec.DecodeArgumentsAsync(request.Params, rpcCall.Parameters).ConfigureAwait(false);

                //From here we got the response id.
                response = new JsonRpcResponse(request.Id);
                //The parser will add context into the args, so the final count is parameter count + 1.
                if (arguments.Length == rpcCall.Parameters.Count)
                {
                    try
                    {
                        var result = await rpcCall.Call(arguments).ConfigureAwait(false);

                        if (request.IsNotification)
                        {
                            return(null);
                        }
                        response.WriteResult(result);
                    }
                    catch (Exception ex)
                    {
                        var argumentString = new StringBuilder();
                        argumentString.Append(Environment.NewLine);
                        var index = 0;
                        foreach (var argument in arguments)
                        {
                            argumentString.AppendLine($"[{index}] {argument}");
                        }

                        argumentString.Append(Environment.NewLine);
                        Logger.WriteError($"Call method {rpcCall.Name} with args:{argumentString} error :{ex.Format()}");
                        response.WriteResult(new InternalErrorException());
                    }
                }
                else
                {
                    throw new InvalidParamsException("Argument count is not matched");
                }
            }
            catch (Exception ex)
            {
                response ??= new JsonRpcResponse();
                if (ex is RpcException rpcException)
                {
                    Logger.WriteError($"Handle request {request} error: {rpcException.Format()}");
                    response.WriteResult(rpcException);
                }
                else
                {
                    Logger.WriteError($"Handle request {request} error: {ex.Format()}");
                    var serverError = new InternalErrorException();
                    response.WriteResult(serverError);
                }
            }
            return(response);
        }