2
3
4
20 this->d_ptr->add_handler(std::string(method), std::move(handler));
26 if (request.is_array()) {
27 return this->process_batch_request(request, data, unique_id);
30 return this->do_process_request(request, data,
false, unique_id);
36 nlohmann::json discarded = nlohmann::json::value_t::discarded;
38 auto request_id = get_request_id(request);
39 bool is_discarded =
false;
41 const auto req = jsonrpc_request::from_json(request);
43 is_discarded = req.id.is_discarded();
45 const dispatcher::context_t ctx = std::make_pair(data, req.extra);
46 const auto res =
this->invoke(req.method, req.params, ctx, unique_id);
47 if (!request_id.is_null()) {
59 catch (
const std::exception& e) {
60 this->request_failed(request_id, &e,
false, unique_id);
61 const auto* eptr =
dynamic_cast<
const exception*>(&e);
63 return is_discarded ? discarded : generate_error_response(ex, request_id);
70 if (request.empty()) {
72 this->request_failed(
nullptr, &e,
true, unique_id);
73 return generate_error_response(e, nlohmann::json(
nullptr));
76 auto response = nlohmann::json::array();
77 for (
const auto& req : request) {
78 if (!req.is_object()) {
79 const exception e(exception::INVALID_REQUEST, err_not_jsonrpc_2_0_request);
80 this->request_failed(
nullptr, &e,
false, unique_id);
82 response.push_back(generate_error_response(e, nlohmann::json(
nullptr)));
84 else if (
const auto res =
85 this->do_process_request(req, data,
true, dispatcher_private::get_and_increment_counter());
88 response.push_back(res);
92 return response.empty() ? nlohmann::json(nlohmann::json::value_t::discarded) : response;
101 const std::string& method,
const nlohmann::json& params,
const dispatcher::context_t& ctx, std::uint64_t
104 if (
const auto handler =
this->d_ptr->find_handler(method); handler !=
nullptr) {
105 return handler(ctx, params);
Private implementation of the JSON RPC dispatcher class.
static std::uint64_t get_and_increment_counter() noexcept
Generates a unique request ID.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
virtual nlohmann::json do_process_request(const nlohmann::json &request, const std::any &data, bool is_batch, std::uint64_t unique_id)
Processes a single, non-batch JSON RPC request.
virtual void request_parsed(const jsonrpc_request &request, const std::any &data, std::uint64_t unique_id)
Invoked after the request has been parsed.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
nlohmann::json process_request(const nlohmann::json &request, const std::any &data={})
Processes a JSON RPC request.
virtual void request_failed(const nlohmann::json &request_id, const std::exception *e, bool is_batch, std::uint64_t unique_id)
Invoked when a request fails.
virtual nlohmann::json invoke(const std::string &method, const nlohmann::json ¶ms, const dispatcher::context_t &ctx, std::uint64_t unique_id)
Invokes a method handler.
virtual nlohmann::json process_batch_request(const nlohmann::json &request, const std::any &data, std::uint64_t unique_id)
Processes a batch request.
dispatcher()
Class constructor.
virtual ~dispatcher()
Class destructor.
JSON RPC Exception class.
Exception thrown when the method is not found.
method_not_found_exception()
static constexpr int INTERNAL_ERROR
Internal JSON-RPC error.
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Represents a JSON RPC request.