A class representing a pending RPC request.
More...
#include <pending_request.hpp>
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.
◆ PendingRequest() [1/3]
jsonrpc::endpoint::PendingRequest::PendingRequest |
( |
asio::strand< asio::any_io_executor > | strand | ) |
|
|
inlineexplicit |
Construct a new Pending Request object.
- Parameters
-
strand | The 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 |
◆ Cancel()
void jsonrpc::endpoint::PendingRequest::Cancel |
( |
int | code, |
|
|
const std::string & | message ) |
|
inline |
Cancels the request with an error.
- Parameters
-
code | The error code |
message | The error message |
Definition at line 64 of file pending_request.hpp.
64 {
65
66 nlohmann::json error = {{"error", {{"code", code}, {"message", message}}}};
67
68 has_error_ = true;
69
70
72 }
void SetResult(nlohmann::json result)
Sets the result of the request.
References SetResult().
◆ 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
81 if (is_ready_) {
82 co_return result_;
83 }
84
85
86
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
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]
◆ operator=() [2/2]
◆ SetResult()
void jsonrpc::endpoint::PendingRequest::SetResult |
( |
nlohmann::json | result | ) |
|
|
inline |
Sets the result of the request.
- Parameters
-
Definition at line 44 of file pending_request.hpp.
44 {
45
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
52 std::lock_guard<std::mutex> lock(mutex_);
53 ready_cv_.notify_all();
54 }
55 });
56 }
Referenced by Cancel().
The documentation for this class was generated from the following file: