OpenVPN 3 Core Library
Loading...
Searching...
No Matches
asiotimersafe.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <openvpn/common/rc.hpp>
16
17// AsioTimerSafe is like AsioTimer but with strict cancellation
18// semantics that guarantees that a handler will never be called
19// with a non-error status after the timer is cancelled.
20
21namespace openvpn {
23{
24 public:
25 typedef std::unique_ptr<AsioTimerSafe> UPtr;
26
27 AsioTimerSafe(openvpn_io::io_context &io_context)
28 : timer_(io_context),
29 epoch_(new Epoch)
30 {
31 }
32
33 std::size_t expires_at(const Time &t)
34 {
35 inc_epoch();
36 return timer_.expires_at(t);
37 }
38
39 std::size_t expires_after(const Time::Duration &d)
40 {
41 inc_epoch();
42 return timer_.expires_after(d);
43 }
44
45 std::size_t cancel()
46 {
47 inc_epoch();
48 return timer_.cancel();
49 }
50
51 template <typename F>
52 void async_wait(F &&func)
53 {
54 inc_epoch();
55 timer_.async_wait([func = std::move(func), epoch = epoch(), eptr = epoch_](const openvpn_io::error_code &error)
56 { func(epoch == eptr->epoch ? error : openvpn_io::error::operation_aborted); });
57 }
58
59 private:
60 typedef std::size_t epoch_t;
61
62 struct Epoch : public RC<thread_unsafe_refcount>
63 {
66 };
67
68 epoch_t epoch() const
69 {
70 return epoch_->epoch;
71 }
72
73 void inc_epoch()
74 {
75 ++epoch_->epoch;
76 }
77
80};
81} // namespace openvpn
std::size_t expires_at(const Time &t)
std::size_t expires_after(const Time::Duration &d)
std::unique_ptr< AsioTimerSafe > UPtr
void async_wait(F &&func)
AsioTimerSafe(openvpn_io::io_context &io_context)
std::size_t expires_at(const Time &t)
Definition asiotimer.hpp:64
std::size_t expires_after(const Time::Duration &d)
Definition asiotimer.hpp:69
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912