diff options
| author | Diego Nehab <diego.nehab@gmail.com> | 2015-08-21 15:39:34 -0300 |
|---|---|---|
| committer | Diego Nehab <diego.nehab@gmail.com> | 2015-08-21 15:39:34 -0300 |
| commit | e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed (patch) | |
| tree | 71475c18fee070c770fc0fe25d0859b7d54c8fbb /src/select.c | |
| parent | 321c0c9b1f7b6b83cd83b58e7e259f53eca69373 (diff) | |
| download | luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.gz luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.bz2 luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.zip | |
New compat.h module implements luaL_setfuncs.
Makes initialization code simpler everywhere.
Diffstat (limited to 'src/select.c')
| -rw-r--r-- | src/select.c | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/select.c b/src/select.c index fafaa62..d14c40a 100644 --- a/src/select.c +++ b/src/select.c | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include "lua.h" | 7 | #include "lua.h" |
| 8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
| 9 | #include "compat.h" | ||
| 9 | 10 | ||
| 10 | #include "socket.h" | 11 | #include "socket.h" |
| 11 | #include "timeout.h" | 12 | #include "timeout.h" |
| @@ -16,10 +17,10 @@ | |||
| 16 | \*=========================================================================*/ | 17 | \*=========================================================================*/ |
| 17 | static t_socket getfd(lua_State *L); | 18 | static t_socket getfd(lua_State *L); |
| 18 | static int dirty(lua_State *L); | 19 | static int dirty(lua_State *L); |
| 19 | static void collect_fd(lua_State *L, int tab, int itab, | 20 | static void collect_fd(lua_State *L, int tab, int itab, |
| 20 | fd_set *set, t_socket *max_fd); | 21 | fd_set *set, t_socket *max_fd); |
| 21 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); | 22 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); |
| 22 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, | 23 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, |
| 23 | int itab, int tab, int start); | 24 | int itab, int tab, int start); |
| 24 | static void make_assoc(lua_State *L, int tab); | 25 | static void make_assoc(lua_State *L, int tab); |
| 25 | static int global_select(lua_State *L); | 26 | static int global_select(lua_State *L); |
| @@ -40,11 +41,7 @@ int select_open(lua_State *L) { | |||
| 40 | lua_pushstring(L, "_SETSIZE"); | 41 | lua_pushstring(L, "_SETSIZE"); |
| 41 | lua_pushnumber(L, FD_SETSIZE); | 42 | lua_pushnumber(L, FD_SETSIZE); |
| 42 | lua_rawset(L, -3); | 43 | lua_rawset(L, -3); |
| 43 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
| 44 | luaL_setfuncs(L, func, 0); | 44 | luaL_setfuncs(L, func, 0); |
| 45 | #else | ||
| 46 | luaL_openlib(L, NULL, func, 0); | ||
| 47 | #endif | ||
| 48 | return 0; | 45 | return 0; |
| 49 | } | 46 | } |
| 50 | 47 | ||
| @@ -98,10 +95,10 @@ static t_socket getfd(lua_State *L) { | |||
| 98 | lua_pushvalue(L, -2); | 95 | lua_pushvalue(L, -2); |
| 99 | lua_call(L, 1, 1); | 96 | lua_call(L, 1, 1); |
| 100 | if (lua_isnumber(L, -1)) { | 97 | if (lua_isnumber(L, -1)) { |
| 101 | double numfd = lua_tonumber(L, -1); | 98 | double numfd = lua_tonumber(L, -1); |
| 102 | fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; | 99 | fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; |
| 103 | } | 100 | } |
| 104 | } | 101 | } |
| 105 | lua_pop(L, 1); | 102 | lua_pop(L, 1); |
| 106 | return fd; | 103 | return fd; |
| 107 | } | 104 | } |
| @@ -114,12 +111,12 @@ static int dirty(lua_State *L) { | |||
| 114 | lua_pushvalue(L, -2); | 111 | lua_pushvalue(L, -2); |
| 115 | lua_call(L, 1, 1); | 112 | lua_call(L, 1, 1); |
| 116 | is = lua_toboolean(L, -1); | 113 | is = lua_toboolean(L, -1); |
| 117 | } | 114 | } |
| 118 | lua_pop(L, 1); | 115 | lua_pop(L, 1); |
| 119 | return is; | 116 | return is; |
| 120 | } | 117 | } |
| 121 | 118 | ||
| 122 | static void collect_fd(lua_State *L, int tab, int itab, | 119 | static void collect_fd(lua_State *L, int tab, int itab, |
| 123 | fd_set *set, t_socket *max_fd) { | 120 | fd_set *set, t_socket *max_fd) { |
| 124 | int i = 1, n = 0; | 121 | int i = 1, n = 0; |
| 125 | /* nil is the same as an empty table */ | 122 | /* nil is the same as an empty table */ |
| @@ -139,16 +136,16 @@ static void collect_fd(lua_State *L, int tab, int itab, | |||
| 139 | if (fd != SOCKET_INVALID) { | 136 | if (fd != SOCKET_INVALID) { |
| 140 | /* make sure we don't overflow the fd_set */ | 137 | /* make sure we don't overflow the fd_set */ |
| 141 | #ifdef _WIN32 | 138 | #ifdef _WIN32 |
| 142 | if (n >= FD_SETSIZE) | 139 | if (n >= FD_SETSIZE) |
| 143 | luaL_argerror(L, tab, "too many sockets"); | 140 | luaL_argerror(L, tab, "too many sockets"); |
| 144 | #else | 141 | #else |
| 145 | if (fd >= FD_SETSIZE) | 142 | if (fd >= FD_SETSIZE) |
| 146 | luaL_argerror(L, tab, "descriptor too large for set size"); | 143 | luaL_argerror(L, tab, "descriptor too large for set size"); |
| 147 | #endif | 144 | #endif |
| 148 | FD_SET(fd, set); | 145 | FD_SET(fd, set); |
| 149 | n++; | 146 | n++; |
| 150 | /* keep track of the largest descriptor so far */ | 147 | /* keep track of the largest descriptor so far */ |
| 151 | if (*max_fd == SOCKET_INVALID || *max_fd < fd) | 148 | if (*max_fd == SOCKET_INVALID || *max_fd < fd) |
| 152 | *max_fd = fd; | 149 | *max_fd = fd; |
| 153 | /* make sure we can map back from descriptor to the object */ | 150 | /* make sure we can map back from descriptor to the object */ |
| 154 | lua_pushnumber(L, (lua_Number) fd); | 151 | lua_pushnumber(L, (lua_Number) fd); |
| @@ -162,9 +159,9 @@ static void collect_fd(lua_State *L, int tab, int itab, | |||
| 162 | 159 | ||
| 163 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { | 160 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { |
| 164 | int ndirty = 0, i = 1; | 161 | int ndirty = 0, i = 1; |
| 165 | if (lua_isnil(L, tab)) | 162 | if (lua_isnil(L, tab)) |
| 166 | return 0; | 163 | return 0; |
| 167 | for ( ;; ) { | 164 | for ( ;; ) { |
| 168 | t_socket fd; | 165 | t_socket fd; |
| 169 | lua_pushnumber(L, i); | 166 | lua_pushnumber(L, i); |
| 170 | lua_gettable(L, tab); | 167 | lua_gettable(L, tab); |
| @@ -185,7 +182,7 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { | |||
| 185 | return ndirty; | 182 | return ndirty; |
| 186 | } | 183 | } |
| 187 | 184 | ||
| 188 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, | 185 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, |
| 189 | int itab, int tab, int start) { | 186 | int itab, int tab, int start) { |
| 190 | t_socket fd; | 187 | t_socket fd; |
| 191 | for (fd = 0; fd < max_fd; fd++) { | 188 | for (fd = 0; fd < max_fd; fd++) { |
