CatapultServer
v0.5.0.1 (Elephant)
|
Go to the documentation of this file.
25 #include <boost/asio/steady_timer.hpp>
28 namespace catapult {
namespace thread {
32 template<
typename TCallback,
typename... TCallbackArgs>
34 :
public std::enable_shared_from_this<StrandedTimedCallback<TCallback, TCallbackArgs...>> {
40 template<
typename TCallbackWrapper>
44 TCallbackWrapper& wrapper,
45 boost::asio::io_context& ioContext,
47 TCallbackArgs&&... timeoutArgs)
58 m_timer.expires_from_now(std::chrono::milliseconds(timeout.
millis()));
60 this->handleTimedOut();
76 bool callback(
const std::tuple<TCallbackArgs...>& args) {
86 callback(std::get<TCallbackArgs>(args)...);
120 :
m_impl(*this, ioContext,
callback, std::forward<TCallbackArgs>(timeoutArgs)...)
128 post([timeout](
auto& impl) { impl.setTimeout(timeout); });
133 post([handler](
auto& impl) { impl.setTimeoutHandler(handler); });
137 template<
typename... TArgs>
142 post([args = std::make_tuple(std::forward<TArgs>(args)...)](
auto& impl) {
148 template<
typename THandler>
154 template<
typename THandler>
156 return m_strandWrapper.post(this->shared_from_this(), [handler](
const auto& pThis) {
157 handler(pThis->m_impl);
169 template<
typename TCallback,
typename... TCallbackArgs>
170 auto MakeTimedCallback(boost::asio::io_context& ioContext, TCallback callback, TCallbackArgs&&... timeoutArgs) {
174 std::forward<TCallbackArgs>(timeoutArgs)...);
StrandedTimedCallback(boost::asio::io_context &ioContext, const TCallback &callback, TCallbackArgs &&... timeoutArgs)
Definition: TimedCallback.h:119
Wraps a strand and automatically augments handlers to extend the lifetime of an owning object.
Definition: StrandOwnerLifetimeExtender.h:29
void handleTimedOut()
Definition: TimedCallback.h:92
StrandOwnerLifetimeExtender< StrandedTimedCallback > m_strandWrapper
Definition: TimedCallback.h:164
BasicTimedCallback< StrandedTimedCallback > m_impl
Definition: TimedCallback.h:162
BasicTimedCallback(TCallbackWrapper &wrapper, boost::asio::io_context &ioContext, const TCallback &callback, TCallbackArgs &&... timeoutArgs)
Definition: TimedCallback.h:43
void setTimeoutHandler(const TimeoutHandlerType &handler)
Definition: TimedCallback.h:64
void post(THandler handler)
Definition: TimedCallback.h:155
TCallback m_callback
Definition: TimedCallback.h:107
Represents a time duration.
Definition: TimeSpan.h:30
void setTimeout(const utils::TimeSpan &timeout)
Definition: TimedCallback.h:57
action TimeoutHandlerType
Definition: TimedCallback.h:36
bool m_isCallbackInvoked
Definition: TimedCallback.h:111
auto MakeTimedCallback(boost::asio::io_context &ioContext, TCallback callback, TCallbackArgs &&... timeoutArgs)
Definition: TimedCallback.h:170
std::tuple< TCallbackArgs... > m_timeoutArgs
Definition: TimedCallback.h:108
auto wrap(THandler handler)
Definition: TimedCallback.h:149
Wraps a callback with a timer using an implicit strand.
Definition: TimedCallback.h:41
boost::asio::io_context::strand m_strand
Definition: TimedCallback.h:163
std::function< void()> action
An action function.
Definition: functions.h:27
constexpr uint64_t millis() const
Returns the number of milliseconds.
Definition: TimeSpan.h:91
void setTimeout(const utils::TimeSpan &timeout)
Sets the timeout to timeout (starting from now).
Definition: TimedCallback.h:127
Definition: TimedCallback.h:33
TimeoutHandlerType m_timeoutHandler
Definition: TimedCallback.h:113
Definition: AddressExtractionExtension.cpp:28
void callback(TArgs &&... args)
Invokes the wrapped callback with args.
Definition: TimedCallback.h:138
void setTimeoutHandler(const TimeoutHandlerType &handler)
Sets the timeout handler to timeoutHandler.
Definition: TimedCallback.h:132
bool callback(const std::tuple< TCallbackArgs... > &args)
Definition: TimedCallback.h:76
TCallbackWrapper & m_wrapper
Definition: TimedCallback.h:106
boost::asio::steady_timer m_timer
Definition: TimedCallback.h:110
bool m_isTimedOut
Definition: TimedCallback.h:112