CCF
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "proxy.h"
6
7#include <chrono>
8
9namespace asynchost
10{
11 template <typename Behaviour>
12 class Timer : public with_uv_handle<uv_timer_t>
13 {
14 public:
15 Behaviour behaviour;
16
17 private:
18 friend class close_ptr<Timer<Behaviour>>;
19
20 template <typename... Args>
21 Timer(std::chrono::milliseconds repeat_ms, Args&&... args) :
22 behaviour(std::forward<Args>(args)...)
23 {
24 int rc;
25
26 if ((rc = uv_timer_init(uv_default_loop(), &uv_handle)) < 0)
27 {
28 LOG_FAIL_FMT("uv_timer_init failed: {}", uv_strerror(rc));
29 throw std::logic_error("uv_timer_init failed");
30 }
31
32 uv_handle.data = this;
33
34 if ((rc = uv_timer_start(&uv_handle, on_timer, 0, repeat_ms.count())) < 0)
35 {
36 LOG_FAIL_FMT("uv_timer_start failed: {}", uv_strerror(rc));
37 throw std::logic_error("uv_timer_start failed");
38 }
39 }
40
41 static void on_timer(uv_timer_t* handle)
42 {
43 static_cast<Timer*>(handle->data)->on_timer();
44 }
45
46 void on_timer()
47 {
48 behaviour.on_timer();
49 }
50 };
51}
Definition timer.h:13
Behaviour behaviour
Definition timer.h:15
Definition proxy.h:15
Definition proxy.h:82
uv_timer_t uv_handle
Definition proxy.h:84
#define LOG_FAIL_FMT
Definition logger.h:363
Definition after_io.h:8
auto handle
Definition kv_helpers.h:85
STL namespace.