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

A class representing a pending RPC request. More...

#include <pending_request.hpp>

Collaboration diagram for jsonrpc::endpoint::PendingRequest:

Public Member Functions

 PendingRequest (asio::strand< asio::any_io_executor > strand)
 Construct a new Pending Request object.
 
 PendingRequest (const PendingRequest &)=delete
 
auto operator= (const PendingRequest &) -> PendingRequest &=delete
 
 PendingRequest (PendingRequest &&)=delete
 
auto operator= (PendingRequest &&) -> PendingRequest &=delete
 
 ~PendingRequest ()=default
 Destructor.
 
void SetResult (nlohmann::json result)
 Sets the result of the request.
 
void Cancel (int code, const std::string &message)
 Cancels the request with an error.
 
auto GetResult () -> asio::awaitable< nlohmann::json >
 Get the result asynchronously.
 
bool IsReady () const
 Check if the request is ready.
 
bool HasError () const
 Check if the request has an error.
 

Detailed Description

A class representing a pending RPC request.

This class provides an awaitable interface for RPC requests using ASIO's awaitable model directly.

Definition at line 17 of file pending_request.hpp.

Constructor & Destructor Documentation

◆ PendingRequest() [1/3]

jsonrpc::endpoint::PendingRequest::PendingRequest ( asio::strand< asio::any_io_executor > strand)
inlineexplicit

Construct a new Pending Request object.

Parameters
strandThe strand to use for synchronization

Definition at line 24 of file pending_request.hpp.

25 : strand_(std::move(strand)) {
26 }

◆ PendingRequest() [2/3]

jsonrpc::endpoint::PendingRequest::PendingRequest ( const PendingRequest & )
delete

◆ PendingRequest() [3/3]

jsonrpc::endpoint::PendingRequest::PendingRequest ( PendingRequest && )
delete

◆ ~PendingRequest()

jsonrpc::endpoint::PendingRequest::~PendingRequest ( )
default

Destructor.

Member Function Documentation

◆ Cancel()

void jsonrpc::endpoint::PendingRequest::Cancel ( int code,
const std::string & message )
inline

Cancels the request with an error.

Parameters
codeThe error code
messageThe error message

Definition at line 64 of file pending_request.hpp.

64 {
65 // Create a JSON-RPC error object
66 nlohmann::json error = {{"error", {{"code", code}, {"message", message}}}};
67
68 has_error_ = true;
69
70 // Set the result with the error
71 SetResult(std::move(error));
72 }
void SetResult(nlohmann::json result)
Sets the result of the request.

References SetResult().

Here is the call graph for this function:

◆ GetResult()

auto jsonrpc::endpoint::PendingRequest::GetResult ( ) -> asio::awaitable<nlohmann::json>
inline

Get the result asynchronously.

Returns
asio::awaitable<nlohmann::json> The result

Definition at line 79 of file pending_request.hpp.

79 {
80 // If the result is already ready, return it immediately
81 if (is_ready_) {
82 co_return result_;
83 }
84
85 // Otherwise, wait for the result using a timer and polling
86 // This is a simplified approach that works with ASIO
87 auto executor = co_await asio::this_coro::executor;
88 asio::steady_timer timer(executor, std::chrono::milliseconds(10));
89
90 while (!is_ready_) {
91 co_await timer.async_wait(asio::use_awaitable);
92 timer.expires_after(std::chrono::milliseconds(10));
93 }
94
95 // Return the result
96 co_return result_;
97 }

◆ HasError()

bool jsonrpc::endpoint::PendingRequest::HasError ( ) const
inlinenodiscard

Check if the request has an error.

Returns
true if the request has an error, false otherwise

Definition at line 113 of file pending_request.hpp.

113 {
114 return has_error_;
115 }

◆ IsReady()

bool jsonrpc::endpoint::PendingRequest::IsReady ( ) const
inlinenodiscard

Check if the request is ready.

Returns
true if the result is ready, false otherwise

Definition at line 104 of file pending_request.hpp.

104 {
105 return is_ready_;
106 }

◆ operator=() [1/2]

auto jsonrpc::endpoint::PendingRequest::operator= ( const PendingRequest & ) -> PendingRequest &=delete
delete

◆ operator=() [2/2]

auto jsonrpc::endpoint::PendingRequest::operator= ( PendingRequest && ) -> PendingRequest &=delete
delete

◆ SetResult()

void jsonrpc::endpoint::PendingRequest::SetResult ( nlohmann::json result)
inline

Sets the result of the request.

Parameters
resultThe JSON result

Definition at line 44 of file pending_request.hpp.

44 {
45 // Execute within the strand to ensure thread safety
46 asio::post(strand_, [this, result = std::move(result)]() mutable {
47 if (!is_ready_) {
48 result_ = std::move(result);
49 is_ready_ = true;
50
51 // Notify any waiting consumers
52 std::lock_guard<std::mutex> lock(mutex_);
53 ready_cv_.notify_all();
54 }
55 });
56 }

Referenced by Cancel().

Here is the caller graph for this function:

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