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 <expected>
4#include <functional>
5#include <optional>
6#include <string>
7#include <unordered_map>
8
9#include <asio.hpp>
10#include <nlohmann/json.hpp>
11#include <spdlog/spdlog.h>
12
15
17
19 public:
20 using MethodCallHandler = std::function<asio::awaitable<nlohmann::json>(
21 const std::optional<nlohmann::json>&)>;
22 using NotificationHandler = std::function<asio::awaitable<void>(
23 const std::optional<nlohmann::json>&)>;
24
25 explicit Dispatcher(
26 asio::any_io_executor executor,
27 std::shared_ptr<spdlog::logger> logger = nullptr);
28
29 Dispatcher(const Dispatcher&) = delete;
31 auto operator=(const Dispatcher&) -> Dispatcher& = delete;
32 auto operator=(Dispatcher&&) -> Dispatcher& = delete;
33 virtual ~Dispatcher() = default;
34
35 auto Logger() -> std::shared_ptr<spdlog::logger> {
36 return logger_;
37 }
38
40 const std::string& method, const MethodCallHandler& handler);
41
43 const std::string& method, const NotificationHandler& handler);
44
45 auto DispatchRequest(std::string request)
46 -> asio::awaitable<std::optional<std::string>>;
47
48 private:
49 auto DispatchSingleRequest(Request request)
50 -> asio::awaitable<std::optional<Response>>;
51
52 auto DispatchBatchRequest(std::vector<Request> requests)
53 -> asio::awaitable<std::vector<Response>>;
54
55 std::unordered_map<std::string, MethodCallHandler> method_handlers_;
56
57 std::unordered_map<std::string, NotificationHandler> notification_handlers_;
58
59 asio::any_io_executor executor_;
60
61 std::shared_ptr<spdlog::logger> logger_;
62};
63
64} // namespace jsonrpc::endpoint
Dispatcher(const Dispatcher &)=delete
Dispatcher(asio::any_io_executor executor, std::shared_ptr< spdlog::logger > logger=nullptr)
std::function< asio::awaitable< nlohmann::json >( const std::optional< nlohmann::json > &)> MethodCallHandler
auto operator=(const Dispatcher &) -> Dispatcher &=delete
void RegisterNotification(const std::string &method, const NotificationHandler &handler)
auto Logger() -> std::shared_ptr< spdlog::logger >
std::function< asio::awaitable< void >( const std::optional< nlohmann::json > &)> NotificationHandler
auto DispatchRequest(std::string request) -> asio::awaitable< std::optional< std::string > >
Dispatcher(Dispatcher &&)=delete
virtual ~Dispatcher()=default
auto operator=(Dispatcher &&) -> Dispatcher &=delete
void RegisterMethodCall(const std::string &method, const MethodCallHandler &handler)
void(const std::optional< nlohmann::json > &params) NotificationHandler
Definition types.hpp:20
nlohmann::json(const std::optional< nlohmann::json > &params) MethodCallHandler
Definition types.hpp:17