int wait_for_packet(void* ptr_api, int data_socket) { GTH_api* api = (GTH_api*)ptr_api; fd_set fds; int result; int nfds = max_arg(api->fd, data_socket) + 1; FD_ZERO(&fds); for (;;) { struct timeval tv = { 1, 0 }; FD_SET(api->fd, &fds); FD_SET(data_socket, &fds); result = select(nfds, &fds, 0, 0, &tv); if (result < 0) { return -1; //failed } if (result == 0) { return 0; } if (FD_ISSET(api->fd, &fds)) { gth_nop(api); } if (FD_ISSET(data_socket, &fds)) { return 1; } } }
// Block (or loop) until a packet arrives. Flush API events with <nop/> // Returns 0 on timeout, 1 otherwise. static int wait_for_packet(GTH_api *api, int data_socket) { fd_set fds; int result; int nfds = max_arg(api->fd, data_socket) + 1; FD_ZERO(&fds); for (;;) { struct timeval tv = {1, 0}; FD_SET(api->fd, &fds); FD_SET(data_socket, &fds); result = select(nfds, &fds, 0, 0, &tv); if (result < 0) { die("internal error---select() returned error"); } if (result == 0) { return 0; } if (FD_ISSET(api->fd, &fds)) { gth_nop(api); } if (FD_ISSET(data_socket, &fds)) { return 1; } } }