2003-03-28 22:08:50 +01:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Select implementation
|
2003-06-26 20:47:49 +02:00
|
|
|
* LuaSocket toolkit
|
2003-03-28 22:08:50 +01:00
|
|
|
\*=========================================================================*/
|
2019-02-25 23:59:29 +01:00
|
|
|
#include "luasocket.h"
|
2003-06-09 20:23:40 +02:00
|
|
|
|
2005-09-29 08:11:42 +02:00
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
2015-08-21 20:39:34 +02:00
|
|
|
#include "compat.h"
|
2003-03-21 00:11:25 +01:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
#include "socket.h"
|
2004-07-01 05:32:09 +02:00
|
|
|
#include "timeout.h"
|
2003-06-09 20:23:40 +02:00
|
|
|
#include "select.h"
|
2002-07-03 21:06:53 +02:00
|
|
|
|
2019-02-25 23:59:29 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal function prototypes.
|
|
|
|
\*=========================================================================*/
|
2005-11-20 08:20:26 +01:00
|
|
|
static t_socket getfd(lua_State *L);
|
2004-06-15 08:24:00 +02:00
|
|
|
static int dirty(lua_State *L);
|
2015-08-21 20:39:34 +02:00
|
|
|
static void collect_fd(lua_State *L, int tab, int itab,
|
2009-05-27 11:31:38 +02:00
|
|
|
fd_set *set, t_socket *max_fd);
|
2004-06-15 08:24:00 +02:00
|
|
|
static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set);
|
2015-08-21 20:39:34 +02:00
|
|
|
static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
|
2004-06-15 08:24:00 +02:00
|
|
|
int itab, int tab, int start);
|
|
|
|
static void make_assoc(lua_State *L, int tab);
|
2003-06-09 20:23:40 +02:00
|
|
|
static int global_select(lua_State *L);
|
2002-07-03 21:06:53 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
/* functions in library namespace */
|
2011-07-05 00:31:14 +02:00
|
|
|
static luaL_Reg func[] = {
|
2003-06-09 20:23:40 +02:00
|
|
|
{"select", global_select},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2002-03-22 21:07:43 +01:00
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
2004-06-15 08:24:00 +02:00
|
|
|
* Exported functions
|
2003-06-26 20:47:49 +02:00
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2019-02-25 23:59:29 +01:00
|
|
|
LUASOCKET_PRIVATE int select_open(lua_State *L) {
|
2009-05-27 11:31:38 +02:00
|
|
|
lua_pushstring(L, "_SETSIZE");
|
2015-10-06 05:33:50 +02:00
|
|
|
lua_pushinteger(L, FD_SETSIZE);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
lua_pushstring(L, "_SOCKETINVALID");
|
|
|
|
lua_pushinteger(L, SOCKET_INVALID);
|
2009-05-27 11:31:38 +02:00
|
|
|
lua_rawset(L, -3);
|
2013-05-29 10:56:56 +02:00
|
|
|
luaL_setfuncs(L, func, 0);
|
2004-02-04 15:29:11 +01:00
|
|
|
return 0;
|
2002-03-22 21:07:43 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Global Lua functions
|
|
|
|
\*=========================================================================*/
|
2002-03-22 21:07:43 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Waits for a set of sockets until a condition is met or timeout.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-06-15 08:24:00 +02:00
|
|
|
static int global_select(lua_State *L) {
|
2005-11-20 08:20:26 +01:00
|
|
|
int rtab, wtab, itab, ret, ndirty;
|
2009-05-27 11:31:38 +02:00
|
|
|
t_socket max_fd = SOCKET_INVALID;
|
2004-06-15 08:24:00 +02:00
|
|
|
fd_set rset, wset;
|
2005-10-07 06:40:59 +02:00
|
|
|
t_timeout tm;
|
2004-07-01 05:32:09 +02:00
|
|
|
double t = luaL_optnumber(L, 3, -1);
|
2004-06-15 08:24:00 +02:00
|
|
|
FD_ZERO(&rset); FD_ZERO(&wset);
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_settop(L, 3);
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_newtable(L); itab = lua_gettop(L);
|
|
|
|
lua_newtable(L); rtab = lua_gettop(L);
|
|
|
|
lua_newtable(L); wtab = lua_gettop(L);
|
2009-05-27 11:31:38 +02:00
|
|
|
collect_fd(L, 1, itab, &rset, &max_fd);
|
|
|
|
collect_fd(L, 2, itab, &wset, &max_fd);
|
2004-06-15 08:24:00 +02:00
|
|
|
ndirty = check_dirty(L, 1, rtab, &rset);
|
2004-07-01 05:32:09 +02:00
|
|
|
t = ndirty > 0? 0.0: t;
|
2005-10-07 06:40:59 +02:00
|
|
|
timeout_init(&tm, t, -1);
|
|
|
|
timeout_markstart(&tm);
|
|
|
|
ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm);
|
2004-07-16 08:48:48 +02:00
|
|
|
if (ret > 0 || ndirty > 0) {
|
2004-06-15 08:24:00 +02:00
|
|
|
return_fd(L, &rset, max_fd+1, itab, rtab, ndirty);
|
|
|
|
return_fd(L, &wset, max_fd+1, itab, wtab, 0);
|
|
|
|
make_assoc(L, rtab);
|
|
|
|
make_assoc(L, wtab);
|
|
|
|
return 2;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
lua_pushstring(L, "timeout");
|
|
|
|
return 3;
|
|
|
|
} else {
|
2009-05-27 11:31:38 +02:00
|
|
|
luaL_error(L, "select failed");
|
2004-06-15 08:24:00 +02:00
|
|
|
return 3;
|
|
|
|
}
|
2002-03-22 21:07:43 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
2004-06-15 08:24:00 +02:00
|
|
|
* Internal functions
|
2003-06-26 20:47:49 +02:00
|
|
|
\*=========================================================================*/
|
2005-11-20 08:20:26 +01:00
|
|
|
static t_socket getfd(lua_State *L) {
|
|
|
|
t_socket fd = SOCKET_INVALID;
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushstring(L, "getfd");
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
if (!lua_isnil(L, -1)) {
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_call(L, 1, 1);
|
2012-04-11 22:21:25 +02:00
|
|
|
if (lua_isnumber(L, -1)) {
|
2015-08-21 20:39:34 +02:00
|
|
|
double numfd = lua_tonumber(L, -1);
|
2012-04-11 22:21:25 +02:00
|
|
|
fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID;
|
|
|
|
}
|
2015-08-21 20:39:34 +02:00
|
|
|
}
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return fd;
|
2002-07-03 21:06:53 +02:00
|
|
|
}
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
static int dirty(lua_State *L) {
|
|
|
|
int is = 0;
|
|
|
|
lua_pushstring(L, "dirty");
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
if (!lua_isnil(L, -1)) {
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_call(L, 1, 1);
|
|
|
|
is = lua_toboolean(L, -1);
|
2015-08-21 20:39:34 +02:00
|
|
|
}
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return is;
|
2002-03-22 21:07:43 +01:00
|
|
|
}
|
2003-06-09 20:23:40 +02:00
|
|
|
|
2015-08-21 20:39:34 +02:00
|
|
|
static void collect_fd(lua_State *L, int tab, int itab,
|
2009-05-27 11:31:38 +02:00
|
|
|
fd_set *set, t_socket *max_fd) {
|
|
|
|
int i = 1, n = 0;
|
|
|
|
/* nil is the same as an empty table */
|
|
|
|
if (lua_isnil(L, tab)) return;
|
|
|
|
/* otherwise we need it to be a table */
|
|
|
|
luaL_checktype(L, tab, LUA_TTABLE);
|
2012-12-11 20:43:49 +01:00
|
|
|
for ( ;; ) {
|
2005-11-20 08:20:26 +01:00
|
|
|
t_socket fd;
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_gettable(L, tab);
|
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-27 11:31:38 +02:00
|
|
|
/* getfd figures out if this is a socket */
|
2004-06-15 08:24:00 +02:00
|
|
|
fd = getfd(L);
|
2005-11-20 08:20:26 +01:00
|
|
|
if (fd != SOCKET_INVALID) {
|
2009-05-27 11:31:38 +02:00
|
|
|
/* make sure we don't overflow the fd_set */
|
2012-04-11 22:21:25 +02:00
|
|
|
#ifdef _WIN32
|
2015-08-21 20:39:34 +02:00
|
|
|
if (n >= FD_SETSIZE)
|
2009-05-27 11:31:38 +02:00
|
|
|
luaL_argerror(L, tab, "too many sockets");
|
2012-04-11 22:21:25 +02:00
|
|
|
#else
|
2015-08-21 20:39:34 +02:00
|
|
|
if (fd >= FD_SETSIZE)
|
2012-04-11 22:21:25 +02:00
|
|
|
luaL_argerror(L, tab, "descriptor too large for set size");
|
|
|
|
#endif
|
2004-06-15 08:24:00 +02:00
|
|
|
FD_SET(fd, set);
|
2009-05-27 11:31:38 +02:00
|
|
|
n++;
|
|
|
|
/* keep track of the largest descriptor so far */
|
2015-08-21 20:39:34 +02:00
|
|
|
if (*max_fd == SOCKET_INVALID || *max_fd < fd)
|
2009-05-27 11:31:38 +02:00
|
|
|
*max_fd = fd;
|
|
|
|
/* make sure we can map back from descriptor to the object */
|
2012-12-11 20:43:49 +01:00
|
|
|
lua_pushnumber(L, (lua_Number) fd);
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_settable(L, itab);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) {
|
|
|
|
int ndirty = 0, i = 1;
|
2015-08-21 20:39:34 +02:00
|
|
|
if (lua_isnil(L, tab))
|
2004-06-15 08:24:00 +02:00
|
|
|
return 0;
|
2015-08-21 20:39:34 +02:00
|
|
|
for ( ;; ) {
|
2005-11-20 08:20:26 +01:00
|
|
|
t_socket fd;
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_gettable(L, tab);
|
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fd = getfd(L);
|
2005-11-20 08:20:26 +01:00
|
|
|
if (fd != SOCKET_INVALID && dirty(L)) {
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushnumber(L, ++ndirty);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_settable(L, dtab);
|
|
|
|
FD_CLR(fd, set);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
return ndirty;
|
|
|
|
}
|
|
|
|
|
2015-08-21 20:39:34 +02:00
|
|
|
static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
|
2004-06-15 08:24:00 +02:00
|
|
|
int itab, int tab, int start) {
|
2005-11-20 08:20:26 +01:00
|
|
|
t_socket fd;
|
2004-06-15 08:24:00 +02:00
|
|
|
for (fd = 0; fd < max_fd; fd++) {
|
|
|
|
if (FD_ISSET(fd, set)) {
|
|
|
|
lua_pushnumber(L, ++start);
|
2012-12-11 20:43:49 +01:00
|
|
|
lua_pushnumber(L, (lua_Number) fd);
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_gettable(L, itab);
|
|
|
|
lua_settable(L, tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_assoc(lua_State *L, int tab) {
|
|
|
|
int i = 1, atab;
|
|
|
|
lua_newtable(L); atab = lua_gettop(L);
|
2012-12-11 20:43:49 +01:00
|
|
|
for ( ;; ) {
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_gettable(L, tab);
|
|
|
|
if (!lua_isnil(L, -1)) {
|
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_settable(L, atab);
|
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_settable(L, atab);
|
|
|
|
} else {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = i+1;
|
|
|
|
}
|
2003-06-26 20:47:49 +02:00
|
|
|
}
|
2004-06-15 08:24:00 +02:00
|
|
|
|