diff options
Diffstat (limited to 'src/select.c')
-rw-r--r-- | src/select.c | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/select.c b/src/select.c index 49730d1..3c01b06 100644 --- a/src/select.c +++ b/src/select.c | |||
@@ -10,6 +10,7 @@ | |||
10 | #include <lauxlib.h> | 10 | #include <lauxlib.h> |
11 | 11 | ||
12 | #include "socket.h" | 12 | #include "socket.h" |
13 | #include "timeout.h" | ||
13 | #include "select.h" | 14 | #include "select.h" |
14 | 15 | ||
15 | /*=========================================================================*\ | 16 | /*=========================================================================*\ |
@@ -48,19 +49,21 @@ int select_open(lua_State *L) { | |||
48 | * Waits for a set of sockets until a condition is met or timeout. | 49 | * Waits for a set of sockets until a condition is met or timeout. |
49 | \*-------------------------------------------------------------------------*/ | 50 | \*-------------------------------------------------------------------------*/ |
50 | static int global_select(lua_State *L) { | 51 | static int global_select(lua_State *L) { |
51 | int timeout, rtab, wtab, itab, max_fd, ret, ndirty; | 52 | int rtab, wtab, itab, max_fd, ret, ndirty; |
52 | fd_set rset, wset; | 53 | fd_set rset, wset; |
54 | t_tm tm; | ||
55 | double t = luaL_optnumber(L, 3, -1); | ||
53 | FD_ZERO(&rset); FD_ZERO(&wset); | 56 | FD_ZERO(&rset); FD_ZERO(&wset); |
54 | lua_settop(L, 3); | 57 | lua_settop(L, 3); |
55 | timeout = lua_isnil(L, 3) ? -1 : (int)(luaL_checknumber(L, 3) * 1000); | ||
56 | lua_newtable(L); itab = lua_gettop(L); | 58 | lua_newtable(L); itab = lua_gettop(L); |
57 | lua_newtable(L); rtab = lua_gettop(L); | 59 | lua_newtable(L); rtab = lua_gettop(L); |
58 | lua_newtable(L); wtab = lua_gettop(L); | 60 | lua_newtable(L); wtab = lua_gettop(L); |
59 | max_fd = collect_fd(L, 1, -1, itab, &rset); | 61 | max_fd = collect_fd(L, 1, -1, itab, &rset); |
60 | ndirty = check_dirty(L, 1, rtab, &rset); | 62 | ndirty = check_dirty(L, 1, rtab, &rset); |
61 | timeout = ndirty > 0? 0: timeout; | 63 | t = ndirty > 0? 0.0: t; |
64 | tm_init(&tm, t, -1); | ||
62 | max_fd = collect_fd(L, 2, max_fd, itab, &wset); | 65 | max_fd = collect_fd(L, 2, max_fd, itab, &wset); |
63 | ret = sock_select(max_fd+1, &rset, &wset, NULL, timeout); | 66 | ret = sock_select(max_fd+1, &rset, &wset, NULL, &tm); |
64 | if (ret > 0 || (ret == 0 && ndirty > 0)) { | 67 | if (ret > 0 || (ret == 0 && ndirty > 0)) { |
65 | return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); | 68 | return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); |
66 | return_fd(L, &wset, max_fd+1, itab, wtab, 0); | 69 | return_fd(L, &wset, max_fd+1, itab, wtab, 0); |