2003-03-28 22:08:50 +01:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Select implementation
|
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2003-03-28 23:14:06 +01:00
|
|
|
#include <string.h>
|
2003-06-09 20:23:40 +02:00
|
|
|
|
2002-07-03 21:06:53 +02:00
|
|
|
#include <lua.h>
|
2003-03-21 00:11:25 +01:00
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2003-03-28 22:08:50 +01:00
|
|
|
#include "luasocket.h"
|
2003-06-09 20:23:40 +02:00
|
|
|
#include "socket.h"
|
|
|
|
#include "auxiliar.h"
|
|
|
|
#include "select.h"
|
2002-07-03 21:06:53 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
static int meth_set(lua_State *L);
|
|
|
|
static int meth_isset(lua_State *L);
|
|
|
|
static int c_select(lua_State *L);
|
|
|
|
static int global_select(lua_State *L);
|
|
|
|
static void check_obj_tab(lua_State *L, int tabidx);
|
2002-07-03 21:06:53 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
/* fd_set object methods */
|
|
|
|
static luaL_reg set[] = {
|
|
|
|
{"set", meth_set},
|
|
|
|
{"isset", meth_isset},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2002-07-03 21:06:53 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
/* functions in library namespace */
|
|
|
|
static luaL_reg func[] = {
|
|
|
|
{"select", global_select},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2002-03-22 21:07:43 +01:00
|
|
|
|
2002-07-03 21:06:53 +02:00
|
|
|
void select_open(lua_State *L)
|
2002-03-22 21:07:43 +01:00
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
/* get select auxiliar lua function from lua code and register
|
|
|
|
* pass it as an upvalue to global_select */
|
|
|
|
#ifdef LUASOCKET_COMPILED
|
|
|
|
#include "select.lch"
|
2003-03-28 22:08:50 +01:00
|
|
|
#else
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_dofile(L, "select.lua");
|
2003-03-28 22:08:50 +01:00
|
|
|
#endif
|
2003-06-09 20:23:40 +02:00
|
|
|
luaL_openlib(L, LUASOCKET_LIBNAME, func, 1);
|
2003-03-28 22:08:50 +01:00
|
|
|
lua_pop(L, 1);
|
2003-06-09 20:23:40 +02:00
|
|
|
aux_newclass(L, "select{fd_set}", set);
|
2002-03-22 21:07:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Waits for a set of sockets until a condition is met or timeout.
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-09 20:23:40 +02:00
|
|
|
static int global_select(lua_State *L)
|
2002-03-22 21:07:43 +01:00
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
fd_set *read_fd_set, *write_fd_set;
|
2002-07-03 21:06:53 +02:00
|
|
|
/* make sure we have enough arguments (nil is the default) */
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_settop(L, 3);
|
|
|
|
/* check object tables */
|
|
|
|
check_obj_tab(L, 1);
|
|
|
|
check_obj_tab(L, 2);
|
|
|
|
/* check timeout */
|
|
|
|
if (!lua_isnil(L, 3) && !lua_isnumber(L, 3))
|
|
|
|
luaL_argerror(L, 3, "number or nil expected");
|
|
|
|
/* select auxiliar lua function to be called comes first */
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_insert(L, 1);
|
|
|
|
/* pass fd_set objects */
|
|
|
|
read_fd_set = lua_newuserdata(L, sizeof(fd_set));
|
|
|
|
FD_ZERO(read_fd_set);
|
|
|
|
aux_setclass(L, "select{fd_set}", -1);
|
|
|
|
write_fd_set = lua_newuserdata(L, sizeof(fd_set));
|
|
|
|
FD_ZERO(write_fd_set);
|
|
|
|
aux_setclass(L, "select{fd_set}", -1);
|
2002-07-03 21:06:53 +02:00
|
|
|
/* pass select auxiliar C function */
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_pushcfunction(L, c_select);
|
2002-07-03 21:06:53 +02:00
|
|
|
/* call select auxiliar lua function */
|
2003-06-09 20:23:40 +02:00
|
|
|
lua_call(L, 6, 3);
|
2002-03-22 21:07:43 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
static int c_select(lua_State *L)
|
2002-03-22 21:07:43 +01:00
|
|
|
{
|
2002-07-03 21:06:53 +02:00
|
|
|
int max_fd = (int) lua_tonumber(L, 1);
|
2003-06-09 20:23:40 +02:00
|
|
|
fd_set *read_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 2);
|
|
|
|
fd_set *write_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 3);
|
|
|
|
int timeout = lua_isnil(L, 4) ? -1 : (int)(lua_tonumber(L, 4) * 1000);
|
2002-07-03 21:06:53 +02:00
|
|
|
struct timeval tv;
|
2003-06-09 20:23:40 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
lua_pushnumber(L, select(max_fd, read_fd_set, write_fd_set, NULL,
|
|
|
|
timeout < 0 ? NULL : &tv));
|
2002-07-03 21:06:53 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
static int meth_set(lua_State *L)
|
2002-07-03 21:06:53 +02:00
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
fd_set *set = (fd_set *) aux_checkclass(L, "select{fd_set}", 1);
|
|
|
|
t_sock fd = (t_sock) lua_tonumber(L, 2);
|
2002-07-03 21:06:53 +02:00
|
|
|
if (fd >= 0) FD_SET(fd, set);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
static int meth_isset(lua_State *L)
|
2002-07-03 21:06:53 +02:00
|
|
|
{
|
2003-06-09 20:23:40 +02:00
|
|
|
fd_set *set = (fd_set *) aux_checkclass(L, "select{fd_set}", 1);
|
|
|
|
t_sock fd = (t_sock) lua_tonumber(L, 2);
|
2002-07-03 21:06:53 +02:00
|
|
|
if (fd >= 0 && FD_ISSET(fd, set)) lua_pushnumber(L, 1);
|
|
|
|
else lua_pushnil(L);
|
|
|
|
return 1;
|
2002-03-22 21:07:43 +01:00
|
|
|
}
|
2003-06-09 20:23:40 +02:00
|
|
|
|
|
|
|
static void check_obj_tab(lua_State *L, int tabidx)
|
|
|
|
{
|
|
|
|
if (tabidx < 0) tabidx = lua_gettop(L) + tabidx + 1;
|
|
|
|
if (lua_istable(L, tabidx)) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, tabidx) != 0) {
|
|
|
|
if (aux_getgroupudata(L, "select{able}", -1) == NULL) {
|
|
|
|
char msg[45];
|
|
|
|
if (lua_isnumber(L, -2))
|
|
|
|
sprintf(msg, "table entry #%g is invalid",
|
|
|
|
lua_tonumber(L, -2));
|
|
|
|
else
|
|
|
|
sprintf(msg, "invalid entry found in table");
|
|
|
|
luaL_argerror(L, tabidx, msg);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
} else if (!lua_isnil(L, tabidx))
|
|
|
|
luaL_argerror(L, tabidx, "table or nil expected");
|
|
|
|
}
|