luasocket/src/select.c

150 lines
5.1 KiB
C
Raw Normal View History

2003-03-28 22:08:50 +01:00
/*=========================================================================*\
* Select implementation
* LuaSocket toolkit
*
2003-03-28 22:08:50 +01:00
* RCS ID: $Id$
\*=========================================================================*/
2003-03-28 23:14:06 +01:00
#include <string.h>
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"
#include "socket.h"
#include "auxiliar.h"
#include "select.h"
2002-07-03 21:06:53 +02:00
/*=========================================================================*\
* Internal function prototypes.
\*=========================================================================*/
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
/* fd_set object methods */
static luaL_reg set[] = {
{"set", meth_set},
{"isset", meth_isset},
{NULL, NULL}
};
2002-07-03 21:06:53 +02:00
/* functions in library namespace */
static luaL_reg func[] = {
{"select", global_select},
{NULL, NULL}
};
2002-03-22 21:07:43 +01:00
/*=========================================================================*\
* Internal function prototypes.
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
2002-07-03 21:06:53 +02:00
void select_open(lua_State *L)
2002-03-22 21:07:43 +01: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
lua_dofile(L, "select.lua");
2003-03-28 22:08:50 +01:00
#endif
luaL_openlib(L, LUASOCKET_LIBNAME, func, 1);
2003-03-28 22:08:50 +01:00
lua_pop(L, 1);
aux_newclass(L, "select{fd_set}", set);
2002-03-22 21:07:43 +01: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.
\*-------------------------------------------------------------------------*/
static int global_select(lua_State *L)
2002-03-22 21:07:43 +01: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) */
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 = (fd_set *) lua_newuserdata(L, sizeof(fd_set));
FD_ZERO(read_fd_set);
aux_setclass(L, "select{fd_set}", -1);
write_fd_set = (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 */
lua_pushcfunction(L, c_select);
2002-07-03 21:06:53 +02:00
/* call select auxiliar lua function */
lua_call(L, 6, 3);
2002-03-22 21:07:43 +01:00
return 3;
}
/*=========================================================================*\
* Lua methods
\*=========================================================================*/
static int meth_set(lua_State *L)
2002-07-03 21:06:53 +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;
}
static int meth_isset(lua_State *L)
2002-07-03 21:06:53 +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
}
/*=========================================================================*\
* Internal functions
\*=========================================================================*/
static int c_select(lua_State *L)
{
int max_fd = (int) lua_tonumber(L, 1);
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);
struct timeval tv;
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));
return 1;
}
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");
}