cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
timer.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 <chrono>
23
#include <memory>
24
#include <utility>
25
26
#include <asio/error_code.hpp>
27
#include <asio/steady_timer.hpp>
28
29
#include "
msgpack_rpc/executors/i_executor.h
"
30
#include "
msgpack_rpc/executors/operation_type.h
"
31
32
namespace
msgpack_rpc::executors
{
33
37
class
Timer
{
38
public
:
45
Timer
(
const
std::shared_ptr<IExecutor>& executor,
OperationType
type)
46
:
timer_
(executor->context(type)) {}
47
55
template
<
typename
Function>
56
void
async_sleep_until
(
57
std::chrono::steady_clock::time_point time, Function&& function) {
58
timer_
.expires_at(time);
59
timer_
.async_wait([function_moved = std::forward<Function>(function)](
60
const
asio::error_code& error)
mutable
{
61
if
(!error) {
62
function_moved();
63
}
64
});
65
}
66
74
template
<
typename
Function>
75
void
async_sleep_for
(
76
std::chrono::steady_clock::duration duration, Function&& function) {
77
timer_
.expires_after(duration);
78
timer_
.async_wait([function_moved = std::forward<Function>(function)](
79
const
asio::error_code& error)
mutable
{
80
if
(!error) {
81
function_moved();
82
}
83
});
84
}
85
89
void
cancel
() {
timer_
.cancel(); }
90
91
private
:
93
asio::steady_timer
timer_
;
94
};
95
96
}
// namespace msgpack_rpc::executors
msgpack_rpc::executors::Timer::cancel
void cancel()
Cancel this timer.
Definition
timer.h:89
msgpack_rpc::executors::Timer::timer_
asio::steady_timer timer_
Timer in asio library.
Definition
timer.h:93
msgpack_rpc::executors::Timer::Timer
Timer(const std::shared_ptr< IExecutor > &executor, OperationType type)
Constructor.
Definition
timer.h:45
msgpack_rpc::executors::Timer::async_sleep_for
void async_sleep_for(std::chrono::steady_clock::duration duration, Function &&function)
Asynchronously sleep for a duration.
Definition
timer.h:75
msgpack_rpc::executors::Timer::async_sleep_until
void async_sleep_until(std::chrono::steady_clock::time_point time, Function &&function)
Asynchronously sleep until a time point.
Definition
timer.h:56
i_executor.h
Definition of IExecutor class.
msgpack_rpc::executors
Namespace of executors to process asynchronous tasks.
Definition
asio_context_type.h:24
msgpack_rpc::executors::OperationType
OperationType
Enumeration of types of operations.
Definition
operation_type.h:29
operation_type.h
Definition of OperationType enumeration.
src
msgpack_rpc
executors
timer.h
Generated on
for cpp-msgpack-rpc by
1.18.0