]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: remove IO_NEXT state.
[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         if (!conn->timeout) {
98                 conn->timeout = malloc(sizeof(*conn->timeout));
99                 if (!conn->timeout)
100                         return false;
101         } else
102                 assert(!timeout_active(conn));
103
104         conn->timeout->next = cb;
105         conn->timeout->next_arg = arg;
106         backend_add_timeout(conn, ts);
107         return true;
108 }
109
110 static enum io_result do_write(struct io_conn *conn)
111 {
112         ssize_t ret = write(conn->fd.fd, conn->plan.u.write.buf, conn->plan.u.write.len);
113         if (ret < 0)
114                 return RESULT_CLOSE;
115
116         conn->plan.u.write.buf += ret;
117         conn->plan.u.write.len -= ret;
118         if (conn->plan.u.write.len == 0)
119                 return RESULT_FINISHED;
120         else
121                 return RESULT_AGAIN;
122 }
123
124 /* Queue some data to be written. */
125 struct io_plan io_write_(const void *data, size_t len,
126                          struct io_plan (*cb)(struct io_conn *, void *),
127                          void *arg)
128 {
129         struct io_plan plan;
130
131         plan.u.write.buf = data;
132         plan.u.write.len = len;
133         plan.io = do_write;
134         plan.next = cb;
135         plan.next_arg = arg;
136         plan.pollflag = POLLOUT;
137         plan.state = IO_IO;
138         return plan;
139 }
140
141 static enum io_result do_read(struct io_conn *conn)
142 {
143         ssize_t ret = read(conn->fd.fd, conn->plan.u.read.buf,
144                            conn->plan.u.read.len);
145         if (ret <= 0)
146                 return RESULT_CLOSE;
147         conn->plan.u.read.buf += ret;
148         conn->plan.u.read.len -= ret;
149         if (conn->plan.u.read.len == 0)
150                 return RESULT_FINISHED;
151         else
152                 return RESULT_AGAIN;
153 }
154
155 /* Queue a request to read into a buffer. */
156 struct io_plan io_read_(void *data, size_t len,
157                         struct io_plan (*cb)(struct io_conn *, void *),
158                         void *arg)
159 {
160         struct io_plan plan;
161
162         plan.u.read.buf = data;
163         plan.u.read.len = len;
164         plan.io = do_read;
165         plan.next = cb;
166         plan.next_arg = arg;
167         plan.pollflag = POLLIN;
168         plan.state = IO_IO;
169         return plan;
170 }
171
172 static enum io_result do_read_partial(struct io_conn *conn)
173 {
174         ssize_t ret = read(conn->fd.fd, conn->plan.u.readpart.buf,
175                            *conn->plan.u.readpart.lenp);
176         if (ret <= 0)
177                 return RESULT_CLOSE;
178         *conn->plan.u.readpart.lenp = ret;
179         return RESULT_FINISHED;
180 }
181
182 /* Queue a partial request to read into a buffer. */
183 struct io_plan io_read_partial_(void *data, size_t *len,
184                                 struct io_plan (*cb)(struct io_conn *, void *),
185                                 void *arg)
186 {
187         struct io_plan plan;
188
189         plan.u.readpart.buf = data;
190         plan.u.readpart.lenp = len;
191         plan.io = do_read_partial;
192         plan.next = cb;
193         plan.next_arg = arg;
194         plan.pollflag = POLLIN;
195         plan.state = IO_IO;
196
197         return plan;
198 }
199
200 static enum io_result do_write_partial(struct io_conn *conn)
201 {
202         ssize_t ret = write(conn->fd.fd, conn->plan.u.writepart.buf,
203                             *conn->plan.u.writepart.lenp);
204         if (ret < 0)
205                 return RESULT_CLOSE;
206         *conn->plan.u.writepart.lenp = ret;
207         return RESULT_FINISHED;
208 }
209
210 /* Queue a partial write request. */
211 struct io_plan io_write_partial_(const void *data, size_t *len,
212                                  struct io_plan (*cb)(struct io_conn*, void *),
213                                  void *arg)
214 {
215         struct io_plan plan;
216
217         plan.u.writepart.buf = data;
218         plan.u.writepart.lenp = len;
219         plan.io = do_write_partial;
220         plan.next = cb;
221         plan.next_arg = arg;
222         plan.pollflag = POLLOUT;
223         plan.state = IO_IO;
224
225         return plan;
226 }
227
228 struct io_plan io_idle(void)
229 {
230         struct io_plan plan;
231
232         plan.pollflag = 0;
233         plan.state = IO_IDLE;
234
235         return plan;
236 }
237
238 void io_wake(struct io_conn *conn, struct io_plan plan)
239
240 {
241         /* It might have finished, but we haven't called its finish() yet. */
242         if (conn->plan.state == IO_FINISHED)
243                 return;
244         assert(conn->plan.state == IO_IDLE);
245         conn->plan = plan;
246         backend_wakeup(conn);
247 }
248
249 static struct io_plan do_next(struct io_conn *conn)
250 {
251         if (timeout_active(conn))
252                 backend_del_timeout(conn);
253         return conn->plan.next(conn, conn->plan.next_arg);
254 }
255
256 struct io_plan do_ready(struct io_conn *conn)
257 {
258         assert(conn->plan.state == IO_IO);
259         switch (conn->plan.io(conn)) {
260         case RESULT_CLOSE:
261                 return io_close(conn, NULL);
262         case RESULT_FINISHED:
263                 return do_next(conn);
264         case RESULT_AGAIN:
265                 return conn->plan;
266         default:
267                 abort();
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.state = IO_FINISHED;
278         plan.pollflag = 0;
279
280         return plan;
281 }
282
283 /* Exit the loop, returning this (non-NULL) arg. */
284 struct io_plan io_break(void *ret, struct io_plan plan)
285 {
286         io_loop_return = ret;
287
288         return plan;
289 }