-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathSignal.cpp
executable file
·86 lines (73 loc) · 1.75 KB
/
Signal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
Author: orcaer@yeah.net
Last modified: 2018-8-23
Description: https://github.com/wlgq2/uv-cpp
*/
#include "include/Signal.hpp"
#include "include/LogWriter.hpp"
#include "include/EventLoop.hpp"
using namespace uv;
using namespace std;
Signal::Signal(EventLoop* loop, int sig, SignalHandle handle)
:signal_(new uv_signal_t),
handle_(handle),
closeCallback_(nullptr)
{
::uv_signal_init(loop->handle(), signal_);
signal_->data = static_cast<void*>(this);
::uv_signal_start(signal_, &Signal::onSignal, sig);
}
void uv::Signal::close(DefaultCallback callback)
{
closeCallback_ = callback;
if (uv_is_closing((uv_handle_t*)signal_) == 0)
{
::uv_close((uv_handle_t*)signal_,
[](uv_handle_t* handle)
{
auto ptr = static_cast<Signal*>(handle->data);
ptr->closeComplete();
delete handle;
});
}
else
{
closeCallback_();
}
}
Signal::~Signal()
{
}
void Signal::setHandle(SignalHandle handle)
{
handle_ = handle;
}
bool Signal::handle(int signum)
{
if (handle_)
{
handle_(signum);
return true;
}
return false;
}
void uv::Signal::closeComplete()
{
if (closeCallback_)
closeCallback_();
}
void uv::Signal::Ignore(int sig)
{
#ifdef __linux__
signal(sig, SIG_IGN);
#endif
}
void Signal::onSignal(uv_signal_t* handle, int signum)
{
auto ptr = static_cast <Signal*>(handle->data);
if (!ptr->handle(signum))
{
uv::LogWriter::Instance()->warn( std::string("non defined signal handle :")+std::to_string(signum));
}
}