]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: simplify do_ready.
[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                                      void (*init)(int fd, void *arg),
17                                      void *arg)
18 {
19         struct io_listener *l = malloc(sizeof(*l));
20
21         if (!l)
22                 return NULL;
23
24         l->fd.listener = true;
25         l->fd.fd = fd;
26         l->init = init;
27         l->arg = arg;
28         if (!add_listener(l)) {
29                 free(l);
30                 return NULL;
31         }
32         return l;
33 }
34
35 void io_close_listener(struct io_listener *l)
36 {
37         close(l->fd.fd);
38         del_listener(l);
39         free(l);
40 }
41
42 struct io_conn *io_new_conn_(int fd,
43                              struct io_plan plan,
44                              void (*finish)(struct io_conn *, void *),
45                              void *arg)
46 {
47         struct io_conn *conn = malloc(sizeof(*conn));
48
49         if (!conn)
50                 return NULL;
51
52         conn->fd.listener = false;
53         conn->fd.fd = fd;
54         conn->plan = plan;
55         conn->finish = finish;
56         conn->finish_arg = arg;
57         conn->duplex = NULL;
58         conn->timeout = NULL;
59         if (!add_conn(conn)) {
60                 free(conn);
61                 return NULL;
62         }
63         return conn;
64 }
65
66 struct io_conn *io_duplex_(struct io_conn *old,
67                            struct io_plan plan,
68                            void (*finish)(struct io_conn *, void *),
69                            void *arg)
70 {
71         struct io_conn *conn;
72
73         assert(!old->duplex);
74
75         conn = malloc(sizeof(*conn));
76         if (!conn)
77                 return NULL;
78
79         conn->fd.listener = false;
80         conn->fd.fd = old->fd.fd;
81         conn->plan = plan;
82         conn->duplex = old;
83         conn->finish = finish;
84         conn->finish_arg = arg;
85         conn->timeout = NULL;
86         if (!add_duplex(conn)) {
87                 free(conn);
88                 return NULL;
89         }
90         old->duplex = conn;
91         return conn;
92 }
93
94 bool io_timeout_(struct io_conn *conn, struct timespec ts,
95                  struct io_plan (*cb)(struct io_conn *, void *), void *arg)
96 {
97         assert(cb);
98
99         if (!conn->timeout) {
100                 conn->timeout = malloc(sizeof(*conn->timeout));
101                 if (!conn->timeout)
102                         return false;
103         } else
104                 assert(!timeout_active(conn));
105
106         conn->timeout->next = cb;
107         conn->timeout->next_arg = arg;
108         backend_add_timeout(conn, ts);
109         return true;
110 }
111
112 /* Returns true if we're finished. */
113 static bool do_write(int fd, struct io_plan *plan)
114 {
115         ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
116         if (ret < 0) {
117                 /* Override next function to close us. */
118                 plan->next = io_close;
119                 return true;
120         }
121
122         plan->u.write.buf += ret;
123         plan->u.write.len -= ret;
124         return (plan->u.write.len == 0);
125 }
126
127 /* Queue some data to be written. */
128 struct io_plan io_write_(const void *data, size_t len,
129                          struct io_plan (*cb)(struct io_conn *, void *),
130                          void *arg)
131 {
132         struct io_plan plan;
133
134         assert(cb);
135         plan.u.write.buf = data;
136         plan.u.write.len = len;
137         plan.io = do_write;
138         plan.next = cb;
139         plan.next_arg = arg;
140         plan.pollflag = POLLOUT;
141         return plan;
142 }
143
144 static bool do_read(int fd, struct io_plan *plan)
145 {
146         ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
147         if (ret <= 0) {
148                 /* Override next function to close us. */
149                 plan->next = io_close;
150                 return true;
151         }
152
153         plan->u.read.buf += ret;
154         plan->u.read.len -= ret;
155         return (plan->u.read.len == 0);
156 }
157
158 /* Queue a request to read into a buffer. */
159 struct io_plan io_read_(void *data, size_t len,
160                         struct io_plan (*cb)(struct io_conn *, void *),
161                         void *arg)
162 {
163         struct io_plan plan;
164
165         assert(cb);
166         plan.u.read.buf = data;
167         plan.u.read.len = len;
168         plan.io = do_read;
169         plan.next = cb;
170         plan.next_arg = arg;
171         plan.pollflag = POLLIN;
172         return plan;
173 }
174
175 static bool do_read_partial(int fd, struct io_plan *plan)
176 {
177         ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
178         if (ret <= 0) {
179                 /* Override next function to close us. */
180                 plan->next = io_close;
181                 return true;
182         }
183
184         *plan->u.readpart.lenp = ret;
185         return true;
186 }
187
188 /* Queue a partial request to read into a buffer. */
189 struct io_plan io_read_partial_(void *data, size_t *len,
190                                 struct io_plan (*cb)(struct io_conn *, void *),
191                                 void *arg)
192 {
193         struct io_plan plan;
194
195         assert(cb);
196         plan.u.readpart.buf = data;
197         plan.u.readpart.lenp = len;
198         plan.io = do_read_partial;
199         plan.next = cb;
200         plan.next_arg = arg;
201         plan.pollflag = POLLIN;
202
203         return plan;
204 }
205
206 static bool do_write_partial(int fd, struct io_plan *plan)
207 {
208         ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
209         if (ret < 0) {
210                 /* Override next function to close us. */
211                 plan->next = io_close;
212                 return true;
213         }
214
215         *plan->u.writepart.lenp = ret;
216         return true;
217 }
218
219 /* Queue a partial write request. */
220 struct io_plan io_write_partial_(const void *data, size_t *len,
221                                  struct io_plan (*cb)(struct io_conn*, void *),
222                                  void *arg)
223 {
224         struct io_plan plan;
225
226         assert(cb);
227         plan.u.writepart.buf = data;
228         plan.u.writepart.lenp = len;
229         plan.io = do_write_partial;
230         plan.next = cb;
231         plan.next_arg = arg;
232         plan.pollflag = POLLOUT;
233
234         return plan;
235 }
236
237 struct io_plan io_idle(void)
238 {
239         struct io_plan plan;
240
241         plan.pollflag = 0;
242         plan.io = NULL;
243         /* Never called (overridded by io_wake), but NULL means closing */
244         plan.next = io_close;
245
246         return plan;
247 }
248
249 void io_wake(struct io_conn *conn, struct io_plan plan)
250
251 {
252         /* It might be closing, but we haven't called its finish() yet. */
253         if (!conn->plan.next)
254                 return;
255         /* It was idle, right? */
256         assert(!conn->plan.io);
257         conn->plan = plan;
258         backend_plan_changed(conn);
259 }
260
261 void io_ready(struct io_conn *conn)
262 {
263         if (conn->plan.io(conn->fd.fd, &conn->plan)) {
264                 if (timeout_active(conn))
265                         backend_del_timeout(conn);
266                 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
267                 backend_plan_changed(conn);
268         }
269 }
270
271 /* Useful next functions. */
272 /* Close the connection, we're done. */
273 struct io_plan io_close(struct io_conn *conn, void *arg)
274 {
275         struct io_plan plan;
276
277         plan.pollflag = 0;
278         /* This means we're closing. */
279         plan.next = NULL;
280
281         return plan;
282 }
283
284 /* Exit the loop, returning this (non-NULL) arg. */
285 struct io_plan io_break(void *ret, struct io_plan plan)
286 {
287         assert(ret);
288         io_loop_return = ret;
289
290         return plan;
291 }