1 /* Licensed under BSD-MIT - see LICENSE file for details */
5 #include <sys/socket.h>
14 struct io_listener *io_new_listener_(int fd,
15 struct io_op *(*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_op *(*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;
61 if (!add_conn(conn)) {
68 struct io_conn *io_duplex_(struct io_conn *old,
69 struct io_op *(*start)(struct io_conn *, void *),
70 void (*finish)(struct io_conn *, void *),
77 conn = malloc(sizeof(*conn));
81 conn->fd.listener = false;
82 conn->fd.fd = old->fd.fd;
83 conn->fd.next = start;
84 conn->fd.finish = finish;
85 conn->fd.finish_arg = conn->fd.next_arg = arg;
88 if (!add_duplex(conn)) {
96 /* Convenient token which only we can produce. */
97 static inline struct io_next *to_ionext(struct io_conn *conn)
99 return (struct io_next *)conn;
102 static inline struct io_op *to_ioop(enum io_state state)
104 return (struct io_op *)(long)state;
107 static inline struct io_conn *from_ionext(struct io_next *next)
109 return (struct io_conn *)next;
112 struct io_next *io_next_(struct io_conn *conn,
113 struct io_op *(*next)(struct io_conn *, void *),
116 conn->fd.next = next;
117 conn->fd.next_arg = arg;
119 return to_ionext(conn);
122 /* Queue some data to be written. */
123 struct io_op *io_write(const void *data, size_t len, struct io_next *next)
125 struct io_conn *conn = from_ionext(next);
126 conn->u.write.buf = data;
127 conn->u.write.len = len;
128 return to_ioop(WRITE);
131 /* Queue a request to read into a buffer. */
132 struct io_op *io_read(void *data, size_t len, struct io_next *next)
134 struct io_conn *conn = from_ionext(next);
135 conn->u.read.buf = data;
136 conn->u.read.len = len;
137 return to_ioop(READ);
140 /* Queue a partial request to read into a buffer. */
141 struct io_op *io_read_partial(void *data, size_t *len, struct io_next *next)
143 struct io_conn *conn = from_ionext(next);
144 conn->u.readpart.buf = data;
145 conn->u.readpart.lenp = len;
146 return to_ioop(READPART);
149 /* Queue a partial write request. */
150 struct io_op *io_write_partial(const void *data, size_t *len, struct io_next *next)
152 struct io_conn *conn = from_ionext(next);
153 conn->u.writepart.buf = data;
154 conn->u.writepart.lenp = len;
155 return to_ioop(WRITEPART);
158 struct io_op *io_idle(struct io_conn *conn)
160 return to_ioop(IDLE);
163 void io_wake_(struct io_conn *conn,
164 struct io_op *(*next)(struct io_conn *, void *), void *arg)
167 /* It might have finished, but we haven't called its finish() yet. */
168 if (conn->state == FINISHED)
170 assert(conn->state == IDLE);
171 conn->fd.next = next;
172 conn->fd.next_arg = arg;
173 backend_set_state(conn, to_ioop(NEXT));
176 static struct io_op *do_next(struct io_conn *conn)
178 return conn->fd.next(conn, conn->fd.next_arg);
181 struct io_op *do_ready(struct io_conn *conn)
186 switch (conn->state) {
188 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
190 return io_close(conn, NULL);
191 conn->u.write.buf += ret;
192 conn->u.write.len -= ret;
193 finished = (conn->u.write.len == 0);
196 ret = write(conn->fd.fd, conn->u.writepart.buf,
197 *conn->u.writepart.lenp);
199 return io_close(conn, NULL);
200 *conn->u.writepart.lenp = ret;
204 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
206 return io_close(conn, NULL);
207 conn->u.read.buf += ret;
208 conn->u.read.len -= ret;
209 finished = (conn->u.read.len == 0);
212 ret = read(conn->fd.fd, conn->u.readpart.buf,
213 *conn->u.readpart.lenp);
215 return io_close(conn, NULL);
216 *conn->u.readpart.lenp = ret;
220 /* Shouldn't happen. */
225 return do_next(conn);
226 return to_ioop(conn->state);
229 /* Useful next functions. */
230 /* Close the connection, we're done. */
231 struct io_op *io_close(struct io_conn *conn, void *arg)
233 return to_ioop(FINISHED);
236 /* Exit the loop, returning this (non-NULL) arg. */
237 struct io_op *io_break(void *arg, struct io_next *next)
239 io_loop_return = arg;
241 return to_ioop(NEXT);