diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-26 18:47:49 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-26 18:47:49 +0000 |
| commit | 71f6bb60bf2b7457091c7106190f92ab7e51f7c6 (patch) | |
| tree | 8ad3668667bd3da3c34f7ff7ae0a9a7a4daa4679 /src/select.c | |
| parent | f330540576031528f0daac231c61d4dd06e8ba1e (diff) | |
| download | luasocket-71f6bb60bf2b7457091c7106190f92ab7e51f7c6.tar.gz luasocket-71f6bb60bf2b7457091c7106190f92ab7e51f7c6.tar.bz2 luasocket-71f6bb60bf2b7457091c7106190f92ab7e51f7c6.zip | |
Finished implementation of LuaSocket 2.0 alpha on Linux.
Some testing still needed.
Diffstat (limited to 'src/select.c')
| -rw-r--r-- | src/select.c | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/src/select.c b/src/select.c index 3cabbd1..9769667 100644 --- a/src/select.c +++ b/src/select.c | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
| 2 | * Select implementation | 2 | * Select implementation |
| 3 | * LuaSocket toolkit | ||
| 4 | * | ||
| 3 | * RCS ID: $Id$ | 5 | * RCS ID: $Id$ |
| 4 | \*=========================================================================*/ | 6 | \*=========================================================================*/ |
| 5 | #include <string.h> | 7 | #include <string.h> |
| @@ -12,6 +14,9 @@ | |||
| 12 | #include "auxiliar.h" | 14 | #include "auxiliar.h" |
| 13 | #include "select.h" | 15 | #include "select.h" |
| 14 | 16 | ||
| 17 | /*=========================================================================*\ | ||
| 18 | * Internal function prototypes. | ||
| 19 | \*=========================================================================*/ | ||
| 15 | static int meth_set(lua_State *L); | 20 | static int meth_set(lua_State *L); |
| 16 | static int meth_isset(lua_State *L); | 21 | static int meth_isset(lua_State *L); |
| 17 | static int c_select(lua_State *L); | 22 | static int c_select(lua_State *L); |
| @@ -31,6 +36,12 @@ static luaL_reg func[] = { | |||
| 31 | {NULL, NULL} | 36 | {NULL, NULL} |
| 32 | }; | 37 | }; |
| 33 | 38 | ||
| 39 | /*=========================================================================*\ | ||
| 40 | * Internal function prototypes. | ||
| 41 | \*=========================================================================*/ | ||
| 42 | /*-------------------------------------------------------------------------*\ | ||
| 43 | * Initializes module | ||
| 44 | \*-------------------------------------------------------------------------*/ | ||
| 34 | void select_open(lua_State *L) | 45 | void select_open(lua_State *L) |
| 35 | { | 46 | { |
| 36 | /* get select auxiliar lua function from lua code and register | 47 | /* get select auxiliar lua function from lua code and register |
| @@ -45,6 +56,9 @@ void select_open(lua_State *L) | |||
| 45 | aux_newclass(L, "select{fd_set}", set); | 56 | aux_newclass(L, "select{fd_set}", set); |
| 46 | } | 57 | } |
| 47 | 58 | ||
| 59 | /*=========================================================================*\ | ||
| 60 | * Global Lua functions | ||
| 61 | \*=========================================================================*/ | ||
| 48 | /*-------------------------------------------------------------------------*\ | 62 | /*-------------------------------------------------------------------------*\ |
| 49 | * Waits for a set of sockets until a condition is met or timeout. | 63 | * Waits for a set of sockets until a condition is met or timeout. |
| 50 | \*-------------------------------------------------------------------------*/ | 64 | \*-------------------------------------------------------------------------*/ |
| @@ -63,10 +77,10 @@ static int global_select(lua_State *L) | |||
| 63 | lua_pushvalue(L, lua_upvalueindex(1)); | 77 | lua_pushvalue(L, lua_upvalueindex(1)); |
| 64 | lua_insert(L, 1); | 78 | lua_insert(L, 1); |
| 65 | /* pass fd_set objects */ | 79 | /* pass fd_set objects */ |
| 66 | read_fd_set = lua_newuserdata(L, sizeof(fd_set)); | 80 | read_fd_set = (fd_set *) lua_newuserdata(L, sizeof(fd_set)); |
| 67 | FD_ZERO(read_fd_set); | 81 | FD_ZERO(read_fd_set); |
| 68 | aux_setclass(L, "select{fd_set}", -1); | 82 | aux_setclass(L, "select{fd_set}", -1); |
| 69 | write_fd_set = lua_newuserdata(L, sizeof(fd_set)); | 83 | write_fd_set = (fd_set *) lua_newuserdata(L, sizeof(fd_set)); |
| 70 | FD_ZERO(write_fd_set); | 84 | FD_ZERO(write_fd_set); |
| 71 | aux_setclass(L, "select{fd_set}", -1); | 85 | aux_setclass(L, "select{fd_set}", -1); |
| 72 | /* pass select auxiliar C function */ | 86 | /* pass select auxiliar C function */ |
| @@ -76,20 +90,9 @@ static int global_select(lua_State *L) | |||
| 76 | return 3; | 90 | return 3; |
| 77 | } | 91 | } |
| 78 | 92 | ||
| 79 | static int c_select(lua_State *L) | 93 | /*=========================================================================*\ |
| 80 | { | 94 | * Lua methods |
| 81 | int max_fd = (int) lua_tonumber(L, 1); | 95 | \*=========================================================================*/ |
| 82 | fd_set *read_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 2); | ||
| 83 | fd_set *write_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 3); | ||
| 84 | int timeout = lua_isnil(L, 4) ? -1 : (int)(lua_tonumber(L, 4) * 1000); | ||
| 85 | struct timeval tv; | ||
| 86 | tv.tv_sec = timeout / 1000; | ||
| 87 | tv.tv_usec = (timeout % 1000) * 1000; | ||
| 88 | lua_pushnumber(L, select(max_fd, read_fd_set, write_fd_set, NULL, | ||
| 89 | timeout < 0 ? NULL : &tv)); | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | static int meth_set(lua_State *L) | 96 | static int meth_set(lua_State *L) |
| 94 | { | 97 | { |
| 95 | fd_set *set = (fd_set *) aux_checkclass(L, "select{fd_set}", 1); | 98 | fd_set *set = (fd_set *) aux_checkclass(L, "select{fd_set}", 1); |
| @@ -107,6 +110,23 @@ static int meth_isset(lua_State *L) | |||
| 107 | return 1; | 110 | return 1; |
| 108 | } | 111 | } |
| 109 | 112 | ||
| 113 | /*=========================================================================*\ | ||
| 114 | * Internal functions | ||
| 115 | \*=========================================================================*/ | ||
| 116 | static int c_select(lua_State *L) | ||
| 117 | { | ||
| 118 | int max_fd = (int) lua_tonumber(L, 1); | ||
| 119 | fd_set *read_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 2); | ||
| 120 | fd_set *write_fd_set = (fd_set *) aux_checkclass(L, "select{fd_set}", 3); | ||
| 121 | int timeout = lua_isnil(L, 4) ? -1 : (int)(lua_tonumber(L, 4) * 1000); | ||
| 122 | struct timeval tv; | ||
| 123 | tv.tv_sec = timeout / 1000; | ||
| 124 | tv.tv_usec = (timeout % 1000) * 1000; | ||
| 125 | lua_pushnumber(L, select(max_fd, read_fd_set, write_fd_set, NULL, | ||
| 126 | timeout < 0 ? NULL : &tv)); | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 110 | static void check_obj_tab(lua_State *L, int tabidx) | 130 | static void check_obj_tab(lua_State *L, int tabidx) |
| 111 | { | 131 | { |
| 112 | if (tabidx < 0) tabidx = lua_gettop(L) + tabidx + 1; | 132 | if (tabidx < 0) tabidx = lua_gettop(L) + tabidx + 1; |
