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