]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ec4d4fabbc19df27e67059c652787df4e37f99eb
[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_plan *(*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_plan *(*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_plan *(*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 static inline struct io_plan *to_ioplan(enum io_state state)
99 {
100         return (struct io_plan *)(long)state;
101 }
102
103 bool io_timeout_(struct io_conn *conn, struct timespec ts,
104                  struct io_plan *(*cb)(struct io_conn *, void *), void *arg)
105 {
106         if (!conn->timeout) {
107                 conn->timeout = malloc(sizeof(*conn->timeout));
108                 if (!conn->timeout)
109                         return false;
110         } else
111                 assert(!timeout_active(conn));
112
113         conn->timeout->next = cb;
114         conn->timeout->next_arg = arg;
115         backend_add_timeout(conn, ts);
116         return true;
117 }
118
119 /* Queue some data to be written. */
120 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
121                           struct io_plan *(*cb)(struct io_conn *, void *),
122                           void *arg)
123 {
124         conn->u.write.buf = data;
125         conn->u.write.len = len;
126         conn->fd.next = cb;
127         conn->fd.next_arg = arg;
128         return to_ioplan(WRITE);
129 }
130
131 /* Queue a request to read into a buffer. */
132 struct io_plan *io_read_(struct io_conn *conn, void *data, size_t len,
133                          struct io_plan *(*cb)(struct io_conn *, void *),
134                          void *arg)
135 {
136         conn->u.read.buf = data;
137         conn->u.read.len = len;
138         conn->fd.next = cb;
139         conn->fd.next_arg = arg;
140         return to_ioplan(READ);
141 }
142
143 /* Queue a partial request to read into a buffer. */
144 struct io_plan *io_read_partial_(struct io_conn *conn, void *data, size_t *len,
145                                  struct io_plan *(*cb)(struct io_conn *, void *),
146                                  void *arg)
147 {
148         conn->u.readpart.buf = data;
149         conn->u.readpart.lenp = len;
150         conn->fd.next = cb;
151         conn->fd.next_arg = arg;
152         return to_ioplan(READPART);
153 }
154
155 /* Queue a partial write request. */
156 struct io_plan *io_write_partial_(struct io_conn *conn,
157                                   const void *data, size_t *len,
158                                   struct io_plan *(*cb)(struct io_conn*, void *),
159                                   void *arg)
160 {
161         conn->u.writepart.buf = data;
162         conn->u.writepart.lenp = len;
163         conn->fd.next = cb;
164         conn->fd.next_arg = arg;
165         return to_ioplan(WRITEPART);
166 }
167
168 struct io_plan *io_idle(struct io_conn *conn)
169 {
170         return to_ioplan(IDLE);
171 }
172
173 void io_wake_(struct io_conn *conn,
174               struct io_plan *(*fn)(struct io_conn *, void *), void *arg)
175
176 {
177         /* It might have finished, but we haven't called its finish() yet. */
178         if (conn->state == FINISHED)
179                 return;
180         assert(conn->state == IDLE);
181         conn->fd.next = fn;
182         conn->fd.next_arg = arg;
183         backend_set_state(conn, to_ioplan(NEXT));
184 }
185
186 static struct io_plan *do_next(struct io_conn *conn)
187 {
188         if (timeout_active(conn))
189                 backend_del_timeout(conn);
190         return conn->fd.next(conn, conn->fd.next_arg);
191 }
192
193 struct io_plan *do_ready(struct io_conn *conn)
194 {
195         ssize_t ret;
196         bool finished;
197
198         switch (conn->state) {
199         case WRITE:
200                 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
201                 if (ret < 0)
202                         return io_close(conn, NULL);
203                 conn->u.write.buf += ret;
204                 conn->u.write.len -= ret;
205                 finished = (conn->u.write.len == 0);
206                 break;
207         case WRITEPART:
208                 ret = write(conn->fd.fd, conn->u.writepart.buf,
209                             *conn->u.writepart.lenp);
210                 if (ret < 0)
211                         return io_close(conn, NULL);
212                 *conn->u.writepart.lenp = ret;
213                 finished = true;
214                 break;
215         case READ:
216                 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
217                 if (ret <= 0)
218                         return io_close(conn, NULL);
219                 conn->u.read.buf += ret;
220                 conn->u.read.len -= ret;
221                 finished = (conn->u.read.len == 0);
222                 break;
223         case READPART:
224                 ret = read(conn->fd.fd, conn->u.readpart.buf,
225                             *conn->u.readpart.lenp);
226                 if (ret <= 0)
227                         return io_close(conn, NULL);
228                 *conn->u.readpart.lenp = ret;
229                 finished = true;
230                 break;
231         default:
232                 /* Shouldn't happen. */
233                 abort();
234         }
235
236         if (finished)
237                 return do_next(conn);
238         return to_ioplan(conn->state);
239 }
240
241 /* Useful next functions. */
242 /* Close the connection, we're done. */
243 struct io_plan *io_close(struct io_conn *conn, void *arg)
244 {
245         return to_ioplan(FINISHED);
246 }
247
248 /* Exit the loop, returning this (non-NULL) arg. */
249 struct io_plan *io_break_(struct io_conn *conn, void *ret,
250                           struct io_plan *(*fn)(struct io_conn *, void *),
251                           void *arg)
252 {
253         io_loop_return = ret;
254         conn->fd.next = fn;
255         conn->fd.next_arg = arg;
256
257         return to_ioplan(NEXT);
258 }