=========================================================================*\ * Global Lua functions \*=========================================================================*/ -------------------------------------------------------------------------*\ * Waits for a set of sockets until a condition is met or timeout. \*-------------------------------------------------------------------------*/
| 50 | * Waits for a set of sockets until a condition is met or timeout. |
| 51 | \*-------------------------------------------------------------------------*/ |
| 52 | static int global_select(lua_State *L) { |
| 53 | int rtab, wtab, itab, ret, ndirty; |
| 54 | t_socket max_fd = SOCKET_INVALID; |
| 55 | fd_set rset, wset; |
| 56 | t_timeout tm; |
| 57 | double t = luaL_optnumber(L, 3, -1); |
| 58 | FD_ZERO(&rset); FD_ZERO(&wset); |
| 59 | lua_settop(L, 3); |
| 60 | lua_newtable(L); itab = lua_gettop(L); |
| 61 | lua_newtable(L); rtab = lua_gettop(L); |
| 62 | lua_newtable(L); wtab = lua_gettop(L); |
| 63 | collect_fd(L, 1, itab, &rset, &max_fd); |
| 64 | collect_fd(L, 2, itab, &wset, &max_fd); |
| 65 | ndirty = check_dirty(L, 1, rtab, &rset); |
| 66 | t = ndirty > 0? 0.0: t; |
| 67 | timeout_init(&tm, t, -1); |
| 68 | timeout_markstart(&tm); |
| 69 | ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm); |
| 70 | if (ret > 0 || ndirty > 0) { |
| 71 | return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); |
| 72 | return_fd(L, &wset, max_fd+1, itab, wtab, 0); |
| 73 | make_assoc(L, rtab); |
| 74 | make_assoc(L, wtab); |
| 75 | return 2; |
| 76 | } else if (ret == 0) { |
| 77 | lua_pushstring(L, "timeout"); |
| 78 | return 3; |
| 79 | } else { |
| 80 | luaL_error(L, "select failed"); |
| 81 | return 3; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /*=========================================================================*\ |
| 86 | * Internal functions |
nothing calls this directly
no test coverage detected