Dispatcher for JSON-RPC requests.
More...
#include <dispatcher.hpp>
Dispatcher for JSON-RPC requests.
Dispatcher manages the registration and execution of method call and notification handlers for JSON-RPC requests. It can operate in single-threaded or multi-threaded mode.
Definition at line 24 of file dispatcher.hpp.
◆ Dispatcher() [1/3]
jsonrpc::server::Dispatcher::Dispatcher |
( |
bool | enable_multithreading = true, |
|
|
size_t | num_threads = std::thread::hardware_concurrency() ) |
|
explicit |
Constructs a Dispatcher.
- Parameters
-
enableMultithreading | Enable multi-threading support. |
numThreads | Number of threads to use if multi-threading is enabled. |
Definition at line 7 of file dispatcher.cpp.
8 : enable_multithreading_(enable_multithreading),
9 thread_pool_(enable_multithreading ? num_threads : 0) {
10
11 if (enable_multithreading_) {
12 spdlog::info(
13 "Dispatcher initialized with multithreading support using {} threads",
14 num_threads);
15 } else {
16 spdlog::info("Dispatcher initialized without multithreading support");
17 }
18}
◆ Dispatcher() [2/3]
jsonrpc::server::Dispatcher::Dispatcher |
( |
const Dispatcher & | | ) |
|
|
delete |
◆ Dispatcher() [3/3]
jsonrpc::server::Dispatcher::Dispatcher |
( |
Dispatcher && | | ) |
|
|
delete |
◆ ~Dispatcher()
virtual jsonrpc::server::Dispatcher::~Dispatcher |
( |
| ) |
|
|
virtualdefault |
◆ DispatchRequest()
auto jsonrpc::server::Dispatcher::DispatchRequest |
( |
const std::string & | request | ) |
-> std::optional<std::string> |
Processes a JSON-RPC request.
Dispatches the request to the appropriate handler.
- Parameters
-
request | The JSON-RPC request as a string. |
- Returns
- The response from the handler as a JSON string, or std::nullopt if no response is needed.
Definition at line 20 of file dispatcher.cpp.
21 {
22 auto request_json = ParseAndValidateJson(request_str);
23 if (!request_json.has_value()) {
25 }
26
27 if (request_json->is_array()) {
28 return DispatchBatchRequest(*request_json);
29 }
30
31 return DispatchSingleRequest(*request_json);
32}
static auto CreateLibError(LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
Creates a Response object for a library error.
References jsonrpc::server::Response::CreateLibError(), and jsonrpc::server::kParseError.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ RegisterMethodCall()
void jsonrpc::server::Dispatcher::RegisterMethodCall |
( |
const std::string & | method, |
|
|
const MethodCallHandler & | handler ) |
Registers a method call handler.
- Parameters
-
method | The name of the RPC method. |
handler | The handler function for this method. |
Definition at line 208 of file dispatcher.cpp.
209 {
210 handlers_[method] = handler;
211 spdlog::info("Dispatcher registered method call: {}", method);
212}
◆ RegisterNotification()
void jsonrpc::server::Dispatcher::RegisterNotification |
( |
const std::string & | method, |
|
|
const NotificationHandler & | handler ) |
Registers a notification handler.
- Parameters
-
method | The name of the RPC notification. |
handler | The handler function for this notification. |
Definition at line 214 of file dispatcher.cpp.
215 {
216 handlers_[method] = handler;
217 spdlog::info("Dispatcher registered notification: {}", method);
218}
The documentation for this class was generated from the following files: