CCF
Loading...
Searching...
No Matches
signal.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 <int signum, typename Behaviour>
12 class Signal : public with_uv_handle<uv_signal_t>
13 {
14 private:
15 friend class close_ptr<Signal<signum, Behaviour>>;
16 Behaviour behaviour;
17
18 template <typename... Args>
19 Signal(Args&&... args) : behaviour(std::forward<Args>(args)...)
20 {
21 int rc;
22
23 if ((rc = uv_signal_init(uv_default_loop(), &uv_handle)) < 0)
24 {
25 LOG_FAIL_FMT("uv_signal_init failed: {}", uv_strerror(rc));
26 throw std::logic_error("uv_signal_init failed");
27 }
28
29 uv_handle.data = this;
30
31 if ((rc = uv_signal_start(&uv_handle, on_signal, signum)) < 0)
32 {
33 LOG_FAIL_FMT("uv_signal_start failed: {}", uv_strerror(rc));
34 throw std::logic_error("uv_signal_start failed");
35 }
36 }
37
38 static void on_signal(uv_signal_t* handle, int signal)
39 {
40 static_cast<Signal*>(handle->data)->on_signal(signal);
41 }
42
43 void on_signal(int signal)
44 {
45 behaviour.on_signal(signal);
46 uv_signal_stop(&uv_handle);
47 }
48 };
49}
Definition signal.h:13
Definition proxy.h:15
Definition proxy.h:82
uv_signal_t uv_handle
Definition proxy.h:84
#define LOG_FAIL_FMT
Definition logger.h:363
Definition after_io.h:8