Exemple #1
0
/*-------------------------------------------------------------------------*\
* Waits for a set of sockets until a condition is met or timeout.
\*-------------------------------------------------------------------------*/
static int global_select(lua_State *L) {
    int rtab, wtab, itab, ret, ndirty;
    t_socket max_fd;
    fd_set rset, wset;
    t_timeout tm;
    double t = luaL_optnumber(L, 3, -1);
    FD_ZERO(&rset); FD_ZERO(&wset);
    lua_settop(L, 3);
    lua_newtable(L); itab = lua_gettop(L);
    lua_newtable(L); rtab = lua_gettop(L);
    lua_newtable(L); wtab = lua_gettop(L);
    max_fd = collect_fd(L, 1, SOCKET_INVALID, itab, &rset);
    ndirty = check_dirty(L, 1, rtab, &rset);
    t = ndirty > 0? 0.0: t;
    timeout_init(&tm, t, -1);
    timeout_markstart(&tm);
    max_fd = collect_fd(L, 2, max_fd, itab, &wset);
    ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm);
    if (ret > 0 || ndirty > 0) {
        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 {
        lua_pushstring(L, "error");
        return 3;
    }
}
Exemple #2
0
int
close(Process *p, FileDescriptor fd) {
  FileHandle *handle = p->fd_table[fd];
  p->fd_table[fd] = NULL;
  return_fd(p, fd);
  delete_handle(handle);
  return 0;
}
Exemple #3
0
static int global_select(lua_State *L, const sigset_t* mask, int sigreceived) {
    int rtab, wtab, etab, itab, ret, ndirty;
    t_socket max_fd;
    fd_set rset, wset, eset;
    t_timeout tm;
    double t = luaL_optnumber(L, 4, -1);
    FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset);
    lua_settop(L, 4);
    lua_newtable(L); itab = lua_gettop(L);
    lua_newtable(L); rtab = lua_gettop(L);
    lua_newtable(L); wtab = lua_gettop(L);
    lua_newtable(L); etab = lua_gettop(L);
    max_fd = collect_fd(L, 1, SOCKET_INVALID, itab, &rset);
    ndirty = check_dirty(L, 1, rtab, &rset);
    t = ndirty > 0? 0.0: t;
    timeout_init(&tm, t, -1);
    timeout_markstart(&tm);
    max_fd = collect_fd(L, 2, max_fd, itab, &wset);
    max_fd = collect_fd(L, 3, max_fd, itab, &eset);
    //printf("+enter select\n");
    if (sigreceived) {
        ret = -1;
    } else {
        ret = socket_select(max_fd+1, &rset, &wset, &eset, &tm, mask);
    }
    //printf("+exit select\n");
    if (ret > 0 || ndirty > 0) {
        return_fd(L, &rset, max_fd+1, itab, rtab, ndirty);
        return_fd(L, &wset, max_fd+1, itab, wtab, 0);
        return_fd(L, &eset, max_fd+1, itab, etab, 0);
        make_assoc(L, rtab);
        make_assoc(L, wtab);
        make_assoc(L, etab);
        return 3; //3 values pushed: 3 result tables
    } else if (ret == 0) {
        lua_pushstring(L, "timeout");
        return 4; //4 values pushed: 3 result tables + timeout msg
    } else {
        lua_pushstring(L, strerror(errno));
        return 4; //4 values pushed: 3 result tables + errno msg
    }
}