X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fio%2Ftest%2Frun-12-bidir.c;h=2ecc5470e7ef5a8d2ec22469bbb119a82833ba60;hb=43185ec669720903fa86aac5f2d1b1ee62e3a12b;hp=3534bc3138f92df5aa6bb57e51849aac26ac6f3b;hpb=737705f0c2ec60ea5b51ca55299488d86db37b5d;p=ccan diff --git a/ccan/io/test/run-12-bidir.c b/ccan/io/test/run-12-bidir.c index 3534bc31..2ecc5470 100644 --- a/ccan/io/test/run-12-bidir.c +++ b/ccan/io/test/run-12-bidir.c @@ -6,9 +6,16 @@ #include #include +#ifdef DEBUG_CONN +#define PORT "64012" +#else +#define PORT "65012" +#endif + struct data { struct io_listener *l; int state; + int done; char buf[4]; char wbuf[32]; }; @@ -18,28 +25,31 @@ static void finish_ok(struct io_conn *conn, struct data *d) d->state++; } -static struct io_plan write_out(struct io_conn *conn, struct data *d) +static struct io_plan *rw_done(struct io_conn *conn, struct data *d) { d->state++; - return io_write(d->wbuf, sizeof(d->wbuf), io_close, d); + d->done++; + if (d->done == 2) + return io_close(conn); + return io_wait(conn, NULL, io_never, NULL); } -static struct io_plan start_ok(struct io_conn *conn, struct data *d) +static struct io_plan *init_conn(struct io_conn *conn, struct data *d) { +#ifdef DEBUG_CONN + io_set_debug(conn, true); +#endif ok1(d->state == 0); d->state++; io_close_listener(d->l); memset(d->wbuf, 7, sizeof(d->wbuf)); - ok1(io_duplex(conn, write_out, finish_ok, d)); - return io_read(d->buf, sizeof(d->buf), io_close, d); -} + io_set_finish(conn, finish_ok, d); -static void init_conn(int fd, struct data *d) -{ - if (!io_new_conn(fd, start_ok, finish_ok, d)) - abort(); + return io_duplex(conn, + 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) @@ -83,9 +93,10 @@ int main(void) /* This is how many tests you plan to run */ plan_tests(10); d->state = 0; - fd = make_listen_fd("65012", &addrinfo); + 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()) { @@ -114,8 +125,9 @@ int main(void) exit(0); } freeaddrinfo(addrinfo); - ok1(io_loop() == NULL); + ok1(io_loop(NULL, NULL) == NULL); ok1(d->state == 4); + ok1(d->done == 2); ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0); free(d);