forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_signal_watcher.cc
More file actions
81 lines (56 loc) · 1.82 KB
/
node_signal_watcher.cc
File metadata and controls
81 lines (56 loc) · 1.82 KB
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
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#include <node_signal_watcher.h>
#include <assert.h>
namespace node {
using namespace v8;
void SignalWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = BuildTemplate<SignalWatcher>("SignalWatcher");
NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
NODE_SET_PROTOTYPE_METHOD(t, "set", Set);
target->Set(String::NewSymbol("SignalWatcher"), t->GetFunction());
}
void SignalWatcher::Callback(EV_P_ ev_signal *watcher, int revents) {
SignalWatcher *w = static_cast<SignalWatcher*>(watcher->data);
assert(watcher == &w->watcher_);
w->MakeCallback(0, NULL);
}
Handle<Value> SignalWatcher::Set(const Arguments& args) {
HandleScope scope;
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
if (args.Length() != 1 || !args[0]->IsInt32()) {
return ThrowException(String::New("Bad arguments"));
}
int sig = args[0]->Int32Value();
ev_signal_set(&w->watcher_, sig);
return Undefined();
}
Handle<Value> SignalWatcher::Start(const Arguments& args) {
HandleScope scope;
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
w->Start();
return Undefined();
}
void SignalWatcher::Start () {
if (!ev_is_active(&watcher_)) {
ev_signal_start(EV_DEFAULT_UC_ &watcher_);
ev_unref(EV_DEFAULT_UC);
Active();
}
}
Handle<Value> SignalWatcher::Stop(const Arguments& args) {
HandleScope scope;
SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder());
w->Stop();
return Undefined();
}
void SignalWatcher::Stop () {
if (ev_is_active(&watcher_)) {
ev_ref(EV_DEFAULT_UC);
ev_signal_stop(EV_DEFAULT_UC_ &watcher_);
Inactive();
}
}
} // namespace node
NODE_MODULE(node_signal_watcher, node::SignalWatcher::Initialize);