X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fio%2Ftest%2Frun-15-timeout.c;h=f8ea6f4c22891c602c339a60401b89830730b5b7;hb=e2ce04eac30ec613c858bd4cd2ca12e1c464edb8;hp=510a97203865af538352ef233d0486f8562d5c60;hpb=0998955055e45ef980a2dfdbd302142269abdd26;p=ccan diff --git a/ccan/io/test/run-15-timeout.c b/ccan/io/test/run-15-timeout.c index 510a9720..f8ea6f4c 100644 --- a/ccan/io/test/run-15-timeout.c +++ b/ccan/io/test/run-15-timeout.c @@ -7,6 +7,10 @@ #include #include +#ifndef PORT +#define PORT "65015" +#endif + struct data { int state; int timeout_usec; @@ -15,14 +19,14 @@ struct data { }; -static struct io_plan *no_timeout(struct io_conn *conn, struct data *d) +static struct io_plan no_timeout(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; return io_close(conn, d); } -static struct io_plan *timeout(struct io_conn *conn, struct data *d) +static struct io_plan timeout(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; @@ -30,19 +34,23 @@ static struct io_plan *timeout(struct io_conn *conn, struct data *d) return io_close(conn, d); } -static struct io_plan *start_ok(struct io_conn *conn, struct data *d) +static void finish_ok(struct io_conn *conn, struct data *d) { - ok1(d->state == 0); + ok1(d->state == 2); d->state++; - io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d); - return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d); + io_break(d, io_idle()); } -static void finish_ok(struct io_conn *conn, struct data *d) +static void init_conn(int fd, struct data *d) { - ok1(d->state == 2); + struct io_conn *conn; + + ok1(d->state == 0); d->state++; - io_break(conn, d, NULL, NULL); + + conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), no_timeout, d), + finish_ok, d); + io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d); } static int make_listen_fd(const char *port, struct addrinfo **info) @@ -89,9 +97,9 @@ int main(void) d->state = 0; d->timed_out = false; d->timeout_usec = 100000; - fd = make_listen_fd("65002", &addrinfo); + fd = make_listen_fd(PORT, &addrinfo); ok1(fd >= 0); - l = io_new_listener(fd, start_ok, finish_ok, d); + l = io_new_listener(fd, init_conn, d); ok1(l); fflush(stdout);