#include <sys/wait.h>
#include <stdio.h>
+#ifdef DEBUG_CONN
+#define PORT "64012"
+#else
+#define PORT "65012"
+#endif
+
struct data {
struct io_listener *l;
int state;
d->state++;
}
-static struct io_plan write_done(struct io_conn *conn, struct data *d)
+static struct io_plan *r_done(struct io_conn *conn, struct data *d)
{
d->state++;
- return io_close(conn, NULL);
+ if (d->state == 3)
+ return io_close(conn);
+ return io_wait(conn, NULL, io_never, NULL);
}
-static void init_conn(int fd, struct data *d)
+static struct io_plan *w_done(struct io_conn *conn, struct data *d)
{
- struct io_conn *conn;
+ d->state++;
+ if (d->state == 3)
+ return io_close(conn);
+ return io_out_wait(conn, NULL, io_never, NULL);
+}
+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_close_listener(d->l);
memset(d->wbuf, 7, sizeof(d->wbuf));
+ io_set_finish(conn, finish_ok, d);
- conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d),
- finish_ok, d);
- ok1(io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d),
- 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)
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, init_conn, d);
+ d->l = io_new_listener(NULL, fd, init_conn, d);
ok1(d->l);
fflush(stdout);
if (!fork()) {
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);