cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
call_future_impl.h
Go to the documentation of this file.
1
/*
2
* Copyright 2023 MusicScience37 (Kenta Kabashima)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
20
#pragma once
21
22
#include <algorithm>
23
#include <chrono>
24
#include <condition_variable>
25
#include <memory>
26
#include <mutex>
27
#include <optional>
28
#include <utility>
29
30
#include "
msgpack_rpc/clients/impl/i_call_future_impl.h
"
31
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
32
#include "
msgpack_rpc/common/status.h
"
33
#include "
msgpack_rpc/common/status_code.h
"
34
#include "
msgpack_rpc/messages/call_result.h
"
35
36
namespace
msgpack_rpc::clients::impl
{
37
42
class
CallFutureImpl
final
43
:
public
ICallFutureImpl,
44
public
std::enable_shared_from_this<CallFutureImpl> {
45
public
:
51
explicit
CallFutureImpl
(std::chrono::steady_clock::time_point deadline)
52
:
deadline_
(deadline) {}
53
59
void
set
(
messages::CallResult
result) {
60
std::unique_lock<std::mutex> lock(
is_set_mutex_
);
61
if
(
is_set_
) {
62
return
;
63
}
64
65
result_
.emplace(std::move(result));
66
67
is_set_
=
true
;
68
lock.unlock();
69
is_set_cond_var_
.notify_all();
70
}
71
77
void
set
(
const
Status
& error) {
78
std::unique_lock<std::mutex> lock(
is_set_mutex_
);
79
if
(
is_set_
) {
80
return
;
81
}
82
83
if
(error.
code
() == StatusCode::SUCCESS) {
84
throw
MsgpackRPCException
(
85
StatusCode::INVALID_ARGUMENT,
"Invalid error status."
);
86
}
87
status_
= error;
88
89
is_set_
=
true
;
90
lock.unlock();
91
is_set_cond_var_
.notify_all();
92
}
93
95
[[nodiscard]]
messages::CallResult
get_result
()
override
{
96
wait
();
97
return
get_result_impl
();
98
}
99
101
[[nodiscard]]
messages::CallResult
get_result_within
(
102
std::chrono::nanoseconds timeout)
override
{
103
wait_for
(timeout);
104
return
get_result_impl
();
105
}
106
107
private
:
111
void
wait
() {
wait_until_impl
(
deadline_
); }
112
118
void
wait_for
(std::chrono::nanoseconds timeout) {
119
const
auto
deadline = std::min<std::chrono::steady_clock::time_point>(
120
std::chrono::steady_clock::now() + timeout,
deadline_
);
121
wait_until_impl
(deadline);
122
}
123
130
void
wait_until_impl
(std::chrono::steady_clock::time_point deadline) {
131
std::unique_lock<std::mutex> lock(
is_set_mutex_
);
132
if
(!
is_set_cond_var_
.wait_until(
133
lock, deadline, [
this
] { return is_set_; })) {
134
throw
MsgpackRPCException
(StatusCode::TIMEOUT,
135
"Result of an RPC couldn't be received within a timeout."
);
136
}
137
}
138
144
[[nodiscard]]
messages::CallResult
get_result_impl
() {
145
if
(
result_
) {
146
return
*
result_
;
147
}
148
throw
MsgpackRPCException
(
status_
);
149
}
150
152
std::optional<messages::CallResult>
result_
{};
153
155
Status
status_
{};
156
158
bool
is_set_
{
false
};
159
161
std::chrono::steady_clock::time_point
deadline_
;
162
170
std::mutex
is_set_mutex_
{};
171
173
std::condition_variable
is_set_cond_var_
{};
174
};
175
176
}
// namespace msgpack_rpc::clients::impl
call_result.h
Definition of CallResult class.
msgpack_rpc::clients::impl::CallFutureImpl::get_result_within
messages::CallResult get_result_within(std::chrono::nanoseconds timeout) override
Get the result of RPC within a timeout.
Definition
call_future_impl.h:101
msgpack_rpc::clients::impl::CallFutureImpl::wait_until_impl
void wait_until_impl(std::chrono::steady_clock::time_point deadline)
Wait the result until the given deadline without consideration of the deadline of the RPC.
Definition
call_future_impl.h:130
msgpack_rpc::clients::impl::CallFutureImpl::wait
void wait()
Wait the result.
Definition
call_future_impl.h:111
msgpack_rpc::clients::impl::CallFutureImpl::deadline_
std::chrono::steady_clock::time_point deadline_
Deadline of the result of the RPC.
Definition
call_future_impl.h:161
msgpack_rpc::clients::impl::CallFutureImpl::get_result
messages::CallResult get_result() override
Get the result of RPC.
Definition
call_future_impl.h:95
msgpack_rpc::clients::impl::CallFutureImpl::status_
Status status_
Status.
Definition
call_future_impl.h:155
msgpack_rpc::clients::impl::CallFutureImpl::wait_for
void wait_for(std::chrono::nanoseconds timeout)
Wait the result for the given time.
Definition
call_future_impl.h:118
msgpack_rpc::clients::impl::CallFutureImpl::CallFutureImpl
CallFutureImpl(std::chrono::steady_clock::time_point deadline)
Constructor.
Definition
call_future_impl.h:51
msgpack_rpc::clients::impl::CallFutureImpl::set
void set(messages::CallResult result)
Set a result.
Definition
call_future_impl.h:59
msgpack_rpc::clients::impl::CallFutureImpl::is_set_mutex_
std::mutex is_set_mutex_
Mutex of is_set_.
Definition
call_future_impl.h:170
msgpack_rpc::clients::impl::CallFutureImpl::is_set_
bool is_set_
Whether a result or an error is set.
Definition
call_future_impl.h:158
msgpack_rpc::clients::impl::CallFutureImpl::result_
std::optional< messages::CallResult > result_
Result of the RPC.
Definition
call_future_impl.h:152
msgpack_rpc::clients::impl::CallFutureImpl::get_result_impl
messages::CallResult get_result_impl()
Get the result assuming the result is already set.
Definition
call_future_impl.h:144
msgpack_rpc::clients::impl::CallFutureImpl::is_set_cond_var_
std::condition_variable is_set_cond_var_
Condition variable for notifying change of is_set_.
Definition
call_future_impl.h:173
msgpack_rpc::clients::impl::CallFutureImpl::set
void set(const Status &error)
Set an error.
Definition
call_future_impl.h:77
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::Status
Class of statuses.
Definition
status.h:34
msgpack_rpc::common::Status::code
StatusCode code() const noexcept
Get the status code.
Definition
status.cpp:69
msgpack_rpc::messages::CallResult
Class of results of methods.
Definition
call_result.h:35
i_call_future_impl.h
Definition of ICallFutureImpl class.
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::clients::impl
Namespace of internal implementations.
Definition
i_call_future_impl.h:26
status.h
Definition of Status class.
status_code.h
Definition of StatusCode enumeration.
src
msgpack_rpc
clients
impl
call_future_impl.h
Generated on
for cpp-msgpack-rpc by
1.18.0