]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/test/run-15-timeout.c
ccan/io: io_connect()
[ccan] / ccan / io / test / run-15-timeout.c
index e8897c95759e3f3a58ce1905d321fd162d19776a..6f92ec3a634848c0770fcd21016e56acfb082274 100644 (file)
@@ -7,6 +7,10 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#ifndef PORT
+#define PORT "65015"
+#endif
+
 struct data {
        int state;
        int timeout_usec;
@@ -15,34 +19,38 @@ 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);
+       return io_close();
 }
 
-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++;
        d->timed_out = true;
-       return io_close(conn, d);
+       return io_close();
 }
 
-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(d->buf, sizeof(d->buf), io_next(conn, 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(d, NULL);
+
+       conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), no_timeout, d));
+       io_set_finish(conn, 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);