/*-------------------------------------------------------------------------*\ * 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; } }
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 } }
int um3_transport_init(char *argv[]) { udp_addr_t addr; int asp1_ep, sgp1_ep; /* create transport layer endpoints */ um3_get_asp1_addr(argv, &addr); asp1_ep = make_lep(addr); um3_get_sgp1_addr(argv, &addr); sgp1_ep = make_rep(addr); um3_get_cn_addr(argv, &addr); sgp1_ep = make_rep(addr); /* create associations between the transport layer endpoints */ make_assoc(asp1_ep, sgp1_ep, 0); return 0; }