X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fio%2Ftest%2Frun-14-duplex-both-read.c;h=ed77cab8ac69d4530f0525c24f6c1db22407fb17;hb=bc6d22ecf820d0b4547ace17ff7e7ecebf0e415b;hp=366f1d44bbaafdf65c09baa1942a490df6c60053;hpb=dded32f2b03fca526271e35671fa36d299880429;p=ccan diff --git a/ccan/io/test/run-14-duplex-both-read.c b/ccan/io/test/run-14-duplex-both-read.c index 366f1d44..ed77cab8 100644 --- a/ccan/io/test/run-14-duplex-both-read.c +++ b/ccan/io/test/run-14-duplex-both-read.c @@ -8,16 +8,11 @@ #include #include -#ifndef PORT #define PORT "65014" -#endif - -#define is_idle(conn) ((conn)->plan.io == NULL) struct data { struct io_listener *l; int state; - struct io_conn *c1, *c2; char buf[4]; char wbuf[32]; }; @@ -27,32 +22,26 @@ static void finish_ok(struct io_conn *conn, struct data *d) d->state++; } -static struct io_plan end(struct io_conn *conn, struct data *d) +static struct io_plan *end(struct io_conn *conn, struct data *d) { d->state++; - - /* last one out closes. */ - if (conn == d->c1 && is_idle(d->c2)) - return io_close(); - - /* last one out closes. */ - if (conn == d->c2 && is_idle(d->c1)) - return io_close(); - - return io_idle(); + /* Close on top of halfclose should work. */ + if (d->state == 4) + return io_close(conn); + else + return io_halfclose(conn); } -static struct io_plan make_duplex(struct io_conn *conn, struct data *d) +static struct io_plan *make_duplex(struct io_conn *conn, struct data *d) { + d->state++; /* Have duplex read the rest of the buffer. */ - d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d)); - ok1(d->c2); - io_set_finish(d->c2, finish_ok, d); - - return io_write(d->wbuf, sizeof(d->wbuf), end, d); + return io_duplex(conn, + io_read(conn, d->buf+1, sizeof(d->buf)-1, end, d), + io_write(conn, d->wbuf, sizeof(d->wbuf), end, d)); } -static void init_conn(int fd, struct data *d) +static struct io_plan *init_conn(struct io_conn *conn, struct data *d) { ok1(d->state == 0); d->state++; @@ -60,9 +49,8 @@ static void init_conn(int fd, struct data *d) io_close_listener(d->l); memset(d->wbuf, 7, sizeof(d->wbuf)); - - d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d)); - io_set_finish(d->c1, finish_ok, d); + io_set_finish(conn, finish_ok, d); + return io_read(conn, d->buf, 1, make_duplex, d); } static int make_listen_fd(const char *port, struct addrinfo **info) @@ -104,11 +92,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(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()) { @@ -137,7 +125,7 @@ int main(void) exit(0); } freeaddrinfo(addrinfo); - ok1(io_loop() == NULL); + ok1(io_loop(NULL, NULL) == NULL); ok1(d->state == 5); ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0); free(d);