1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
14 struct io_listener *io_new_listener_(int fd,
15 struct io_plan *(*start)(struct io_conn *,
17 void (*finish)(struct io_conn *, void *),
20 struct io_listener *l = malloc(sizeof(*l));
25 l->fd.listener = true;
28 l->fd.finish = finish;
29 l->fd.finish_arg = l->fd.next_arg = arg;
30 if (!add_listener(l)) {
37 void io_close_listener(struct io_listener *l)
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 *),
49 struct io_conn *conn = malloc(sizeof(*conn));
54 conn->fd.listener = false;
56 conn->fd.next = start;
57 conn->fd.finish = finish;
58 conn->fd.finish_arg = conn->fd.next_arg = arg;
62 if (!add_conn(conn)) {
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 *),
78 conn = malloc(sizeof(*conn));
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;
90 if (!add_duplex(conn)) {
98 static inline struct io_plan *to_ioplan(enum io_state state)
100 return (struct io_plan *)(long)state;
103 bool io_timeout_(struct io_conn *conn, struct timespec ts,
104 struct io_plan *(*cb)(struct io_conn *, void *), void *arg)
106 if (!conn->timeout) {
107 conn->timeout = malloc(sizeof(*conn->timeout));
111 assert(!timeout_active(conn));
113 conn->timeout->next = cb;
114 conn->timeout->next_arg = arg;
115 backend_add_timeout(conn, ts);
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 *),
124 conn->u.write.buf = data;
125 conn->u.write.len = len;
127 conn->fd.next_arg = arg;
128 return to_ioplan(WRITE);
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 *),
136 conn->u.read.buf = data;
137 conn->u.read.len = len;
139 conn->fd.next_arg = arg;
140 return to_ioplan(READ);
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 *),
148 conn->u.readpart.buf = data;
149 conn->u.readpart.lenp = len;
151 conn->fd.next_arg = arg;
152 return to_ioplan(READPART);
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 *),
161 conn->u.writepart.buf = data;
162 conn->u.writepart.lenp = len;
164 conn->fd.next_arg = arg;
165 return to_ioplan(WRITEPART);
168 struct io_plan *io_idle(struct io_conn *conn)
170 return to_ioplan(IDLE);
173 void io_wake_(struct io_conn *conn,
174 struct io_plan *(*fn)(struct io_conn *, void *), void *arg)
177 /* It might have finished, but we haven't called its finish() yet. */
178 if (conn->state == FINISHED)
180 assert(conn->state == IDLE);
182 conn->fd.next_arg = arg;
183 backend_set_state(conn, to_ioplan(NEXT));
186 static struct io_plan *do_next(struct io_conn *conn)
188 if (timeout_active(conn))
189 backend_del_timeout(conn);
190 return conn->fd.next(conn, conn->fd.next_arg);
193 struct io_plan *do_ready(struct io_conn *conn)
198 switch (conn->state) {
200 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
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);
208 ret = write(conn->fd.fd, conn->u.writepart.buf,
209 *conn->u.writepart.lenp);
211 return io_close(conn, NULL);
212 *conn->u.writepart.lenp = ret;
216 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
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);
224 ret = read(conn->fd.fd, conn->u.readpart.buf,
225 *conn->u.readpart.lenp);
227 return io_close(conn, NULL);
228 *conn->u.readpart.lenp = ret;
232 /* Shouldn't happen. */
237 return do_next(conn);
238 return to_ioplan(conn->state);
241 /* Useful next functions. */
242 /* Close the connection, we're done. */
243 struct io_plan *io_close(struct io_conn *conn, void *arg)
245 return to_ioplan(FINISHED);
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 *),
253 io_loop_return = ret;
255 conn->fd.next_arg = arg;
257 return to_ioplan(NEXT);