X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fio%2Ftest%2Frun-12-bidir.c;h=77843ae7ba0bb2951d32c6d3e56b7d64d2916eed;hb=d00c9d1be5a56fbf990882f9a6da5704b3f1ea75;hp=eb4f4f0551f71804396131ae23f148a87699770d;hpb=0998955055e45ef980a2dfdbd302142269abdd26;p=ccan diff --git a/ccan/io/test/run-12-bidir.c b/ccan/io/test/run-12-bidir.c index eb4f4f05..77843ae7 100644 --- a/ccan/io/test/run-12-bidir.c +++ b/ccan/io/test/run-12-bidir.c @@ -6,6 +6,8 @@ #include #include +#define PORT "65012" + struct data { struct io_listener *l; int state; @@ -18,13 +20,23 @@ 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 *r_done(struct io_conn *conn, struct data *d) +{ + d->state++; + if (d->state == 3) + return io_close(conn); + return io_wait(conn, NULL, io_never, NULL); +} + +static struct io_plan *w_done(struct io_conn *conn, struct data *d) { d->state++; - return io_write(conn, d->wbuf, sizeof(d->wbuf), io_close, d); + if (d->state == 3) + return io_close(conn); + return io_out_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) { ok1(d->state == 0); d->state++; @@ -32,8 +44,11 @@ static struct io_plan *start_ok(struct io_conn *conn, struct data *d) io_close_listener(d->l); memset(d->wbuf, 7, sizeof(d->wbuf)); - ok1(io_duplex(conn, write_out, finish_ok, d)); - return io_read(conn, d->buf, sizeof(d->buf), io_close, d); + io_set_finish(conn, finish_ok, d); + + return io_duplex(conn, + io_read(conn, d->buf, sizeof(d->buf), r_done, d), + io_write(conn, d->wbuf, sizeof(d->wbuf), w_done, d)); } static int make_listen_fd(const char *port, struct addrinfo **info) @@ -75,11 +90,11 @@ int main(void) int fd, status; /* This is how many tests you plan to run */ - plan_tests(10); + plan_tests(9); d->state = 0; - fd = make_listen_fd("65012", &addrinfo); + fd = make_listen_fd(PORT, &addrinfo); ok1(fd >= 0); - d->l = io_new_listener(fd, start_ok, finish_ok, d); + d->l = io_new_listener(NULL, fd, init_conn, d); ok1(d->l); fflush(stdout); if (!fork()) { @@ -108,7 +123,7 @@ int main(void) exit(0); } freeaddrinfo(addrinfo); - ok1(io_loop() == NULL); + ok1(io_loop(NULL, NULL) == NULL); ok1(d->state == 4); ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0); free(d);