bool server_tick(server_t server, int timeout_ms) { int i; int numready; bool raft_ready = false; #ifdef USE_EPOLL struct epoll_event events[MAX_EVENTS]; numready = epoll_wait(server->epollfd, events, MAX_EVENTS, timeout_ms); if (numready < 0) { shout("failed to epoll: %s\n", strerror(errno)); return false; } for (i = 0; i < numready; i++) { stream_t stream = (stream_t)events[i].data.ptr; if (stream == NULL) { server_accept(server); } else if (stream == &server->raft_stream) { raft_ready = true; } else { if (events[i].events & EPOLLERR) { stream->good = false; } else if (events[i].events & EPOLLIN) { server_stream_handle(server, stream); } } } #else fd_set readfds = server->all; struct timeval timeout = ms2tv(timeout_ms); numready = select(server->maxfd + 1, &readfds, NULL, NULL, &timeout); if (numready == -1) { shout("failed to select: %s\n", strerror(errno)); return false; } if (FD_ISSET(server->listener, &readfds)) { numready--; server_accept(server); } if ((server->raft_stream.good) && FD_ISSET(server->raft_stream.fd, &readfds)) { numready--; raft_ready = true; } stream_t s; for (s = server_used_chain; (s != NULL) && (numready > 0); s = s->next) { if (FD_ISSET(s->fd, &readfds)) { server_stream_handle(server, s); numready--; } } #endif server_close_bad_streams(server); server_flush(server); return raft_ready; }
void server_loop(server_t server) { while (1) { int i; fd_set readfds = server->all; debug("selecting\n"); int numready = select(server->maxfd + 1, &readfds, NULL, NULL, NULL); if (numready == -1) { shout("failed to select: %s\n", strerror(errno)); return; } if (FD_ISSET(server->listener, &readfds)) { numready--; server_accept(server); } for (i = 0; (i < server->streamsnum) && (numready > 0); i++) { stream_t stream = server->streams + i; if (FD_ISSET(stream->fd, &readfds)) { server_stream_handle(server, stream); numready--; } } server_close_bad_streams(server); server_flush(server); } }