int main() { fdselect_t sel; sock_t pipe[2]; int i, n; tid_t tid; debug_init(DEBUG_INFO, 0, 0); sock_init(); fdselect_init(&sel); for(i=0; i<CHILD_FORK_MAX; i++) { pipe_child_arg_t *a; n = sock_pair(pipe); assertb_sockerr(n>=0); n = sock_nonblock(pipe[0], 1); assertb_sockerr(n>=0); a = calloc(1, sizeof(*a)); a->sock = pipe[1]; a->id = i; tid = thread_start(pipe_child, a); fdselect_set(&sel, pipe[0], FDSELECT_READ, pipe_select_read, (void*)tid); } while(1) { i = fdselect_count(&sel); printf("fdselect_select: count=%d\n", i); if( i<=0 ) break; fdselect_select(&sel, 1000); } return 0; }
/* * Arguments: sd_udata, [type ("stream", "dgram"), domain ("inet", "inet6", "unix"), * sd_udata (socketpair)] * Returns: [sd_udata] */ static int sock_socket (lua_State *L) { sd_t *sdp = checkudata(L, 1, SD_TYPENAME); const char *typep = lua_tostring(L, 2); const char *domainp = lua_tostring(L, 3); int type = SOCK_STREAM, domain = AF_INET; sd_t sd, sv[2]; sd_t *pair_sdp = (lua_gettop(L) > 1 && lua_isuserdata(L, -1)) ? checkudata(L, -1, SD_TYPENAME) : NULL; if (typep && typep[0] == 'd') type = SOCK_DGRAM; if (domainp) { if (domainp[0] == 'u') domain = AF_UNIX; else if (domainp[0] == 'i' && domainp[1] == 'n' && domainp[2] == 'e' && domainp[3] == 't' && domainp[4] == '6') domain = AF_INET6; } #ifndef _WIN32 sd = (pair_sdp) ? socketpair(AF_UNIX, type, 0, sv) : socket(domain, type, 0); #else sd = (pair_sdp) ? sock_pair(type, sv) : WSASocket(domain, type, 0, NULL, 0, IS_OVERLAPPED); #endif if (sd != -1) { if (pair_sdp) { *sdp = sv[0]; *pair_sdp = sv[1]; } else *sdp = sd; lua_settop(L, 1); return 1; } return sys_seterror(L, 0); }