]> git.ozlabs.org Git - ccan/commitdiff
ccan/io: have io_plan mappable back to io_conn.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 28 Feb 2019 05:24:09 +0000 (15:54 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 28 Feb 2019 05:24:09 +0000 (15:54 +1030)
io_conn contains two plans: by knowing what plan this is, it can be cast
back into the io_conn.  That helps for the next patch.

We also remove the strange logic where io_duplex() would return a
magic, invalid io_plan pointer.

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

index 2ee5a830e1233bd614d916d73d7087a9f9ec7cea..c071c5d6c5939372bcbb290a0a580e5257076e38 100644 (file)
@@ -37,6 +37,7 @@ enum io_plan_status {
 /**
  * struct io_plan - one half of I/O to do
  * @status: the status of this plan.
+ * @dir: are we plan[0] or plan[1] inside io_conn?
  * @io: function to call when fd becomes read/writable, returns 0 to be
  *      called again, 1 if it's finished, and -1 on error (fd will be closed)
  * @next: the next function which is called if io returns 1.
@@ -45,6 +46,7 @@ enum io_plan_status {
  */
 struct io_plan {
        enum io_plan_status status;
+       enum io_direction dir;
 
        int (*io)(int fd, struct io_plan_arg *arg);
 
index bdcbf9e26b8f9f580fea08c2c0913d31efe9dcc7..c5c9bee0daf0329f433eec930058bfd20905f96c 100644 (file)
@@ -61,10 +61,7 @@ static bool next_plan(struct io_conn *conn, struct io_plan *plan)
        if (plan == &io_conn_freed)
                return false;
 
-       /* It should have set a plan inside this conn (or duplex) */
-       assert(plan == &conn->plan[IO_IN]
-              || plan == &conn->plan[IO_OUT]
-              || plan == &conn->plan[2]);
+       assert(plan == &conn->plan[plan->dir]);
        assert(conn->plan[IO_IN].status != IO_UNSET
               || conn->plan[IO_OUT].status != IO_UNSET);
 
@@ -108,6 +105,10 @@ struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
        /* Keep our I/O async. */
        io_fd_block(fd, false);
 
+       /* So we can get back from plan -> conn later */
+       conn->plan[IO_OUT].dir = IO_OUT;
+       conn->plan[IO_IN].dir = IO_IN;
+
        /* We start with out doing nothing, and in doing our init. */
        conn->plan[IO_OUT].status = IO_UNSET;
 
@@ -488,7 +489,7 @@ struct io_plan *io_duplex(struct io_conn *conn,
        assert(conn == container_of(in_plan, struct io_conn, plan[IO_IN]));
        /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
        assert(out_plan == in_plan + 1);
-       return out_plan + 1;
+       return in_plan;
 }
 
 struct io_plan *io_halfclose(struct io_conn *conn)