/* * scamper_fds_poll * * the money function: this function polls the file descriptors held by * scamper. for each fd with an event, it calls the callback registered * with the fd. */ int scamper_fds_poll(struct timeval *timeout) { scamper_fd_t *fdn; struct timeval tv; /* * if there are fds that can be reaped, then do so. * if there are fds left over after, use that to guide the select timeout. */ if(dlist_count(refcnt_0) > 0) { gettimeofday_wrap(&tv); while((fdn = (scamper_fd_t *)dlist_head_get(refcnt_0)) != NULL) { assert(fdn->refcnt == 0); if(timeval_cmp(&fdn->tv, &tv) > 0) break; fd_close(fdn); fd_free(fdn); } if(fdn != NULL) { timeval_diff_tv(&tv, &tv, &fdn->tv); if(timeout == NULL || timeval_cmp(&tv, timeout) < 0) timeout = &tv; } } return pollfunc(timeout); }
static void interactive_mode(void) { int libfd=-1, maxdev, ndevs; fd_set readfds; ossmix_select_poll_t pollfunc; struct timeval tmout; libfd=ossmix_get_fd(&pollfunc); ossmix_set_callback(event_callback); printf("libfd=%d, func=%p\n", libfd, pollfunc); printf("\n"); printf("> ");fflush(stdout); if (libfd >= 0) { tmout.tv_sec = 0; tmout.tv_usec = 100000; /* 0.1 sec */ } else { tmout.tv_sec = 0; tmout.tv_usec = 0; } while (1) { FD_ZERO (&readfds); maxdev = 0; FD_SET (0, &readfds); // Stdin if (libfd > 0) FD_SET (libfd, &readfds); if (libfd > maxdev) maxdev = libfd; if ((ndevs = select (maxdev + 1, &readfds, NULL, NULL, &tmout)) == -1) { perror ("select"); exit (-1); } if (ndevs == 0) { ossmix_timertick(); tmout.tv_sec = 0; tmout.tv_usec = 100000; /* 0.1 sec */ } if (FD_ISSET (0, &readfds)) /* Stdio */ { char line[128]; if (fgets(line, sizeof(line)-1, stdin) != NULL) { command_parser(line); printf("> ");fflush(stdout); } } if (libfd > 0) if (FD_ISSET (libfd, &readfds)) pollfunc(); } }