]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/test/run-09-connect.c
ccan/io: implement debug.
[ccan] / ccan / io / test / run-09-connect.c
index fd7e1606232e2a59e112e87654f3194dbec7b857..124b6a1f70e715f08a3014a8cee72372611ce575 100644 (file)
@@ -6,36 +6,43 @@
 #include <sys/wait.h>
 #include <stdio.h>
 
-#ifndef PORT
+#ifdef DEBUG_CONN
+#define PORT "64009"
+#else
 #define PORT "65009"
 #endif
 
 static struct io_listener *l;
+static struct data *d2;
 
 struct data {
        int state;
        char buf[10];
 };
 
-static struct io_plan closer(struct io_conn *conn, struct data *d)
+static struct io_plan *closer(struct io_conn *conn, struct data *d)
 {
        d->state++;
-       return io_close();
+       return io_close(conn);
 }
 
-static struct io_plan connected(struct io_conn *conn, struct data *d2)
+static struct io_plan *connected(struct io_conn *conn, struct data *d2)
 {
        ok1(d2->state == 0);
        d2->state++;
-       return io_read(d2->buf, sizeof(d2->buf), closer, d2);
+       return io_read(conn, d2->buf, sizeof(d2->buf), closer, d2);
 }
 
-static void init_conn(int fd, 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_new_conn(fd, io_write(d->buf, sizeof(d->buf), closer, d));
        io_close_listener(l);
+
+       return io_write(conn, d->buf, sizeof(d->buf), closer, d);
 }
 
 static int make_listen_fd(const char *port, struct addrinfo **info)
@@ -70,9 +77,17 @@ static int make_listen_fd(const char *port, struct addrinfo **info)
        return fd;
 }
 
+static struct io_plan *setup_connect(struct io_conn *conn,
+                                    struct addrinfo *addrinfo)
+{
+       d2 = malloc(sizeof(*d2));
+       d2->state = 0;
+       return io_connect(conn, addrinfo, connected, d2);
+}
+
 int main(void)
 {
-       struct data *d = malloc(sizeof(*d)), *d2 = malloc(sizeof(*d2));
+       struct data *d = malloc(sizeof(*d));
        struct addrinfo *addrinfo;
        int fd;
 
@@ -82,15 +97,14 @@ int main(void)
        memset(d->buf, 'a', sizeof(d->buf));
        fd = make_listen_fd(PORT, &addrinfo);
        ok1(fd >= 0);
-       l = io_new_listener(fd, init_conn, d);
+       l = io_new_listener(NULL, fd, init_conn, d);
        ok1(l);
 
        fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
                    addrinfo->ai_protocol);
-       d2->state = 0;
-       ok1(io_new_conn(fd, io_connect(fd, addrinfo, connected, d2)));
+       ok1(io_new_conn(NULL, fd, setup_connect, addrinfo));
 
-       ok1(io_loop() == NULL);
+       ok1(io_loop(NULL, NULL) == NULL);
        ok1(d->state == 2);
        ok1(d2->state == 2);