more network stuff
[libjh.git] / net.c
diff --git a/net.c b/net.c
index a5d5b50..8fe5980 100644 (file)
--- 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;
+}