]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
84c2ccad0c2c0ac032201ab6fb8bce44a05f1a23
[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
12 void *io_loop_return;
13
14 struct io_listener *io_new_listener_(int fd,
15                                      struct io_op *(*start)(struct io_conn *,
16                                                             void *arg),
17                                      void (*finish)(struct io_conn *, void *),
18                                      void *arg)
19 {
20         struct io_listener *l = malloc(sizeof(*l));
21
22         if (!l)
23                 return NULL;
24
25         l->fd.listener = true;
26         l->fd.fd = fd;
27         l->fd.next = start;
28         l->fd.finish = finish;
29         l->fd.finish_arg = l->fd.next_arg = arg;
30         if (!add_listener(l)) {
31                 free(l);
32                 return NULL;
33         }
34         return l;
35 }
36
37 void io_close_listener(struct io_listener *l)
38 {
39         close(l->fd.fd);
40         del_listener(l);
41         free(l);
42 }
43
44 struct io_conn *io_new_conn_(int fd,
45                              struct io_op *(*start)(struct io_conn *, void *),
46                              void (*finish)(struct io_conn *, void *),
47                              void *arg)
48 {
49         struct io_conn *conn = malloc(sizeof(*conn));
50
51         if (!conn)
52                 return NULL;
53
54         conn->fd.listener = false;
55         conn->fd.fd = fd;
56         conn->fd.next = start;
57         conn->fd.finish = finish;
58         conn->fd.finish_arg = conn->fd.next_arg = arg;
59         conn->state = NEXT;
60         conn->duplex = NULL;
61         conn->timeout = NULL;
62         if (!add_conn(conn)) {
63                 free(conn);
64                 return NULL;
65         }
66         return conn;
67 }
68
69 struct io_conn *io_duplex_(struct io_conn *old,
70                              struct io_op *(*start)(struct io_conn *, void *),
71                              void (*finish)(struct io_conn *, void *),
72                              void *arg)
73 {
74         struct io_conn *conn;
75
76         assert(!old->duplex);
77
78         conn = malloc(sizeof(*conn));
79         if (!conn)
80                 return NULL;
81
82         conn->fd.listener = false;
83         conn->fd.fd = old->fd.fd;
84         conn->fd.next = start;
85         conn->fd.finish = finish;
86         conn->fd.finish_arg = conn->fd.next_arg = arg;
87         conn->state = NEXT;
88         conn->duplex = old;
89         conn->timeout = NULL;
90         if (!add_duplex(conn)) {
91                 free(conn);
92                 return NULL;
93         }
94         old->duplex = conn;
95         return conn;
96 }
97
98 /* Convenient token which only we can produce. */
99 static inline struct io_next *to_ionext(struct io_conn *conn)
100 {
101         return (struct io_next *)conn;
102 }
103
104 static inline struct io_op *to_ioop(enum io_state state)
105 {
106         return (struct io_op *)(long)state;
107 }
108
109 static inline struct io_conn *from_ionext(struct io_next *next)
110 {
111         return (struct io_conn *)next;
112 }
113
114 struct io_next *io_next_(struct io_conn *conn,
115                          struct io_op *(*next)(struct io_conn *, void *),
116                          void *arg)
117 {
118         conn->fd.next = next;
119         conn->fd.next_arg = arg;
120
121         return to_ionext(conn);
122 }
123
124 bool io_timeout_(struct io_conn *conn, struct timespec ts,
125                  struct io_op *(*next)(struct io_conn *, void *), void *arg)
126 {
127         if (!conn->timeout) {
128                 conn->timeout = malloc(sizeof(*conn->timeout));
129                 if (!conn->timeout)
130                         return false;
131         } else
132                 assert(!timeout_active(conn));
133
134         conn->timeout->next = next;
135         conn->timeout->next_arg = arg;
136         backend_add_timeout(conn, ts);
137         return true;
138 }
139
140 /* Queue some data to be written. */
141 struct io_op *io_write(const void *data, size_t len, struct io_next *next)
142 {
143         struct io_conn *conn = from_ionext(next);
144         conn->u.write.buf = data;
145         conn->u.write.len = len;
146         return to_ioop(WRITE);
147 }
148
149 /* Queue a request to read into a buffer. */
150 struct io_op *io_read(void *data, size_t len, struct io_next *next)
151 {
152         struct io_conn *conn = from_ionext(next);
153         conn->u.read.buf = data;
154         conn->u.read.len = len;
155         return to_ioop(READ);
156 }
157
158 /* Queue a partial request to read into a buffer. */
159 struct io_op *io_read_partial(void *data, size_t *len, struct io_next *next)
160 {
161         struct io_conn *conn = from_ionext(next);
162         conn->u.readpart.buf = data;
163         conn->u.readpart.lenp = len;
164         return to_ioop(READPART);
165 }
166
167 /* Queue a partial write request. */
168 struct io_op *io_write_partial(const void *data, size_t *len, struct io_next *next)
169 {
170         struct io_conn *conn = from_ionext(next);
171         conn->u.writepart.buf = data;
172         conn->u.writepart.lenp = len;
173         return to_ioop(WRITEPART);
174 }
175
176 struct io_op *io_idle(struct io_conn *conn)
177 {
178         return to_ioop(IDLE);
179 }
180
181 void io_wake_(struct io_conn *conn,
182               struct io_op *(*next)(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->fd.next = next;
190         conn->fd.next_arg = arg;
191         backend_set_state(conn, to_ioop(NEXT));
192 }
193
194 static struct io_op *do_next(struct io_conn *conn)
195 {
196         if (timeout_active(conn))
197                 backend_del_timeout(conn);
198         return conn->fd.next(conn, conn->fd.next_arg);
199 }
200
201 struct io_op *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_ioop(conn->state);
247 }
248
249 /* Useful next functions. */
250 /* Close the connection, we're done. */
251 struct io_op *io_close(struct io_conn *conn, void *arg)
252 {
253         return to_ioop(FINISHED);
254 }
255
256 /* Exit the loop, returning this (non-NULL) arg. */
257 struct io_op *io_break(void *arg, struct io_next *next)
258 {
259         io_loop_return = arg;
260
261         return to_ioop(NEXT);
262 }