CCF
Loading...
Searching...
No Matches
every_io.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
7namespace asynchost
8{
9 // This runs every loop. If any instance of this is active, the loop's poll
10 // timeout will be 0 (see uv_prepare_t vs uv_idle_t)
11 template <typename Behaviour>
12 class EveryIO : public with_uv_handle<uv_idle_t>
13 {
14 private:
15 friend class close_ptr<EveryIO<Behaviour>>;
16 Behaviour behaviour;
17
18 template <typename... Args>
19 EveryIO(Args&&... args) : behaviour(std::forward<Args>(args)...)
20 {
21 int rc;
22
23 if ((rc = uv_idle_init(uv_default_loop(), &uv_handle)) < 0)
24 {
25 LOG_FAIL_FMT("uv_idle_init failed: {}", uv_strerror(rc));
26 throw std::logic_error("uv_idle_init failed");
27 }
28
29 uv_handle.data = this;
30
31 if ((rc = uv_idle_start(&uv_handle, on_every)) < 0)
32 {
33 LOG_FAIL_FMT("uv_idle_start failed: {}", uv_strerror(rc));
34 throw std::logic_error("uv_idle_start failed");
35 }
36 }
37
38 static void on_every(uv_idle_t* handle)
39 {
40 static_cast<EveryIO*>(handle->data)->on_every();
41 }
42
43 void on_every()
44 {
45 behaviour.every();
46 }
47 };
48}
Definition every_io.h:13
Definition proxy.h:15
Definition proxy.h:82
uv_idle_t uv_handle
Definition proxy.h:84
#define LOG_FAIL_FMT
Definition logger.h:363
Definition after_io.h:8