]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
150f7269a4428b9890319481dfb93532971304ba
[ccan] / ccan / io / io.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <poll.h>
12
13 void *io_loop_return;
14
15 struct io_listener *io_new_listener_(int fd,
16                                      struct io_plan *(*start)(struct io_conn *,
17                                                               void *arg),
18                                      void (*finish)(struct io_conn *, void *),
19                                      void *arg)
20 {
21         struct io_listener *l = malloc(sizeof(*l));
22
23         if (!l)
24                 return NULL;
25
26         l->fd.listener = true;
27         l->fd.fd = fd;
28         l->next = start;
29         l->finish = finish;
30         l->conn_arg = arg;
31         if (!add_listener(l)) {
32                 free(l);
33                 return NULL;
34         }
35         return l;
36 }
37
38 void io_close_listener(struct io_listener *l)
39 {
40         close(l->fd.fd);
41         del_listener(l);
42         free(l);
43 }
44
45 struct io_conn *io_new_conn_(int fd,
46                              struct io_plan *(*start)(struct io_conn *, void *),
47                              void (*finish)(struct io_conn *, void *),
48                              void *arg)
49 {
50         struct io_conn *conn = malloc(sizeof(*conn));
51
52         if (!conn)
53                 return NULL;
54
55         conn->fd.listener = false;
56         conn->fd.fd = fd;
57         conn->next = start;
58         conn->finish = finish;
59         conn->finish_arg = conn->next_arg = arg;
60         conn->pollflag = 0;
61         conn->state = NEXT;
62         conn->duplex = NULL;
63         conn->timeout = NULL;
64         if (!add_conn(conn)) {
65                 free(conn);
66                 return NULL;
67         }
68         return conn;
69 }
70
71 struct io_conn *io_duplex_(struct io_conn *old,
72                              struct io_plan *(*start)(struct io_conn *, void *),
73                              void (*finish)(struct io_conn *, void *),
74                              void *arg)
75 {
76         struct io_conn *conn;
77
78         assert(!old->duplex);
79
80         conn = malloc(sizeof(*conn));
81         if (!conn)
82                 return NULL;
83
84         conn->fd.listener = false;
85         conn->fd.fd = old->fd.fd;
86         conn->next = start;
87         conn->finish = finish;
88         conn->finish_arg = conn->next_arg = arg;
89         conn->pollflag = 0;
90         conn->state = NEXT;
91         conn->duplex = old;
92         conn->timeout = NULL;
93         if (!add_duplex(conn)) {
94                 free(conn);
95                 return NULL;
96         }
97         old->duplex = conn;
98         return conn;
99 }
100
101 static inline struct io_plan *to_ioplan(enum io_state state)
102 {
103         return (struct io_plan *)(long)state;
104 }
105
106 bool io_timeout_(struct io_conn *conn, struct timespec ts,
107                  struct io_plan *(*cb)(struct io_conn *, void *), void *arg)
108 {
109         if (!conn->timeout) {
110                 conn->timeout = malloc(sizeof(*conn->timeout));
111                 if (!conn->timeout)
112                         return false;
113         } else
114                 assert(!timeout_active(conn));
115
116         conn->timeout->next = cb;
117         conn->timeout->next_arg = arg;
118         backend_add_timeout(conn, ts);
119         return true;
120 }
121
122 /* Queue some data to be written. */
123 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
124                           struct io_plan *(*cb)(struct io_conn *, void *),
125                           void *arg)
126 {
127         conn->u.write.buf = data;
128         conn->u.write.len = len;
129         conn->next = cb;
130         conn->next_arg = arg;
131         conn->pollflag = POLLOUT;
132         return to_ioplan(WRITE);
133 }
134
135 /* Queue a request to read into a buffer. */
136 struct io_plan *io_read_(struct io_conn *conn, void *data, size_t len,
137                          struct io_plan *(*cb)(struct io_conn *, void *),
138                          void *arg)
139 {
140         conn->u.read.buf = data;
141         conn->u.read.len = len;
142         conn->next = cb;
143         conn->next_arg = arg;
144         conn->pollflag = POLLIN;
145         return to_ioplan(READ);
146 }
147
148 /* Queue a partial request to read into a buffer. */
149 struct io_plan *io_read_partial_(struct io_conn *conn, void *data, size_t *len,
150                                  struct io_plan *(*cb)(struct io_conn *, void *),
151                                  void *arg)
152 {
153         conn->u.readpart.buf = data;
154         conn->u.readpart.lenp = len;
155         conn->next = cb;
156         conn->next_arg = arg;
157         conn->pollflag = POLLIN;
158         return to_ioplan(READPART);
159 }
160
161 /* Queue a partial write request. */
162 struct io_plan *io_write_partial_(struct io_conn *conn,
163                                   const void *data, size_t *len,
164                                   struct io_plan *(*cb)(struct io_conn*, void *),
165                                   void *arg)
166 {
167         conn->u.writepart.buf = data;
168         conn->u.writepart.lenp = len;
169         conn->next = cb;
170         conn->next_arg = arg;
171         conn->pollflag = POLLOUT;
172         return to_ioplan(WRITEPART);
173 }
174
175 struct io_plan *io_idle(struct io_conn *conn)
176 {
177         conn->pollflag = 0;
178         return to_ioplan(IDLE);
179 }
180
181 void io_wake_(struct io_conn *conn,
182               struct io_plan *(*fn)(struct io_conn *, void *), void *arg)
183
184 {
185         /* It might have finished, but we haven't called its finish() yet. */
186         if (conn->state == FINISHED)
187                 return;
188         assert(conn->state == IDLE);
189         conn->next = fn;
190         conn->next_arg = arg;
191         backend_set_state(conn, to_ioplan(NEXT));
192 }
193
194 static struct io_plan *do_next(struct io_conn *conn)
195 {
196         if (timeout_active(conn))
197                 backend_del_timeout(conn);
198         return conn->next(conn, conn->next_arg);
199 }
200
201 struct io_plan *do_ready(struct io_conn *conn)
202 {
203         ssize_t ret;
204         bool finished;
205
206         switch (conn->state) {
207         case WRITE:
208                 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
209                 if (ret < 0)
210                         return io_close(conn, NULL);
211                 conn->u.write.buf += ret;
212                 conn->u.write.len -= ret;
213                 finished = (conn->u.write.len == 0);
214                 break;
215         case WRITEPART:
216                 ret = write(conn->fd.fd, conn->u.writepart.buf,
217                             *conn->u.writepart.lenp);
218                 if (ret < 0)
219                         return io_close(conn, NULL);
220                 *conn->u.writepart.lenp = ret;
221                 finished = true;
222                 break;
223         case READ:
224                 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
225                 if (ret <= 0)
226                         return io_close(conn, NULL);
227                 conn->u.read.buf += ret;
228                 conn->u.read.len -= ret;
229                 finished = (conn->u.read.len == 0);
230                 break;
231         case READPART:
232                 ret = read(conn->fd.fd, conn->u.readpart.buf,
233                             *conn->u.readpart.lenp);
234                 if (ret <= 0)
235                         return io_close(conn, NULL);
236                 *conn->u.readpart.lenp = ret;
237                 finished = true;
238                 break;
239         default:
240                 /* Shouldn't happen. */
241                 abort();
242         }
243
244         if (finished)
245                 return do_next(conn);
246         return to_ioplan(conn->state);
247 }
248
249 /* Useful next functions. */
250 /* Close the connection, we're done. */
251 struct io_plan *io_close(struct io_conn *conn, void *arg)
252 {
253         return to_ioplan(FINISHED);
254 }
255
256 /* Exit the loop, returning this (non-NULL) arg. */
257 struct io_plan *io_break_(struct io_conn *conn, void *ret,
258                           struct io_plan *(*fn)(struct io_conn *, void *),
259                           void *arg)
260 {
261         io_loop_return = ret;
262         conn->next = fn;
263         conn->next_arg = arg;
264
265         return to_ioplan(NEXT);
266 }