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_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;
62 if (!add_conn(conn)) {
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 *),
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 /* Convenient token which only we can produce. */
99 static inline struct io_next *to_ionext(struct io_conn *conn)
101 return (struct io_next *)conn;
104 static inline struct io_op *to_ioop(enum io_state state)
106 return (struct io_op *)(long)state;
109 static inline struct io_conn *from_ionext(struct io_next *next)
111 return (struct io_conn *)next;
114 struct io_next *io_next_(struct io_conn *conn,
115 struct io_op *(*next)(struct io_conn *, void *),
118 conn->fd.next = next;
119 conn->fd.next_arg = arg;
121 return to_ionext(conn);
124 bool io_timeout_(struct io_conn *conn, struct timespec ts,
125 struct io_op *(*next)(struct io_conn *, void *), void *arg)
127 if (!conn->timeout) {
128 conn->timeout = malloc(sizeof(*conn->timeout));
132 assert(!timeout_active(conn));
134 conn->timeout->next = next;
135 conn->timeout->next_arg = arg;
136 backend_add_timeout(conn, ts);
140 /* Queue some data to be written. */
141 struct io_op *io_write(const void *data, size_t len, struct io_next *next)
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);
149 /* Queue a request to read into a buffer. */
150 struct io_op *io_read(void *data, size_t len, struct io_next *next)
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);
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)
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);
167 /* Queue a partial write request. */
168 struct io_op *io_write_partial(const void *data, size_t *len, struct io_next *next)
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);
176 struct io_op *io_idle(struct io_conn *conn)
178 return to_ioop(IDLE);
181 void io_wake_(struct io_conn *conn,
182 struct io_op *(*next)(struct io_conn *, void *), void *arg)
185 /* It might have finished, but we haven't called its finish() yet. */
186 if (conn->state == FINISHED)
188 assert(conn->state == IDLE);
189 conn->fd.next = next;
190 conn->fd.next_arg = arg;
191 backend_set_state(conn, to_ioop(NEXT));
194 static struct io_op *do_next(struct io_conn *conn)
196 if (timeout_active(conn))
197 backend_del_timeout(conn);
198 return conn->fd.next(conn, conn->fd.next_arg);
201 struct io_op *do_ready(struct io_conn *conn)
206 switch (conn->state) {
208 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
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);
216 ret = write(conn->fd.fd, conn->u.writepart.buf,
217 *conn->u.writepart.lenp);
219 return io_close(conn, NULL);
220 *conn->u.writepart.lenp = ret;
224 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
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);
232 ret = read(conn->fd.fd, conn->u.readpart.buf,
233 *conn->u.readpart.lenp);
235 return io_close(conn, NULL);
236 *conn->u.readpart.lenp = ret;
240 /* Shouldn't happen. */
245 return do_next(conn);
246 return to_ioop(conn->state);
249 /* Useful next functions. */
250 /* Close the connection, we're done. */
251 struct io_op *io_close(struct io_conn *conn, void *arg)
253 return to_ioop(FINISHED);
256 /* Exit the loop, returning this (non-NULL) arg. */
257 struct io_op *io_break(void *arg, struct io_next *next)
259 io_loop_return = arg;
261 return to_ioop(NEXT);