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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Lua polling library
* Copyright (C) 2017-2018 Kim Alvefur
*
* This project is MIT licensed. Please see the
* COPYING file in the source package for more information.
*
*/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#ifdef __linux__
#define USE_EPOLL
#endif
#ifdef USE_EPOLL
#include <sys/epoll.h>
#ifndef MAX_EVENTS
#define MAX_EVENTS 10
#endif
#else
#include <sys/select.h>
#endif
#include <lualib.h>
#include <lauxlib.h>
#ifdef USE_EPOLL
#define STATE_MT "util.poll<epoll>"
#else
#define STATE_MT "util.poll<select>"
#endif
#if (LUA_VERSION_NUM == 501)
#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
#endif
/*
* Structure to keep state for each type of API
*/
typedef struct Lpoll_state {
int processed;
#ifdef USE_EPOLL
int epoll_fd;
struct epoll_event events[MAX_EVENTS];
#else
fd_set wantread;
fd_set wantwrite;
fd_set readable;
fd_set writable;
fd_set all;
fd_set err;
#endif
} Lpoll_state;
/*
* Add an FD to be watched
*/
int Ladd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
int wantread = lua_toboolean(L, 3);
int wantwrite = lua_toboolean(L, 4);
if(fd < 0) {
lua_pushnil(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
}
#ifdef USE_EPOLL
struct epoll_event event;
event.data.fd = fd;
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
if(ret < 0) {
ret = errno;
lua_pushnil(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
lua_pushboolean(L, 1);
return 1;
#else
if(fd > FD_SETSIZE) {
lua_pushnil(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
}
if(FD_ISSET(fd, &state->all)) {
lua_pushnil(L);
lua_pushstring(L, strerror(EEXIST));
lua_pushinteger(L, EEXIST);
return 3;
}
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->err);
FD_SET(fd, &state->all);
if(wantread) {
FD_SET(fd, &state->wantread);
}
else {
FD_CLR(fd, &state->wantread);
}
if(wantwrite) {
FD_SET(fd, &state->wantwrite);
}
else {
FD_CLR(fd, &state->wantwrite);
}
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Set events to watch for, readable and/or writable
*/
int Lset(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
#ifdef USE_EPOLL
int wantread = lua_toboolean(L, 3);
int wantwrite = lua_toboolean(L, 4);
struct epoll_event event;
event.data.fd = fd;
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
if(ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
else {
ret = errno;
lua_pushnil(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
#else
if(!FD_ISSET(fd, &state->all)) {
lua_pushnil(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
}
if(!lua_isnoneornil(L, 3)) {
if(lua_toboolean(L, 3)) {
FD_SET(fd, &state->wantread);
}
else {
FD_CLR(fd, &state->wantread);
}
}
if(!lua_isnoneornil(L, 4)) {
if(lua_toboolean(L, 4)) {
FD_SET(fd, &state->wantwrite);
}
else {
FD_CLR(fd, &state->wantwrite);
}
}
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Remove FDs
*/
int Ldel(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
#ifdef USE_EPOLL
struct epoll_event event;
event.data.fd = fd;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
if(ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
else {
ret = errno;
lua_pushnil(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
#else
if(!FD_ISSET(fd, &state->all)) {
lua_pushnil(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
}
FD_CLR(fd, &state->wantread);
FD_CLR(fd, &state->wantwrite);
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->all);
FD_CLR(fd, &state->err);
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Check previously manipulated event state for FDs ready for reading or writing
*/
int Lpushevent(lua_State *L, struct Lpoll_state *state) {
#ifdef USE_EPOLL
if(state->processed > 0) {
state->processed--;
struct epoll_event event = state->events[state->processed];
lua_pushinteger(L, event.data.fd);
lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
lua_pushboolean(L, event.events & EPOLLOUT);
return 3;
}
#else
for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
lua_pushinteger(L, fd);
lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
lua_pushboolean(L, FD_ISSET(fd, &state->writable));
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->err);
state->processed = fd;
return 3;
}
}
#endif
return 0;
}
/*
* Wait for event
*/
int Lwait(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int ret = Lpushevent(L, state);
if(ret != 0) {
return ret;
}
lua_Number timeout = luaL_checknumber(L, 2);
luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
#ifdef USE_EPOLL
ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
#else
/*
* select(2) mutates the fd_sets passed to it so in order to not
* have to recreate it manually every time a copy is made.
*/
memcpy(&state->readable, &state->wantread, sizeof(fd_set));
memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
memcpy(&state->err, &state->all, sizeof(fd_set));
struct timeval tv;
tv.tv_sec = (time_t)timeout;
tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
#endif
if(ret == 0) {
lua_pushnil(L);
lua_pushstring(L, "timeout");
return 2;
}
else if(ret < 0) {
ret = errno;
lua_pushnil(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
/*
* Search for the first ready FD and return it
*/
#ifdef USE_EPOLL
state->processed = ret;
#else
state->processed = -1;
#endif
return Lpushevent(L, state);
}
#ifdef USE_EPOLL
/*
* Return Epoll FD
*/
int Lgetfd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushinteger(L, state->epoll_fd);
return 1;
}
/*
* Close epoll FD
*/
int Lgc(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
if(close(state->epoll_fd) == 0) {
state->epoll_fd = -1;
}
else {
lua_pushstring(L, strerror(errno));
lua_error(L);
}
return 0;
}
#endif
/*
* String representation
*/
int Ltos(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushfstring(L, "%s: %p", STATE_MT, state);
return 1;
}
/*
* Create a new context
*/
int Lnew(lua_State *L) {
/* Allocate state */
Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
luaL_setmetatable(L, STATE_MT);
/* Initialize state */
#ifdef USE_EPOLL
state->epoll_fd = epoll_create1(0);
state->processed = 0;
#else
FD_ZERO(&state->wantread);
FD_ZERO(&state->wantwrite);
FD_ZERO(&state->readable);
FD_ZERO(&state->writable);
state->processed = FD_SETSIZE;
#endif
return 1;
}
/*
* Open library
*/
int luaopen_util_poll(lua_State *L) {
#if (LUA_VERSION_NUM > 501)
luaL_checkversion(L);
#endif
luaL_newmetatable(L, STATE_MT);
{
lua_pushliteral(L, STATE_MT);
lua_setfield(L, -2, "__name");
lua_pushcfunction(L, Ltos);
lua_setfield(L, -2, "__tostring");
lua_createtable(L, 0, 2);
{
lua_pushcfunction(L, Ladd);
lua_setfield(L, -2, "add");
lua_pushcfunction(L, Lset);
lua_setfield(L, -2, "set");
lua_pushcfunction(L, Ldel);
lua_setfield(L, -2, "del");
lua_pushcfunction(L, Lwait);
lua_setfield(L, -2, "wait");
#ifdef USE_EPOLL
lua_pushcfunction(L, Lgetfd);
lua_setfield(L, -2, "getfd");
#endif
}
lua_setfield(L, -2, "__index");
#ifdef USE_EPOLL
lua_pushcfunction(L, Lgc);
lua_setfield(L, -2, "__gc");
#endif
}
lua_createtable(L, 0, 1);
{
lua_pushcfunction(L, Lnew);
lua_setfield(L, -2, "new");
}
return 1;
}
|