Remove "wants_reads" field from struct io_fd.
[srvx.git] / src / ioset.c
index f79c23e53edc74be2575ff604a97f5fefefda3d9..6013232653b3839f3909361c97787651e2c32dbf 100644 (file)
@@ -93,7 +93,8 @@ extern struct io_engine io_engine_select;
 void
 ioset_init(void)
 {
-    assert(engine == NULL);
+    if (engine) /* someone beat us to it */
+        return;
 
 #if WITH_IOSET_KQUEUE
     if (!engine && io_engine_kqueue.init())
@@ -133,15 +134,17 @@ ioset_add(int fd) {
         log_module(MAIN_LOG, LOG_ERROR, "Somebody called ioset_add(%d) on a negative fd!", fd);
         return 0;
     }
+    if (!engine)
+        ioset_init();
     res = calloc(1, sizeof(*res));
     if (!res)
         return 0;
     res->fd = fd;
     ioq_init(&res->send, 1024);
     ioq_init(&res->recv, 1024);
-    engine->add(res);
     flags = fcntl(fd, F_GETFL);
     fcntl(fd, F_SETFL, flags|O_NONBLOCK);
+    engine->add(res);
     return res;
 }
 
@@ -194,9 +197,12 @@ struct io_fd *ioset_listen(struct sockaddr *local, unsigned int sa_size, void *d
 
 struct io_fd *
 ioset_connect(struct sockaddr *local, unsigned int sa_size, const char *peer, unsigned int port, int blocking, void *data, void (*connect_cb)(struct io_fd *fd, int error)) {
-    int fd, res;
+    struct addrinfo hints;
+    struct addrinfo *ai;
     struct io_fd *io_fd;
-    struct addrinfo hints, *ai;
+    struct io_fd *old_active;
+    int res;
+    int fd;
     char portnum[10];
 
     memset(&hints, 0, sizeof(hints));
@@ -256,12 +262,20 @@ ioset_connect(struct sockaddr *local, unsigned int sa_size, const char *peer, un
         }
     }
     io_fd->state = IO_CONNECTED;
+    old_active = active_fd;
     if (connect_cb)
         connect_cb(io_fd, ((res < 0) ? errno : 0));
-    engine->update(io_fd);
+    if (active_fd)
+        engine->update(io_fd);
+    if (old_active != io_fd)
+        active_fd = old_active;
     return io_fd;
 }
 
+void ioset_update(struct io_fd *fd) {
+    engine->update(fd);
+}
+
 static void
 ioset_try_write(struct io_fd *fd) {
     int res;
@@ -311,7 +325,7 @@ ioset_close(struct io_fd *fdp, int os_close) {
 static void
 ioset_accept(struct io_fd *listener)
 {
-    struct io_fd *old_active_fd;
+    struct io_fd *old_active;
     struct io_fd *new_fd;
     int fd;
 
@@ -323,7 +337,7 @@ ioset_accept(struct io_fd *listener)
 
     new_fd = ioset_add(fd);
     new_fd->state = IO_CONNECTED;
-    old_active_fd = active_fd;
+    old_active = active_fd;
     active_fd = new_fd;
     listener->accept_cb(listener, new_fd);
     assert(active_fd == NULL || active_fd == new_fd);
@@ -333,7 +347,7 @@ ioset_accept(struct io_fd *listener)
         else
             engine->update(new_fd);
     }
-    active_fd = old_active_fd;
+    active_fd = old_active;
 }
 
 static int
@@ -392,16 +406,21 @@ ioset_buffered_read(struct io_fd *fd) {
         if (fd->recv.put == fd->recv.size)
             fd->recv.put = 0;
         fdnum = fd->fd;
-        while (fd->wants_reads && (fd->line_len > 0)) {
+        while (fd->line_len > 0) {
             struct io_fd *old_active;
+            int died = 0;
 
             old_active = active_fd;
             active_fd = fd;
             fd->readable_cb(fd);
             if (active_fd)
                 ioset_find_line_length(fd);
+            else
+                died = 1;
             if (old_active != fd)
                 active_fd = old_active;
+            if (died)
+                break;
         }
     }
 }
@@ -449,18 +468,23 @@ ioset_events(struct io_fd *fd, int readable, int writable)
         break;
     case IO_CONNECTING:
         assert(active_fd == NULL || active_fd == fd);
-        if (active_fd && writable) {
+        if (active_fd && readable) {
             socklen_t arglen;
             int rc;
             arglen = sizeof(rc);
             if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &rc, &arglen) < 0)
                 rc = errno;
-            fd->state = IO_CONNECTED;
+            fd->state = IO_CLOSED;
             if (fd->connect_cb)
                 fd->connect_cb(fd, rc);
-            if (active_fd == fd)
-                engine->update(fd);
+        } else if (active_fd && writable) {
+            fd->state = IO_CONNECTED;
+            if (fd->connect_cb)
+                fd->connect_cb(fd, 0);
         }
+        if (active_fd != fd)
+            break;
+        engine->update(fd);
         /* and fall through */
     case IO_CONNECTED:
         assert(active_fd == NULL || active_fd == fd);