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

A JSON-RPC client for sending requests and receiving responses. More...

#include <client.hpp>

Collaboration diagram for jsonrpc::client::Client:

Public Member Functions

 Client (std::unique_ptr< transport::Transport > transport)
 Constructs a JSON-RPC client with a specified transport layer.
 
 ~Client ()=default
 Destructor.
 
 Client (const Client &)=delete
 
auto operator= (const Client &) -> Client &=delete
 
 Client (Client &&) noexcept=delete
 
auto operator= (Client &&) noexcept -> Client &=delete
 
void Start ()
 Starts the JSON-RPC client listener thread.
 
void Stop ()
 Stops the JSON-RPC client listener thread.
 
auto IsRunning () const -> bool
 Checks if the client listener is running.
 
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.
 
auto SendMethodCallAsync (const std::string &method, std::optional< nlohmann::json > params=std::nullopt) -> std::future< nlohmann::json >
 Sends a JSON-RPC method call asynchronously.
 
void SendNotification (const std::string &method, std::optional< nlohmann::json > params=std::nullopt)
 Sends a JSON-RPC notification.
 
auto HasPendingRequests () const -> bool
 Checks if there are any pending requests.
 

Detailed Description

A JSON-RPC client for sending requests and receiving responses.

This client allows for synchronous and asynchronous method calls as well as notifications.

Definition at line 25 of file client.hpp.

Constructor & Destructor Documentation

◆ Client() [1/3]

jsonrpc::client::Client::Client ( std::unique_ptr< transport::Transport > transport)
explicit

Constructs a JSON-RPC client with a specified transport layer.

Parameters
transportA unique pointer to a transport layer used for communication.

Definition at line 7 of file client.cpp.

8 : transport_(std::move(transport)) {
9 spdlog::info("Initializing JSON-RPC client");
10}

◆ ~Client()

jsonrpc::client::Client::~Client ( )
default

Destructor.

◆ Client() [2/3]

jsonrpc::client::Client::Client ( const Client & )
delete

◆ Client() [3/3]

jsonrpc::client::Client::Client ( Client && )
deletenoexcept

Member Function Documentation

◆ HasPendingRequests()

auto jsonrpc::client::Client::HasPendingRequests ( ) const -> bool

Checks if there are any pending requests.

Returns
True if there are pending requests, false otherwise.

Definition at line 30 of file client.cpp.

30 {
31 std::lock_guard<std::mutex> lock(requests_mutex_);
32 return !requests_map_.empty();
33}

◆ IsRunning()

auto jsonrpc::client::Client::IsRunning ( ) const -> bool

Checks if the client listener is running.

Returns
True if the listener thread is running, false otherwise.

Definition at line 26 of file client.cpp.

26 {
27 return is_running_.load();
28}

◆ operator=() [1/2]

auto jsonrpc::client::Client::operator= ( Client && ) -> Client &=delete
deletenoexcept

◆ operator=() [2/2]

auto jsonrpc::client::Client::operator= ( const Client & ) -> Client &=delete
delete

◆ SendMethodCall()

auto jsonrpc::client::Client::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.

This is a blocking call that sends a method request to the server and waits for the corresponding response.

Parameters
methodThe name of the method to call.
paramsOptional parameters to pass to the method.
Returns
The JSON response received from the server.

Definition at line 46 of file client.cpp.

48 {
49 Request request(method, std::move(params), false, [this]() {
50 return GetNextRequestId();
51 });
52 return SendRequest(request);
53}

◆ SendMethodCallAsync()

auto jsonrpc::client::Client::SendMethodCallAsync ( const std::string & method,
std::optional< nlohmann::json > params = std::nullopt ) -> std::future<nlohmann::json>

Sends a JSON-RPC method call asynchronously.

This method sends a request to the server without blocking the calling thread.

Parameters
methodThe name of the method to call.
paramsOptional parameters to pass to the method.
Returns
A future that will hold the JSON response from the server.

Definition at line 55 of file client.cpp.

57 {
58 Request request(method, std::move(params), false, [this]() {
59 return GetNextRequestId();
60 });
61 return SendRequestAsync(request);
62}

◆ SendNotification()

void jsonrpc::client::Client::SendNotification ( const std::string & method,
std::optional< nlohmann::json > params = std::nullopt )

Sends a JSON-RPC notification.

Notifications do not expect a response from the server.

Parameters
methodThe name of the method to notify.
paramsOptional parameters to pass to the method.

Definition at line 64 of file client.cpp.

65 {
66 Request request(
67 method, std::move(params), true, [this]() { return GetNextRequestId(); });
68 transport_->SendMessage(request.Dump());
69}

◆ Start()

void jsonrpc::client::Client::Start ( )

Starts the JSON-RPC client listener thread.

Initializes and starts a background thread that listens for responses from the server. This must be called before sending any requests.

Definition at line 12 of file client.cpp.

12 {
13 spdlog::info("Starting JSON-RPC client");
14 is_running_.store(true);
15 listener_ = std::thread(&Client::Listener, this);
16}

◆ Stop()

void jsonrpc::client::Client::Stop ( )

Stops the JSON-RPC client listener thread.

Stops the background listener thread and waits for it to join. This should be called before the client is destroyed or when the client no longer needs to listen for responses.

Definition at line 18 of file client.cpp.

18 {
19 spdlog::info("Stopping JSON-RPC client");
20 is_running_.store(false);
21 if (listener_.joinable()) {
22 listener_.join();
23 }
24}

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