]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-02-read.c
ccan/io: simplify I/O callbacks.
[ccan] / ccan / io / test / run-02-read.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8
9 #ifndef PORT
10 #define PORT "65002"
11 #endif
12
13 struct data {
14         int state;
15         char buf[4];
16 };
17
18 static void finish_ok(struct io_conn *conn, struct data *d)
19 {
20         ok1(d->state == 1);
21         d->state++;
22         io_break(d, io_idle());
23 }
24
25 static void init_conn(int fd, struct data *d)
26 {
27         ok1(d->state == 0);
28         d->state++;
29
30         io_set_finish(io_new_conn(fd,
31                                   io_read(d->buf, sizeof(d->buf), io_close, d)),
32                       finish_ok, d);
33 }
34
35 static int make_listen_fd(const char *port, struct addrinfo **info)
36 {
37         int fd, on = 1;
38         struct addrinfo *addrinfo, hints;
39
40         memset(&hints, 0, sizeof(hints));
41         hints.ai_family = AF_UNSPEC;
42         hints.ai_socktype = SOCK_STREAM;
43         hints.ai_flags = AI_PASSIVE;
44         hints.ai_protocol = 0;
45
46         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
47                 return -1;
48
49         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
50                     addrinfo->ai_protocol);
51         if (fd < 0)
52                 return -1;
53
54         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
55         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
56                 close(fd);
57                 return -1;
58         }
59         if (listen(fd, 1) != 0) {
60                 close(fd);
61                 return -1;
62         }
63         *info = addrinfo;
64         return fd;
65 }
66
67 int main(void)
68 {
69         struct data *d = malloc(sizeof(*d));
70         struct addrinfo *addrinfo;
71         struct io_listener *l;
72         int fd, status;
73
74         /* This is how many tests you plan to run */
75         plan_tests(10);
76         d->state = 0;
77         fd = make_listen_fd(PORT, &addrinfo);
78         ok1(fd >= 0);
79         l = io_new_listener(fd, init_conn, d);
80         ok1(l);
81         fflush(stdout);
82         if (!fork()) {
83                 int i;
84
85                 io_close_listener(l);
86                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
87                             addrinfo->ai_protocol);
88                 if (fd < 0)
89                         exit(1);
90                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
91                         exit(2);
92                 signal(SIGPIPE, SIG_IGN);
93                 for (i = 0; i < strlen("hellothere"); i++) {
94                         if (write(fd, "hellothere" + i, 1) != 1)
95                                 break;
96                 }
97                 close(fd);
98                 freeaddrinfo(addrinfo);
99                 free(d);
100                 exit(0);
101         }
102         freeaddrinfo(addrinfo);
103         ok1(io_loop() == d);
104         ok1(d->state == 2);
105         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
106         free(d);
107         io_close_listener(l);
108
109         ok1(wait(&status));
110         ok1(WIFEXITED(status));
111         ok1(WEXITSTATUS(status) == 0);
112
113         /* This exits depending on whether all tests passed */
114         return exit_status();
115 }