X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=net.c;h=8fe59802e318cbe7a9ee147604d6ddbb96bdbda0;hp=a5d5b50ef50e107ea676b2428d50332a064bb813;hb=6d9303c441fe1d8b473d24f40421d70058e2d2e5;hpb=64999b98e2c98ab92d60ac625fc3ea3a1ec630ec;ds=sidebyside diff --git a/net.c b/net.c index a5d5b50..8fe5980 100644 --- a/net.c +++ b/net.c @@ -79,3 +79,24 @@ err_socket: freeaddrinfo(addrs); return EAI_SYSTEM; } + +/********************** SERVER SOCKET STUFF **********************/ +HEADER typedef void (*jh_ev_io_ssock_cb)(EV_P_ ev_io *w, int socket); +static void ev_io_ssock_cb(EV_P_ ev_io *w, int revents) { + jh_ev_io_ssock_cb cb = (jh_ev_io_ssock_cb)w->data; + if (revents == EV_ERROR) { + cb(EV_A_ w, -1); + return; + } + int fd = accept(w->fd, NULL, NULL); + if (fd == -1) { + fprintf(stderr, "\nLIBJH ERROR: ACCEPT() FAILED - SLEEPING 1s\n"); + sleep(1); + return; + } + cb(EV_A_ w, fd); +} +PUBLIC_FN void jh_ev_io_ssock_init(ev_io *w, jh_ev_io_ssock_cb cb, int fd) { + ev_io_init(w, ev_io_ssock_cb, fd, EV_READ); + w->data = cb; +}