author | Kim Alvefur <zash@zash.se> |
Sat, 20 Apr 2019 15:11:04 +0200 | |
changeset 9973 | 61bc5c52c941 |
parent 9958 | 36280801ddf1 |
child 10100 | 46a7792fdac5 |
permissions | -rw-r--r-- |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
* Lua polling library |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
* Copyright (C) 2017-2018 Kim Alvefur |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
* This project is MIT licensed. Please see the |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
* COPYING file in the source package for more information. |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
#include <unistd.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
#include <string.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
#include <errno.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
#ifdef __linux__ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
#define USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
#include <sys/epoll.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
#ifndef MAX_EVENTS |
9453
b890ceb1c24f
util.poll: Increase max epoll events per call
Kim Alvefur <zash@zash.se>
parents:
9451
diff
changeset
|
22 |
#define MAX_EVENTS 64 |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
#include <sys/select.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
#include <lualib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
#include <lauxlib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
#ifdef USE_EPOLL |
9443
1ddecde4ce98
util.poll: Tweak metatable name field
Kim Alvefur <zash@zash.se>
parents:
9440
diff
changeset
|
32 |
#define STATE_MT "util.poll<epoll>" |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
#else |
9443
1ddecde4ce98
util.poll: Tweak metatable name field
Kim Alvefur <zash@zash.se>
parents:
9440
diff
changeset
|
34 |
#define STATE_MT "util.poll<select>" |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
|
9321 | 37 |
#if (LUA_VERSION_NUM == 501) |
38 |
#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2) |
|
39 |
#endif |
|
40 |
||
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
* Structure to keep state for each type of API |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
typedef struct Lpoll_state { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 |
int processed; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 |
int epoll_fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
struct epoll_event events[MAX_EVENTS]; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 |
fd_set wantread; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
fd_set wantwrite; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
fd_set readable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
fd_set writable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
fd_set all; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 |
fd_set err; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
} Lpoll_state; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
* Add an FD to be watched |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
62 |
static int Ladd(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
int fd = luaL_checkinteger(L, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
int wantread = lua_toboolean(L, 3); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 |
int wantwrite = lua_toboolean(L, 4); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
|
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
69 |
if(fd < 0) { |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
70 |
lua_pushnil(L); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
71 |
lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
72 |
lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
73 |
return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
74 |
} |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
75 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 |
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 |
if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 |
ret = errno; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 |
|
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
98 |
if(fd > FD_SETSIZE) { |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
99 |
lua_pushnil(L); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
100 |
lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
101 |
lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
102 |
return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
103 |
} |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
104 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 |
if(FD_ISSET(fd, &state->all)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
lua_pushstring(L, strerror(EEXIST)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 |
lua_pushinteger(L, EEXIST); |
9449
6b4d28eb19cf
util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents:
9443
diff
changeset
|
109 |
return 3; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 |
FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 |
FD_SET(fd, &state->all); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 |
if(wantread) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
FD_SET(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 |
if(wantwrite) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 |
FD_SET(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 |
FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
* Set events to watch for, readable and/or writable |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
140 |
static int Lset(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 |
int fd = luaL_checkinteger(L, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 |
int wantread = lua_toboolean(L, 3); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 |
int wantwrite = lua_toboolean(L, 4); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 |
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 |
ret = errno; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 |
if(!FD_ISSET(fd, &state->all)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 |
lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 |
lua_pushinteger(L, ENOENT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
if(!lua_isnoneornil(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 |
if(lua_toboolean(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 |
FD_SET(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 |
if(!lua_isnoneornil(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 |
if(lua_toboolean(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
FD_SET(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 |
FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 |
* Remove FDs |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
203 |
static int Ldel(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 |
int fd = luaL_checkinteger(L, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 |
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 |
if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 |
ret = errno; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 |
if(!FD_ISSET(fd, &state->all)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 |
lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 |
lua_pushinteger(L, ENOENT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 |
FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 |
FD_CLR(fd, &state->all); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 |
FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 |
* Check previously manipulated event state for FDs ready for reading or writing |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
250 |
static int Lpushevent(lua_State *L, struct Lpoll_state *state) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 |
if(state->processed > 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 |
state->processed--; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 |
struct epoll_event event = state->events[state->processed]; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 |
lua_pushinteger(L, event.data.fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 |
lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 |
lua_pushboolean(L, event.events & EPOLLOUT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 |
for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 |
if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 |
lua_pushinteger(L, fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 |
lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 |
lua_pushboolean(L, FD_ISSET(fd, &state->writable)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 |
FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 |
state->processed = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 |
return 0; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 |
* Wait for event |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
284 |
static int Lwait(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 |
int ret = Lpushevent(L, state); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 |
if(ret != 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 |
return ret; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 |
lua_Number timeout = luaL_checknumber(L, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 |
luaL_argcheck(L, timeout >= 0, 1, "positive number expected"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 |
ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 |
* select(2) mutates the fd_sets passed to it so in order to not |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 |
* have to recreate it manually every time a copy is made. |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 |
memcpy(&state->readable, &state->wantread, sizeof(fd_set)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 |
memcpy(&state->writable, &state->wantwrite, sizeof(fd_set)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 |
memcpy(&state->err, &state->all, sizeof(fd_set)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 |
struct timeval tv; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 |
tv.tv_sec = (time_t)timeout; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 |
tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 |
|
9440
b202aa1e2d7b
util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents:
9321
diff
changeset
|
311 |
ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 |
if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 |
lua_pushstring(L, "timeout"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 |
return 2; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 |
} |
9511
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
319 |
else if(ret < 0 && errno == EINTR) { |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
320 |
lua_pushnil(L); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
321 |
lua_pushstring(L, "signal"); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
322 |
return 2; |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
323 |
} |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 |
else if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 |
ret = errno; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 |
* Search for the first ready FD and return it |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 |
state->processed = ret; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 |
state->processed = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 |
return Lpushevent(L, state); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 |
* Return Epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
347 |
static int Lgetfd(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 |
lua_pushinteger(L, state->epoll_fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 |
* Close epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
356 |
static int Lgc(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 |
|
9481 | 359 |
if(state->epoll_fd == -1) { |
9478
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
360 |
return 0; |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
361 |
} |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
362 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 |
if(close(state->epoll_fd) == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 |
state->epoll_fd = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 |
lua_pushstring(L, strerror(errno)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
368 |
lua_error(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 |
return 0; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 |
* String representation |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
378 |
static int Ltos(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 |
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 |
lua_pushfstring(L, "%s: %p", STATE_MT, state); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
382 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 |
* Create a new context |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
387 |
static int Lnew(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 |
/* Allocate state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 |
Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
390 |
luaL_setmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
391 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 |
/* Initialize state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 |
#ifdef USE_EPOLL |
9479
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
394 |
state->epoll_fd = -1; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 |
state->processed = 0; |
9479
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
396 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
397 |
int epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
398 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
399 |
if(epoll_fd <= 0) { |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
400 |
lua_pushnil(L); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
401 |
lua_pushstring(L, strerror(errno)); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
402 |
lua_pushinteger(L, errno); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
403 |
return 3; |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
404 |
} |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
405 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
406 |
state->epoll_fd = epoll_fd; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 |
#else |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
408 |
FD_ZERO(&state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
409 |
FD_ZERO(&state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 |
FD_ZERO(&state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 |
FD_ZERO(&state->writable); |
9451
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
412 |
FD_ZERO(&state->all); |
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
413 |
FD_ZERO(&state->err); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 |
state->processed = FD_SETSIZE; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
415 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
416 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
419 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
420 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 |
* Open library |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
423 |
int luaopen_util_poll(lua_State *L) { |
9321 | 424 |
#if (LUA_VERSION_NUM > 501) |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 |
luaL_checkversion(L); |
9321 | 426 |
#endif |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
427 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 |
luaL_newmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 |
lua_pushliteral(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
432 |
lua_setfield(L, -2, "__name"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
434 |
lua_pushcfunction(L, Ltos); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
435 |
lua_setfield(L, -2, "__tostring"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 |
lua_createtable(L, 0, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 |
lua_pushcfunction(L, Ladd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 |
lua_setfield(L, -2, "add"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 |
lua_pushcfunction(L, Lset); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 |
lua_setfield(L, -2, "set"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
443 |
lua_pushcfunction(L, Ldel); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 |
lua_setfield(L, -2, "del"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
445 |
lua_pushcfunction(L, Lwait); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
446 |
lua_setfield(L, -2, "wait"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 |
lua_pushcfunction(L, Lgetfd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
449 |
lua_setfield(L, -2, "getfd"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 |
lua_setfield(L, -2, "__index"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
453 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 |
lua_pushcfunction(L, Lgc); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 |
lua_setfield(L, -2, "__gc"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
457 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
458 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 |
|
9510
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
460 |
lua_createtable(L, 0, 3); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 |
lua_pushcfunction(L, Lnew); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 |
lua_setfield(L, -2, "new"); |
9509
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
464 |
|
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
465 |
#define push_errno(named_error) lua_pushinteger(L, named_error);\ |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
466 |
lua_setfield(L, -2, #named_error); |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
467 |
|
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
468 |
push_errno(EEXIST); |
9510
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
469 |
push_errno(ENOENT); |
9509
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
470 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 |