JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::endpoint::Dispatcher Class Reference

#include <dispatcher.hpp>

Collaboration diagram for jsonrpc::endpoint::Dispatcher:

Public Types

using MethodCallHandler
 
using NotificationHandler
 

Public Member Functions

 Dispatcher (asio::any_io_executor executor, std::shared_ptr< spdlog::logger > logger=nullptr)
 
 Dispatcher (const Dispatcher &)=delete
 
 Dispatcher (Dispatcher &&)=delete
 
auto operator= (const Dispatcher &) -> Dispatcher &=delete
 
auto operator= (Dispatcher &&) -> Dispatcher &=delete
 
virtual ~Dispatcher ()=default
 
auto Logger () -> std::shared_ptr< spdlog::logger >
 
void RegisterMethodCall (const std::string &method, const MethodCallHandler &handler)
 
void RegisterNotification (const std::string &method, const NotificationHandler &handler)
 
auto DispatchRequest (std::string request) -> asio::awaitable< std::optional< std::string > >
 

Detailed Description

Definition at line 18 of file dispatcher.hpp.

Member Typedef Documentation

◆ MethodCallHandler

Initial value:
std::function<asio::awaitable<nlohmann::json>(
const std::optional<nlohmann::json>&)>

Definition at line 20 of file dispatcher.hpp.

◆ NotificationHandler

Initial value:
std::function<asio::awaitable<void>(
const std::optional<nlohmann::json>&)>

Definition at line 22 of file dispatcher.hpp.

Constructor & Destructor Documentation

◆ Dispatcher() [1/3]

jsonrpc::endpoint::Dispatcher::Dispatcher ( asio::any_io_executor executor,
std::shared_ptr< spdlog::logger > logger = nullptr )
explicit

Definition at line 12 of file dispatcher.cpp.

14 : executor_(std::move(executor)),
15 logger_(logger ? logger : spdlog::default_logger()) {
16}

◆ Dispatcher() [2/3]

jsonrpc::endpoint::Dispatcher::Dispatcher ( const Dispatcher & )
delete

◆ Dispatcher() [3/3]

jsonrpc::endpoint::Dispatcher::Dispatcher ( Dispatcher && )
delete

◆ ~Dispatcher()

virtual jsonrpc::endpoint::Dispatcher::~Dispatcher ( )
virtualdefault

Member Function Documentation

◆ DispatchRequest()

auto jsonrpc::endpoint::Dispatcher::DispatchRequest ( std::string request) -> asio::awaitable<std::optional<std::string>>

Definition at line 28 of file dispatcher.cpp.

29 {
30 auto root = nlohmann::json::parse(request, nullptr, false);
31 if (root.is_discarded()) {
32 co_return Response::CreateError(RpcErrorCode::kParseError).ToJson().dump();
33 }
34
35 // Single request
36 if (root.is_object()) {
37 auto request = Request::FromJson(root);
38 if (!request.has_value()) {
39 co_return Response::CreateError(request.error()).ToJson().dump();
40 }
41
42 auto response = co_await DispatchSingleRequest(request.value());
43 if (response.has_value()) {
44 co_return response.value().ToJson().dump();
45 }
46 co_return std::nullopt;
47 }
48
49 // Batch request
50 if (root.is_array()) {
51 if (root.empty()) {
52 co_return Response::CreateError(RpcErrorCode::kInvalidRequest)
53 .ToJson()
54 .dump();
55 }
56
57 std::vector<Request> requests;
58 std::vector<Response> responses;
59 for (const auto& element : root) {
60 auto request = Request::FromJson(element);
61 if (!request.has_value()) {
62 responses.push_back(Response::CreateError(request.error()));
63 continue;
64 }
65 requests.push_back(request.value());
66 }
67
68 auto dispatched = co_await DispatchBatchRequest(requests);
69 for (const auto& response : dispatched) {
70 responses.push_back(response);
71 }
72
73 co_return nlohmann::json(responses).dump();
74 }
75
76 co_return Response::CreateError(RpcErrorCode::kInvalidRequest)
77 .ToJson()
78 .dump();
79}
static auto FromJson(const nlohmann::json &json_obj) -> std::expected< Request, error::RpcError >
Definition request.cpp:31
static auto CreateError(RpcErrorCode code, const std::optional< RequestId > &id=std::nullopt) -> Response
Definition response.cpp:28

References jsonrpc::endpoint::Response::CreateError(), and jsonrpc::endpoint::Request::FromJson().

Here is the call graph for this function:

◆ Logger()

auto jsonrpc::endpoint::Dispatcher::Logger ( ) -> std::shared_ptr<spdlog::logger>
inline

Definition at line 35 of file dispatcher.hpp.

35 {
36 return logger_;
37 }

◆ operator=() [1/2]

auto jsonrpc::endpoint::Dispatcher::operator= ( const Dispatcher & ) -> Dispatcher &=delete
delete

◆ operator=() [2/2]

auto jsonrpc::endpoint::Dispatcher::operator= ( Dispatcher && ) -> Dispatcher &=delete
delete

◆ RegisterMethodCall()

void jsonrpc::endpoint::Dispatcher::RegisterMethodCall ( const std::string & method,
const MethodCallHandler & handler )

Definition at line 18 of file dispatcher.cpp.

19 {
20 method_handlers_[method] = handler;
21}

Referenced by jsonrpc::endpoint::RpcEndpoint::RegisterMethodCall().

Here is the caller graph for this function:

◆ RegisterNotification()

void jsonrpc::endpoint::Dispatcher::RegisterNotification ( const std::string & method,
const NotificationHandler & handler )

Definition at line 23 of file dispatcher.cpp.

24 {
25 notification_handlers_[method] = handler;
26}

Referenced by jsonrpc::endpoint::RpcEndpoint::RegisterNotification().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: