]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: use explicit IO callback functions, instead of io_state values.
[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 static enum io_result do_write(struct io_conn *conn)
123 {
124         ssize_t ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
125         if (ret < 0)
126                 return RESULT_CLOSE;
127
128         conn->u.write.buf += ret;
129         conn->u.write.len -= ret;
130         if (conn->u.write.len == 0)
131                 return RESULT_FINISHED;
132         else
133                 return RESULT_AGAIN;
134 }
135
136 /* Queue some data to be written. */
137 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
138                           struct io_plan *(*cb)(struct io_conn *, void *),
139                           void *arg)
140 {
141         conn->u.write.buf = data;
142         conn->u.write.len = len;
143         conn->io = do_write;
144         conn->next = cb;
145         conn->next_arg = arg;
146         conn->pollflag = POLLOUT;
147         return to_ioplan(IO);
148 }
149
150 static enum io_result do_read(struct io_conn *conn)
151 {
152         ssize_t ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
153         if (ret <= 0)
154                 return RESULT_CLOSE;
155         conn->u.read.buf += ret;
156         conn->u.read.len -= ret;
157         if (conn->u.read.len == 0)
158                 return RESULT_FINISHED;
159         else
160                 return RESULT_AGAIN;
161 }
162
163 /* Queue a request to read into a buffer. */
164 struct io_plan *io_read_(struct io_conn *conn, void *data, size_t len,
165                          struct io_plan *(*cb)(struct io_conn *, void *),
166                          void *arg)
167 {
168         conn->u.read.buf = data;
169         conn->u.read.len = len;
170         conn->io = do_read;
171         conn->next = cb;
172         conn->next_arg = arg;
173         conn->pollflag = POLLIN;
174         return to_ioplan(IO);
175 }
176
177 static enum io_result do_read_partial(struct io_conn *conn)
178 {
179         ssize_t ret = read(conn->fd.fd, conn->u.readpart.buf,
180                            *conn->u.readpart.lenp);
181         if (ret <= 0)
182                 return RESULT_CLOSE;
183         *conn->u.readpart.lenp = ret;
184         return RESULT_FINISHED;
185 }
186
187 /* Queue a partial request to read into a buffer. */
188 struct io_plan *io_read_partial_(struct io_conn *conn, void *data, size_t *len,
189                                  struct io_plan *(*cb)(struct io_conn *, void *),
190                                  void *arg)
191 {
192         conn->u.readpart.buf = data;
193         conn->u.readpart.lenp = len;
194         conn->io = do_read_partial;
195         conn->next = cb;
196         conn->next_arg = arg;
197         conn->pollflag = POLLIN;
198         return to_ioplan(IO);
199 }
200
201 static enum io_result do_write_partial(struct io_conn *conn)
202 {
203         ssize_t ret = write(conn->fd.fd, conn->u.writepart.buf,
204                             *conn->u.writepart.lenp);
205         if (ret < 0)
206                 return RESULT_CLOSE;
207         *conn->u.writepart.lenp = ret;
208         return RESULT_FINISHED;
209 }
210
211 /* Queue a partial write request. */
212 struct io_plan *io_write_partial_(struct io_conn *conn,
213                                   const void *data, size_t *len,
214                                   struct io_plan *(*cb)(struct io_conn*, void *),
215                                   void *arg)
216 {
217         conn->u.writepart.buf = data;
218         conn->u.writepart.lenp = len;
219         conn->io = do_write_partial;
220         conn->next = cb;
221         conn->next_arg = arg;
222         conn->pollflag = POLLOUT;
223         return to_ioplan(IO);
224 }
225
226 struct io_plan *io_idle(struct io_conn *conn)
227 {
228         conn->pollflag = 0;
229         return to_ioplan(IDLE);
230 }
231
232 void io_wake_(struct io_conn *conn,
233               struct io_plan *(*fn)(struct io_conn *, void *), void *arg)
234
235 {
236         /* It might have finished, but we haven't called its finish() yet. */
237         if (conn->state == FINISHED)
238                 return;
239         assert(conn->state == IDLE);
240         conn->next = fn;
241         conn->next_arg = arg;
242         backend_set_state(conn, to_ioplan(NEXT));
243 }
244
245 static struct io_plan *do_next(struct io_conn *conn)
246 {
247         if (timeout_active(conn))
248                 backend_del_timeout(conn);
249         return conn->next(conn, conn->next_arg);
250 }
251
252 struct io_plan *do_ready(struct io_conn *conn)
253 {
254         assert(conn->state == IO);
255         switch (conn->io(conn)) {
256         case RESULT_CLOSE:
257                 return io_close(conn, NULL);
258         case RESULT_FINISHED:
259                 return do_next(conn);
260         case RESULT_AGAIN:
261                 return to_ioplan(conn->state);
262         default:
263                 abort();
264         }
265 }
266
267 /* Useful next functions. */
268 /* Close the connection, we're done. */
269 struct io_plan *io_close(struct io_conn *conn, void *arg)
270 {
271         return to_ioplan(FINISHED);
272 }
273
274 /* Exit the loop, returning this (non-NULL) arg. */
275 struct io_plan *io_break_(struct io_conn *conn, void *ret,
276                           struct io_plan *(*fn)(struct io_conn *, void *),
277                           void *arg)
278 {
279         io_loop_return = ret;
280         conn->next = fn;
281         conn->next_arg = arg;
282
283         return to_ioplan(NEXT);
284 }