JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <future>
5#include <memory>
6#include <mutex>
7#include <optional>
8#include <string>
9#include <thread>
10#include <unordered_map>
11
12#include <nlohmann/json_fwd.hpp>
13
16
17namespace jsonrpc::client {
18
25class Client {
26 public:
33 explicit Client(std::unique_ptr<transport::Transport> transport);
34
36 ~Client() = default;
37
38 // Delete copy constructor and copy assignment operator
39 Client(const Client &) = delete;
40 auto operator=(const Client &) -> Client & = delete;
41
42 // Delete move constructor and move assignment operator
43 Client(Client &&) noexcept = delete;
44 auto operator=(Client &&) noexcept -> Client & = delete;
45
52 void Start();
53
61 void Stop();
62
68 auto IsRunning() const -> bool;
69
80 auto SendMethodCall(
81 const std::string &method,
82 std::optional<nlohmann::json> params = std::nullopt) -> nlohmann::json;
83
95 const std::string &method,
96 std::optional<nlohmann::json> params = std::nullopt)
97 -> std::future<nlohmann::json>;
98
107 void SendNotification(
108 const std::string &method,
109 std::optional<nlohmann::json> params = std::nullopt);
110
116 auto HasPendingRequests() const -> bool;
117
118 private:
121 void Listener();
122
133 auto SendRequest(const Request &request) -> nlohmann::json;
134
143 auto SendRequestAsync(const Request &request) -> std::future<nlohmann::json>;
144
150 auto GetNextRequestId() -> int;
151
160 void HandleResponse(const std::string &response);
161
170 static auto ValidateResponse(const nlohmann::json &response) -> bool;
171
173 std::unique_ptr<transport::Transport> transport_;
174
176 std::atomic<int> req_id_counter_{0};
177
179 std::atomic<int> expected_count_{0};
180
182 std::unordered_map<int, std::promise<nlohmann::json>> requests_map_;
183
185 mutable std::mutex requests_mutex_;
186
188 std::thread listener_;
189
191 std::atomic<bool> is_running_{false};
192};
193
194} // namespace jsonrpc::client
A JSON-RPC client for sending requests and receiving responses.
Definition client.hpp:25
~Client()=default
Destructor.
auto operator=(const Client &) -> Client &=delete
void SendNotification(const std::string &method, std::optional< nlohmann::json > params=std::nullopt)
Sends a JSON-RPC notification.
Definition client.cpp:64
void Start()
Starts the JSON-RPC client listener thread.
Definition client.cpp:12
Client(std::unique_ptr< transport::Transport > transport)
Constructs a JSON-RPC client with a specified transport layer.
Definition client.cpp:7
auto IsRunning() const -> bool
Checks if the client listener is running.
Definition client.cpp:26
auto SendMethodCall(const std::string &method, std::optional< nlohmann::json > params=std::nullopt) -> nlohmann::json
Sends a JSON-RPC method call and waits for the response.
Definition client.cpp:46
auto SendMethodCallAsync(const std::string &method, std::optional< nlohmann::json > params=std::nullopt) -> std::future< nlohmann::json >
Sends a JSON-RPC method call asynchronously.
Definition client.cpp:55
void Stop()
Stops the JSON-RPC client listener thread.
Definition client.cpp:18
auto HasPendingRequests() const -> bool
Checks if there are any pending requests.
Definition client.cpp:30
Client(const Client &)=delete
Client(Client &&) noexcept=delete
Represents a JSON-RPC request.
Definition request.hpp:17