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;
60 if (!add_conn(conn)) {
67 /* Convenient token which only we can produce. */
68 static inline struct io_next *to_ionext(struct io_conn *conn)
70 return (struct io_next *)conn;
73 static inline struct io_op *to_ioop(enum io_state state)
75 return (struct io_op *)(long)state;
78 static inline struct io_conn *from_ionext(struct io_next *next)
80 return (struct io_conn *)next;
83 struct io_next *io_next_(struct io_conn *conn,
84 struct io_op *(*next)(struct io_conn *, void *),
88 conn->fd.next_arg = arg;
90 return to_ionext(conn);
93 /* Queue some data to be written. */
94 struct io_op *io_write(const void *data, size_t len, struct io_next *next)
96 struct io_conn *conn = from_ionext(next);
97 conn->u.write.buf = data;
98 conn->u.write.len = len;
99 return to_ioop(WRITE);
102 /* Queue a request to read into a buffer. */
103 struct io_op *io_read(void *data, size_t len, struct io_next *next)
105 struct io_conn *conn = from_ionext(next);
106 conn->u.read.buf = data;
107 conn->u.read.len = len;
108 return to_ioop(READ);
111 /* Queue a partial request to read into a buffer. */
112 struct io_op *io_read_partial(void *data, size_t *len, struct io_next *next)
114 struct io_conn *conn = from_ionext(next);
115 conn->u.readpart.buf = data;
116 conn->u.readpart.lenp = len;
117 return to_ioop(READPART);
120 /* Queue a partial write request. */
121 struct io_op *io_write_partial(const void *data, size_t *len, struct io_next *next)
123 struct io_conn *conn = from_ionext(next);
124 conn->u.writepart.buf = data;
125 conn->u.writepart.lenp = len;
126 return to_ioop(WRITEPART);
129 struct io_op *io_idle(struct io_conn *conn)
131 return to_ioop(IDLE);
134 void io_wake_(struct io_conn *conn,
135 struct io_op *(*next)(struct io_conn *, void *), void *arg)
138 /* It might have finished, but we haven't called its finish() yet. */
139 if (conn->state == FINISHED)
141 assert(conn->state == IDLE);
142 conn->fd.next = next;
143 conn->fd.next_arg = arg;
144 backend_set_state(conn, to_ioop(NEXT));
147 static struct io_op *do_next(struct io_conn *conn)
149 return conn->fd.next(conn, conn->fd.next_arg);
152 struct io_op *do_writeable(struct io_conn *conn)
157 switch (conn->state) {
159 ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
161 return io_close(conn, NULL);
162 conn->u.write.buf += ret;
163 conn->u.write.len -= ret;
164 finished = (conn->u.write.len == 0);
167 ret = write(conn->fd.fd, conn->u.writepart.buf,
168 *conn->u.writepart.lenp);
170 return io_close(conn, NULL);
171 *conn->u.writepart.lenp = ret;
175 /* Shouldn't happen. */
180 return do_next(conn);
181 return to_ioop(conn->state);
184 struct io_op *do_readable(struct io_conn *conn)
189 switch (conn->state) {
191 ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
193 return io_close(conn, NULL);
194 conn->u.read.buf += ret;
195 conn->u.read.len -= ret;
196 finished = (conn->u.read.len == 0);
199 ret = read(conn->fd.fd, conn->u.readpart.buf,
200 *conn->u.readpart.lenp);
202 return io_close(conn, NULL);
203 *conn->u.readpart.lenp = ret;
207 /* Shouldn't happen. */
212 return do_next(conn);
213 return to_ioop(conn->state);
216 /* Useful next functions. */
217 /* Close the connection, we're done. */
218 struct io_op *io_close(struct io_conn *conn, void *arg)
220 return to_ioop(FINISHED);
223 /* Exit the loop, returning this (non-NULL) arg. */
224 struct io_op *io_break(void *arg, struct io_next *next)
226 io_loop_return = arg;
228 return to_ioop(NEXT);