/
async.cpp
148 lines (137 loc) · 4.16 KB
/
async.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "SilkJS.h"
static JSVAL async_alloc_fd_set(JSARGS args) {
fd_set *set = new fd_set;
FD_ZERO(set);
return Opaque::New(set);
}
static JSVAL async_free_fd_set(JSARGS args) {
fd_set *set = (fd_set *)JSOPAQUE(args[0]);
delete set;
return Undefined();
}
static JSVAL async_fd_zero(JSARGS args) {
fd_set *set = (fd_set *)JSOPAQUE(args[0]);
FD_ZERO(set);
return args[0];
}
static JSVAL async_fd_set(JSARGS args) {
int fd = args[0]->IntegerValue();
fd_set *set = (fd_set *)JSOPAQUE(args[1]);
FD_SET(fd, set);
return args[0];
}
static JSVAL async_fd_clr(JSARGS args) {
int fd = args[0]->IntegerValue();
fd_set *set = (fd_set *)JSOPAQUE(args[1]);
FD_CLR(fd, set);
return args[0];
}
static JSVAL async_fd_isset(JSARGS args) {
int fd = args[0]->IntegerValue();
fd_set *set = (fd_set *)JSOPAQUE(args[1]);
return FD_ISSET(fd, set) ? True() : False();
}
static JSVAL async_select(JSARGS args) {
fd_set readfds,
writefds,
exceptfds,
*fds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int maxfd = args[0]->IntegerValue();
if (!args[1]->IsNull() && !args[1]->IsUndefined()) {
fds = (fd_set *)JSOPAQUE(args[1]);
readfds = *fds;
}
else {
FD_ZERO(&readfds);
}
if (!args[2]->IsNull() && !args[2]->IsUndefined()) {
fds = (fd_set *)JSOPAQUE(args[2]);
writefds = *fds;
}
else {
FD_ZERO(&writefds);
}
if (!args[3]->IsNull() && !args[3]->IsUndefined()) {
fds = (fd_set *)JSOPAQUE(args[3]);
exceptfds = *fds;
}
else {
FD_ZERO(&exceptfds);
}
int ret = select(maxfd, &readfds, &writefds, &exceptfds, NULL);
switch (ret) {
case 0:
return False();
case -1:
return False();
}
JSARRAY ra = Array::New();
JSARRAY wa = Array::New();
JSARRAY ea = Array::New();
int randx = 0,
wandx = 0,
eandx = 0;
for (int i=0; i<= maxfd; i++) {
if (FD_ISSET(i, &readfds)) {
ra->Set(randx++, Integer::New(i));
}
else if (FD_ISSET(i, &writefds)) {
wa->Set(wandx++, Integer::New(i));
}
else if (FD_ISSET(i, &exceptfds)) {
ea->Set(eandx++, Integer::New(i));
}
}
JSOBJ o = Object::New();
o->Set(String::New("read"), ra);
o->Set(String::New("write"), wa);
o->Set(String::New("except"), ea);
return o;
}
JSVAL async_write(JSARGS args) {
int fd = args[0]->IntegerValue();
String::AsciiValue s(args[1]);
size_t size = args[2]->IntegerValue();
return Integer::New(write(fd, *s, size));
}
JSVAL async_read(JSARGS args) {
int fd = args[0]->IntegerValue();
size_t size = args[1]->IntegerValue();
char *buf = new char[size+1];
int ret = read(fd, buf, size);
if (ret == 0) {
// printf("Async read %d %s\n", fd, strerror(errno));
delete[] buf;
return False();
}
else if (ret < 0) {
delete[] buf;
return False();
}
else {
Handle<String>s = String::New(buf, ret);
delete[] buf;
return s;
}
}
JSVAL async_close(JSARGS args) {
int fd = args[0]->IntegerValue();
return Integer::New(close(fd));
}
void init_async_object () {
Handle<ObjectTemplate>async = ObjectTemplate::New();
async->Set(String::New("alloc_fd_set"), FunctionTemplate::New(async_alloc_fd_set));
async->Set(String::New("free_fd_set"), FunctionTemplate::New(async_free_fd_set));
async->Set(String::New("FD_ZERO"), FunctionTemplate::New(async_fd_zero));
async->Set(String::New("FD_SET"), FunctionTemplate::New(async_fd_set));
async->Set(String::New("FD_CLR"), FunctionTemplate::New(async_fd_clr));
async->Set(String::New("FD_ISSET"), FunctionTemplate::New(async_fd_isset));
async->Set(String::New("select"), FunctionTemplate::New(async_select));
async->Set(String::New("read"), FunctionTemplate::New(async_read));
async->Set(String::New("write"), FunctionTemplate::New(async_write));
async->Set(String::New("close"), FunctionTemplate::New(async_close));
builtinObject->Set(String::New("async"), async);
}