Using libev ev_io and ev_idle events together -
i interested in listening incoming connections , when there no active connections want background processing. have not found examples of both these together.
i want similar this:
from 1 of examples:
//ev_io callback static void server_cb(ev_p_ ev_io *w, int revents) { //accept new client connection //read client socket //stop event loop ev_io_stop(ev_a_ &client->io); //send client //start event loop again ev_io_start(ev_a_ &client->io); } main() { // sure aren't blocking ev_periodic_init(&every_few_seconds, not_blocked, 0, 5, 0); ev_periodic_start(ev_a_ &every_few_seconds); // notified whenever socket ready read ev_io_init(&server.io, server_cb, server.fd, ev_read); ev_io_start(ev_a_ &server.io); } now in example should add idle event , , when should start , stop idle event not interfere in main event handler , should invoked when idle.
thanks.
well, new libev, put simple demo of server recieves data on socket while processing timeouts , idle events.
#include <ev.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> static struct ev_loop *loop; static ev_timer timeout_watcher; static ev_io in_watcher; static ev_idle idle_watcher; static int sock_fd; // socket input watcher static void in_cb(ev_p_ ev_io *watcher, int revents) { int r, t; char buf[1024]; (t = 0; (r = read(sock_fd, buf, sizeof(buf))) > 0;) { t += r; write(stdout_fileno, buf, r); // copy input stdout if (buf[r-1] == '\n') break; // operate line-at-a-time } fprintf(stderr, "in: count = %d\n", t); if (r == 0) { fputs("in: connection closed\n", stderr); ev_io_stop(loop, &in_watcher); // stop socket watcher ev_break(loop, evbreak_all); // exit loop } else if (r < 0) { perror("read"); } } static void timeout_cb(ev_p_ ev_timer *watcher, int revents) { fprintf(stderr, "timeout: = %f\n", ev_now(loop)); } static void idle_cb(ev_p_ ev_idle *watcher, int revents) { static long idle_count = 0; fprintf(stderr, "idle: count = %ld\n", ++idle_count); sleep(1); // simulate doing stuff } int main() { extern int errno; int master_fd; int sock_opt = 1; int conn_port = 7000; struct sockaddr_in addr; socklen_t addrlen; // **** following needed set socket receive data **** master_fd = socket(af_inet, sock_stream, 0); if (master_fd == -1) { perror("socket"); return errno; } if (setsockopt(master_fd, sol_socket, so_reuseaddr, (char *) &sock_opt, sizeof(sock_opt)) == -1) { perror("setsockopt"); return errno; } addr.sin_family = af_inet; addr.sin_addr.s_addr = inaddr_any; addr.sin_port = htons(conn_port); addrlen = sizeof(addr); if (bind(master_fd, (struct sockaddr *) &addr, addrlen) != 0) { perror("bind"); return errno; } if (listen(master_fd, 3) != 0) { perror("listen"); return errno; } fprintf(stderr, "awaiting connection on port %d\n", conn_port); sock_fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen); if (sock_fd == -1) { perror("accept"); return errno; } fputs("in: connection established\n", stderr); // **** end of socket setup code **** // define loop loop = ev_default_loop(0); // define repeating timer ev_timer_init (&timeout_watcher, timeout_cb, 5.0, 5.0); ev_timer_start (loop, &timeout_watcher); // define idle process ev_idle_init(&idle_watcher, idle_cb); ev_idle_start (loop, &idle_watcher); // define socket data receiver ev_io_init(&in_watcher, in_cb, sock_fd, ev_read); ev_io_start (loop, &in_watcher); // run loop ev_run(loop, 0); // clean close(sock_fd); close(master_fd); return 0; }
Comments
Post a Comment