OpenVPN 3 Core Library
Loading...
Searching...
No Matches
asiotimer.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// Create an Asio time_traits class to allow Asio to natively handle
13// our Time and Time::Duration classes.
14
15#pragma once
16
17#include <chrono>
18#include <memory>
19
20#include <openvpn/io/io.hpp> // was: #include <asio/basic_waitable_timer.hpp>
21
23#include <openvpn/time/time.hpp>
24
25namespace openvpn {
27{
28 typedef olong rep;
29 typedef std::ratio<1, 1024> period; // time resolution of openvpn::Time, note 1024 instead of the usual 1000
30 typedef std::chrono::duration<rep, period> duration;
31 typedef std::chrono::time_point<AsioClock> time_point;
32
33 static constexpr bool is_steady()
34 {
35 return false;
36 }
37
38 static time_point now()
39 {
40 return to_time_point(Time::now());
41 }
42
43 static time_point to_time_point(const Time &t)
44 {
45 return time_point(duration(t.raw()));
46 }
47
48 static duration to_duration(const Time::Duration &d)
49 {
50 return duration(d.raw());
51 }
52};
53
54class AsioTimer : public openvpn_io::basic_waitable_timer<AsioClock>
55{
56 public:
57 typedef std::unique_ptr<AsioTimer> UPtr;
58
59 AsioTimer(openvpn_io::io_context &io_context)
60 : openvpn_io::basic_waitable_timer<AsioClock>(io_context)
61 {
62 }
63
64 std::size_t expires_at(const Time &t)
65 {
66 return openvpn_io::basic_waitable_timer<AsioClock>::expires_at(AsioClock::to_time_point(t));
67 }
68
69 std::size_t expires_after(const Time::Duration &d)
70 {
71 return openvpn_io::basic_waitable_timer<AsioClock>::expires_after(AsioClock::to_duration(d));
72 }
73};
74} // namespace openvpn
std::size_t expires_at(const Time &t)
Definition asiotimer.hpp:64
AsioTimer(openvpn_io::io_context &io_context)
Definition asiotimer.hpp:59
std::unique_ptr< AsioTimer > UPtr
Definition asiotimer.hpp:57
std::size_t expires_after(const Time::Duration &d)
Definition asiotimer.hpp:69
T raw() const
Definition time.hpp:409
static TimeType now()
Definition time.hpp:305
long olong
Definition olong.hpp:23
static duration to_duration(const Time::Duration &d)
Definition asiotimer.hpp:48
static time_point now()
Definition asiotimer.hpp:38
static time_point to_time_point(const Time &t)
Definition asiotimer.hpp:43
std::chrono::duration< rep, period > duration
Definition asiotimer.hpp:30
std::ratio< 1, 1024 > period
Definition asiotimer.hpp:29
std::chrono::time_point< AsioClock > time_point
Definition asiotimer.hpp:31
static constexpr bool is_steady()
Definition asiotimer.hpp:33