void close_and_free_server(EV_P_ struct server *server) { if (server != NULL) { if (server->query != NULL) { asyncns_cancel(server->listen_ctx->asyncns, server->query); server->query = NULL; } ev_io_stop(EV_A_ &server->send_ctx->io); ev_io_stop(EV_A_ &server->recv_ctx->io); ev_timer_stop(EV_A_ &server->recv_ctx->watcher); close(server->fd); free_server(server); if (verbose) { server_conn--; LOGD("current server connection: %d", server_conn); } } }
int main(int argc, char **argv) { asyncns_t *asyncns; asyncns_query_t *query; struct addrinfo *result; struct pollfd pollfd = { .events = POLLIN }; int status; asyncns = asyncns_new(10); assert(asyncns); assert(asyncns_getnqueries(asyncns) == 0); assert(asyncns_getnext(asyncns) == NULL); pollfd.fd = asyncns_fd(asyncns); assert(pollfd.fd > 2); query = asyncns_getaddrinfo(asyncns, "127.0.0.1", NULL, NULL); assert(query); assert(asyncns_getnqueries(asyncns) == 1); assert(asyncns_getnext(asyncns) == NULL); asyncns_cancel(asyncns, query); query = NULL; assert(asyncns_getnqueries(asyncns) == 0); assert(asyncns_getnext(asyncns) == NULL); query = asyncns_getaddrinfo(asyncns, "127.0.0.1", NULL, NULL); assert(query); assert(asyncns_getnqueries(asyncns) == 1); assert(asyncns_getnext(asyncns) == NULL); usleep(100000); status = poll(&pollfd, 1, 0); assert(status == 1); status = asyncns_wait(asyncns, 0); assert(status == 0); assert(asyncns_isdone(asyncns, query)); assert(asyncns_getnqueries(asyncns) == 1); assert(asyncns_getnext(asyncns) == query); status = poll(&pollfd, 1, 100); assert(status == 0); status = asyncns_getaddrinfo_done(asyncns, query, &result); assert(asyncns_getnqueries(asyncns) == 0); /* Intuitively, this should not be needed but the docs state that * a call to `asyncns_wait()` is necessary so that `asyncns_getnext()` * provides meaningful results. */ status = asyncns_wait(asyncns, 0); assert(status == 0); /* There were two queries issued, one of which has been cancelled * and the other has been freed afterwards. As none of them can be * returned, the only meaningful result of `asyncns_getnext()` is * NULL. */ assert(asyncns_getnext(asyncns) == NULL); asyncns_free(asyncns); asyncns_freeaddrinfo(result); return EXIT_SUCCESS; }