]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/test/run-12-bidir.c
ccan/io: duplex support.
[ccan] / ccan / io / test / run-12-bidir.c
index 533f465d12a8e161a646177cd0e1046d944f86fc..0329b81f6755e63f338de1f1890bb803d72b422e 100644 (file)
@@ -6,7 +6,6 @@
 #include <sys/wait.h>
 #include <stdio.h>
 
-#if 0
 #ifndef PORT
 #define PORT "65012"
 #endif
@@ -14,6 +13,7 @@
 struct data {
        struct io_listener *l;
        int state;
+       int done;
        char buf[4];
        char wbuf[32];
 };
@@ -23,28 +23,27 @@ static void finish_ok(struct io_conn *conn, struct data *d)
        d->state++;
 }
 
-static struct io_plan *write_done(struct io_conn *conn, struct data *d)
+static struct io_plan *rw_done(struct io_conn *conn, struct data *d)
 {
        d->state++;
-       return io_close(conn);
+       d->done++;
+       if (d->done == 2)
+               return io_close(conn);
+       return io_wait(conn, NULL, io_never, NULL);
 }
 
-static void init_conn(int fd, struct data *d)
+static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
 {
-       struct io_conn *conn;
-
        ok1(d->state == 0);
        d->state++;
 
        io_close_listener(d->l);
 
        memset(d->wbuf, 7, sizeof(d->wbuf));
-
-       conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close_cb, d));
-       io_set_finish(conn, finish_ok, d);
-       conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
-       ok1(conn);
        io_set_finish(conn, finish_ok, d);
+
+       return io_duplex(io_read(conn, d->buf, sizeof(d->buf), rw_done, d),
+                        io_write(conn, d->wbuf, sizeof(d->wbuf), rw_done, d));
 }
 
 static int make_listen_fd(const char *port, struct addrinfo **info)
@@ -88,9 +87,10 @@ int main(void)
        /* This is how many tests you plan to run */
        plan_tests(10);
        d->state = 0;
+       d->done = 0;
        fd = make_listen_fd(PORT, &addrinfo);
        ok1(fd >= 0);
-       d->l = io_new_listener(fd, init_conn, d);
+       d->l = io_new_listener(NULL, fd, init_conn, d);
        ok1(d->l);
        fflush(stdout);
        if (!fork()) {
@@ -121,6 +121,7 @@ int main(void)
        freeaddrinfo(addrinfo);
        ok1(io_loop() == NULL);
        ok1(d->state == 4);
+       ok1(d->done == 2);
        ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
        free(d);
 
@@ -131,9 +132,3 @@ int main(void)
        /* This exits depending on whether all tests passed */
        return exit_status();
 }
-#else
-int main(void)
-{
-       return 0;
-}
-#endif