JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
dispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5#include <unordered_map>
6#include <vector>
7
8#include <BS_thread_pool.hpp>
9#include <nlohmann/json.hpp>
10
14
15namespace jsonrpc::server {
16
25 public:
32 explicit Dispatcher(
33 bool enable_multithreading = true,
34 size_t num_threads = std::thread::hardware_concurrency());
35
36 Dispatcher(const Dispatcher &) = delete;
37 Dispatcher(Dispatcher &&) = delete;
38 auto operator=(const Dispatcher &) -> Dispatcher & = delete;
39 auto operator=(Dispatcher &&) -> Dispatcher & = delete;
40
42 virtual ~Dispatcher() = default;
43
53 auto DispatchRequest(const std::string &request)
54 -> std::optional<std::string>;
55
63 const std::string &method, const MethodCallHandler &handler);
64
72 const std::string &method, const NotificationHandler &handler);
73
74 private:
84 static auto ParseAndValidateJson(const std::string &request_str)
85 -> std::optional<nlohmann::json>;
86
98 auto DispatchSingleRequest(const nlohmann::json &request_json)
99 -> std::optional<std::string>;
100
112 auto DispatchSingleRequestInner(const nlohmann::json &request_json)
113 -> std::optional<nlohmann::json>;
114
126 auto DispatchBatchRequest(const nlohmann::json &request_json)
127 -> std::optional<std::string>;
128
139 auto DispatchBatchRequestInner(const nlohmann::json &request_json)
140 -> std::vector<nlohmann::json>;
141
152 static auto ValidateRequest(const nlohmann::json &request_json)
153 -> std::optional<Response>;
154
165 static auto FindHandler(
166 const std::unordered_map<std::string, Handler> &handlers,
167 const std::string &method) -> std::optional<Handler>;
168
181 static auto HandleRequest(const Request &request, const Handler &handler)
182 -> std::optional<nlohmann::json>;
183
193 static auto HandleMethodCall(
194 const Request &request, const MethodCallHandler &handler) -> Response;
195
205 static void HandleNotification(
206 const Request &request, const NotificationHandler &handler);
207
209 std::unordered_map<std::string, Handler> handlers_;
210
212 bool enable_multithreading_;
213
215 BS::thread_pool thread_pool_;
216};
217
218} // namespace jsonrpc::server
Dispatcher for JSON-RPC requests.
virtual ~Dispatcher()=default
Destructor.
Dispatcher(const Dispatcher &)=delete
Dispatcher(bool enable_multithreading=true, size_t num_threads=std::thread::hardware_concurrency())
Constructs a Dispatcher.
Definition dispatcher.cpp:7
void RegisterMethodCall(const std::string &method, const MethodCallHandler &handler)
Registers a method call handler.
auto DispatchRequest(const std::string &request) -> std::optional< std::string >
Processes a JSON-RPC request.
auto operator=(Dispatcher &&) -> Dispatcher &=delete
auto operator=(const Dispatcher &) -> Dispatcher &=delete
Dispatcher(Dispatcher &&)=delete
void RegisterNotification(const std::string &method, const NotificationHandler &handler)
Registers a notification handler.
Represents a JSON-RPC request.
Definition request.hpp:13
Represents a JSON-RPC response.
Definition response.hpp:24
std::function< void(const std::optional< nlohmann::json > &)> NotificationHandler
Type alias for notification handler functions.
Definition types.hpp:26
std::variant< MethodCallHandler, NotificationHandler > Handler
Type alias for a handler which can be either a method call handler or a notification handler.
Definition types.hpp:33
std::function< nlohmann::json(const std::optional< nlohmann::json > &)> MethodCallHandler
Type alias for method call handler functions.
Definition types.hpp:17