ioset event handling fixups
authorMichael Poole <mdpoole@troilus.org>
Sat, 23 Sep 2006 01:14:35 +0000 (01:14 +0000)
committerMichael Poole <mdpoole@troilus.org>
Sat, 23 Sep 2006 01:14:35 +0000 (01:14 +0000)
src/ioset.c (ioset_connect): Properly handle the situation if the connect
    handler closes the fd.
  (ioset_accept): Rename "old_active_fd" to "old_active" for consistency.
  (ioset_buffered_read): Make sure we bail if the fd is closed.
  (ioset_events): Handle 'readable' on a connecting fd as an error.
git-archimport-id: srvx@srvx.net--2006/srvx--devo--1.3--patch-49

ChangeLog
src/ioset.c

index 9c732adc6934458b9e51e7ebcfb8b52b4c507681..47d99ffd517059f86fcd427625c5a915879e46d7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,23 @@
 # arch-tag: automatic-ChangeLog--srvx@srvx.net--2006/srvx--devo--1.3
 #
 
+2006-09-23 01:14:35 GMT        Michael Poole <mdpoole@troilus.org>     patch-49
+
+    Summary:
+      ioset event handling fixups
+    Revision:
+      srvx--devo--1.3--patch-49
+
+    src/ioset.c (ioset_connect): Properly handle the situation if the connect
+        handler closes the fd.
+      (ioset_accept): Rename "old_active_fd" to "old_active" for consistency.
+      (ioset_buffered_read): Make sure we bail if the fd is closed.
+      (ioset_events): Handle 'readable' on a connecting fd as an error.
+
+    modified files:
+     ChangeLog src/ioset.c
+
+
 2006-09-23 01:03:08 GMT        Michael Poole <mdpoole@troilus.org>     patch-48
 
     Summary:
index f79c23e53edc74be2575ff604a97f5fefefda3d9..a66af6f8b8fbb8badf2db3e20f71fbf4c98693d2 100644 (file)
@@ -194,9 +194,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,9 +259,13 @@ 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;
 }
 
@@ -311,7 +318,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 +330,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 +340,7 @@ ioset_accept(struct io_fd *listener)
         else
             engine->update(new_fd);
     }
-    active_fd = old_active_fd;
+    active_fd = old_active;
 }
 
 static int
@@ -394,14 +401,19 @@ ioset_buffered_read(struct io_fd *fd) {
         fdnum = fd->fd;
         while (fd->wants_reads && (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 +461,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);