int main (int, char *[]) { // Instantiate a server which will receive messages for DURATION // seconds. Server_Events server_events (UDP_PORT, MCAST_ADDR, DURATION); // Instance of the ACE_Reactor. ACE_Reactor reactor; if (reactor.register_handler (&server_events, ACE_Event_Handler::READ_MASK) == -1) ACE_ERROR ((LM_ERROR, "%p\n%a", "register_handler", 1)); ACE_DEBUG ((LM_DEBUG, "starting up server\n")); for (;;) reactor.handle_events (server_events.wait_time ()); ACE_NOTREACHED (return 0;) }
static void run_server(struct thread *t) { struct options *opts = t->opts; struct callbacks *cb = t->cb; struct addrinfo *ai = t->ai; struct epoll_event *events; int fd_listen, epfd; char *buf; fd_listen = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd_listen == -1) PLOG_FATAL(cb, "socket"); set_reuseport(fd_listen, cb); set_reuseaddr(fd_listen, 1, cb); if (bind(fd_listen, ai->ai_addr, ai->ai_addrlen)) PLOG_FATAL(cb, "bind"); if (opts->min_rto) set_min_rto(fd_listen, opts->min_rto, cb); if (listen(fd_listen, opts->listen_backlog)) PLOG_FATAL(cb, "listen"); epfd = epoll_create1(0); if (epfd == -1) PLOG_FATAL(cb, "epoll_create1"); epoll_add_or_die(epfd, t->stop_efd, EPOLLIN, cb); epoll_add_or_die(epfd, fd_listen, EPOLLIN, cb); events = calloc(opts->maxevents, sizeof(struct epoll_event)); buf = buf_alloc(opts); pthread_barrier_wait(t->ready); while (!t->stop) { int ms = opts->nonblocking ? 10 /* milliseconds */ : -1; int nfds = epoll_wait(epfd, events, opts->maxevents, ms); if (nfds == -1) { if (errno == EINTR) continue; PLOG_FATAL(cb, "epoll_wait"); } server_events(t, epfd, events, nfds, fd_listen, buf); } free(buf); free(events); do_close(epfd); }