ccan/io: remove next & finish from union.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 10:58:35 +0000 (21:28 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 10:58:35 +0000 (21:28 +1030)
Sure, both listener and conn need them, but for different things
(listener uses them simply to set up conn).  Putting them in the
common union was a mistake.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io/backend.h
ccan/io/io.c
ccan/io/poll.c

index 2eb333091c187b4c39dc5ade4e4e7d84ee6054c2..bc88c289b7bf412d8d12d46497e1f375616a8e72 100644 (file)
@@ -8,18 +8,17 @@ struct fd {
        int fd;
        bool listener;
        size_t backend_info;
-
-       struct io_plan *(*next)(struct io_conn *, void *arg);
-       void *next_arg;
-
-       void (*finish)(struct io_conn *, void *arg);
-       void *finish_arg;
 };
 
 
 /* Listeners create connections. */
 struct io_listener {
        struct fd fd;
+
+       /* These are for connections we create. */
+       struct io_plan *(*next)(struct io_conn *, void *arg);
+       void (*finish)(struct io_conn *, void *arg);
+       void *conn_arg;
 };
 
 enum io_state {
@@ -74,6 +73,12 @@ struct io_timeout {
 struct io_conn {
        struct fd fd;
 
+       struct io_plan *(*next)(struct io_conn *, void *arg);
+       void *next_arg;
+
+       void (*finish)(struct io_conn *, void *arg);
+       void *finish_arg;
+
        struct io_conn *duplex;
        struct io_timeout *timeout;
 
index 2ce5d6ae3224b1ac8cd5c51b5e354871a46f8e2c..150f7269a4428b9890319481dfb93532971304ba 100644 (file)
@@ -25,9 +25,9 @@ struct io_listener *io_new_listener_(int fd,
 
        l->fd.listener = true;
        l->fd.fd = fd;
-       l->fd.next = start;
-       l->fd.finish = finish;
-       l->fd.finish_arg = l->fd.next_arg = arg;
+       l->next = start;
+       l->finish = finish;
+       l->conn_arg = arg;
        if (!add_listener(l)) {
                free(l);
                return NULL;
@@ -54,9 +54,9 @@ struct io_conn *io_new_conn_(int fd,
 
        conn->fd.listener = false;
        conn->fd.fd = fd;
-       conn->fd.next = start;
-       conn->fd.finish = finish;
-       conn->fd.finish_arg = conn->fd.next_arg = arg;
+       conn->next = start;
+       conn->finish = finish;
+       conn->finish_arg = conn->next_arg = arg;
        conn->pollflag = 0;
        conn->state = NEXT;
        conn->duplex = NULL;
@@ -83,9 +83,9 @@ struct io_conn *io_duplex_(struct io_conn *old,
 
        conn->fd.listener = false;
        conn->fd.fd = old->fd.fd;
-       conn->fd.next = start;
-       conn->fd.finish = finish;
-       conn->fd.finish_arg = conn->fd.next_arg = arg;
+       conn->next = start;
+       conn->finish = finish;
+       conn->finish_arg = conn->next_arg = arg;
        conn->pollflag = 0;
        conn->state = NEXT;
        conn->duplex = old;
@@ -126,8 +126,8 @@ struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
 {
        conn->u.write.buf = data;
        conn->u.write.len = len;
-       conn->fd.next = cb;
-       conn->fd.next_arg = arg;
+       conn->next = cb;
+       conn->next_arg = arg;
        conn->pollflag = POLLOUT;
        return to_ioplan(WRITE);
 }
@@ -139,8 +139,8 @@ struct io_plan *io_read_(struct io_conn *conn, void *data, size_t len,
 {
        conn->u.read.buf = data;
        conn->u.read.len = len;
-       conn->fd.next = cb;
-       conn->fd.next_arg = arg;
+       conn->next = cb;
+       conn->next_arg = arg;
        conn->pollflag = POLLIN;
        return to_ioplan(READ);
 }
@@ -152,8 +152,8 @@ struct io_plan *io_read_partial_(struct io_conn *conn, void *data, size_t *len,
 {
        conn->u.readpart.buf = data;
        conn->u.readpart.lenp = len;
-       conn->fd.next = cb;
-       conn->fd.next_arg = arg;
+       conn->next = cb;
+       conn->next_arg = arg;
        conn->pollflag = POLLIN;
        return to_ioplan(READPART);
 }
@@ -166,8 +166,8 @@ struct io_plan *io_write_partial_(struct io_conn *conn,
 {
        conn->u.writepart.buf = data;
        conn->u.writepart.lenp = len;
-       conn->fd.next = cb;
-       conn->fd.next_arg = arg;
+       conn->next = cb;
+       conn->next_arg = arg;
        conn->pollflag = POLLOUT;
        return to_ioplan(WRITEPART);
 }
@@ -186,8 +186,8 @@ void io_wake_(struct io_conn *conn,
        if (conn->state == FINISHED)
                return;
        assert(conn->state == IDLE);
-       conn->fd.next = fn;
-       conn->fd.next_arg = arg;
+       conn->next = fn;
+       conn->next_arg = arg;
        backend_set_state(conn, to_ioplan(NEXT));
 }
 
@@ -195,7 +195,7 @@ static struct io_plan *do_next(struct io_conn *conn)
 {
        if (timeout_active(conn))
                backend_del_timeout(conn);
-       return conn->fd.next(conn, conn->fd.next_arg);
+       return conn->next(conn, conn->next_arg);
 }
 
 struct io_plan *do_ready(struct io_conn *conn)
@@ -259,8 +259,8 @@ struct io_plan *io_break_(struct io_conn *conn, void *ret,
                          void *arg)
 {
        io_loop_return = ret;
-       conn->fd.next = fn;
-       conn->fd.next_arg = arg;
+       conn->next = fn;
+       conn->next_arg = arg;
 
        return to_ioplan(NEXT);
 }
index bd76ebc2aa5701011f1844c9ac54bd49f991b944..5982ef1535e363723ac4904729fa824e1d718855 100644 (file)
@@ -90,8 +90,8 @@ bool add_duplex(struct io_conn *c)
 
 static void del_conn(struct io_conn *conn)
 {
-       if (conn->fd.finish)
-               conn->fd.finish(conn, conn->fd.finish_arg);
+       if (conn->finish)
+               conn->finish(conn, conn->finish_arg);
        if (timeout_active(conn))
                backend_del_timeout(conn);
        free(conn->timeout);
@@ -146,7 +146,7 @@ static void accept_conn(struct io_listener *l)
        /* FIXME: What to do here? */
        if (fd < 0)
                return;
-       c = io_new_conn(fd, l->fd.next, l->fd.finish, l->fd.next_arg);
+       c = io_new_conn(fd, l->next, l->finish, l->conn_arg);
        if (!c) {
                close(fd);
                return;
@@ -174,9 +174,7 @@ static void finish_and_next(bool finished_only)
                                free(c);
                                i--;
                        } else if (!finished_only && c->state == NEXT) {
-                               backend_set_state(c,
-                                                 c->fd.next(c,
-                                                            c->fd.next_arg));
+                               backend_set_state(c, c->next(c, c->next_arg));
                                num_next--;
                        }
                }