1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
15 struct io_listener *io_new_listener_(int fd,
16 struct io_plan *(*start)(struct io_conn *,
18 void (*finish)(struct io_conn *, void *),
21 struct io_listener *l = malloc(sizeof(*l));
26 l->fd.listener = true;
31 if (!add_listener(l)) {
38 void io_close_listener(struct io_listener *l)
45 struct io_conn *io_new_conn_(int fd,
46 struct io_plan *(*start)(struct io_conn *, void *),
47 void (*finish)(struct io_conn *, void *),
50 struct io_conn *conn = malloc(sizeof(*conn));
55 conn->fd.listener = false;
58 conn->finish = finish;
59 conn->finish_arg = conn->next_arg = arg;
61 conn->state = IO_NEXT;
64 if (!add_conn(conn)) {
71 struct io_conn *io_duplex_(struct io_conn *old,
72 struct io_plan *(*start)(struct io_conn *, void *),
73 void (*finish)(struct io_conn *, void *),
80 conn = malloc(sizeof(*conn));
84 conn->fd.listener = false;
85 conn->fd.fd = old->fd.fd;
87 conn->finish = finish;
88 conn->finish_arg = conn->next_arg = arg;
90 conn->state = IO_NEXT;
93 if (!add_duplex(conn)) {
101 static inline struct io_plan *to_ioplan(enum io_state state)
103 return (struct io_plan *)(long)state;
106 bool io_timeout_(struct io_conn *conn, struct timespec ts,
107 struct io_plan *(*cb)(struct io_conn *, void *), void *arg)
109 if (!conn->timeout) {
110 conn->timeout = malloc(sizeof(*conn->timeout));
114 assert(!timeout_active(conn));
116 conn->timeout->next = cb;
117 conn->timeout->next_arg = arg;
118 backend_add_timeout(conn, ts);
122 static enum io_result do_write(struct io_conn *conn)
124 ssize_t ret = write(conn->fd.fd, conn->u.write.buf, conn->u.write.len);
128 conn->u.write.buf += ret;
129 conn->u.write.len -= ret;
130 if (conn->u.write.len == 0)
131 return RESULT_FINISHED;
136 /* Queue some data to be written. */
137 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
138 struct io_plan *(*cb)(struct io_conn *, void *),
141 conn->u.write.buf = data;
142 conn->u.write.len = len;
145 conn->next_arg = arg;
146 conn->pollflag = POLLOUT;
147 return to_ioplan(IO_IO);
150 static enum io_result do_read(struct io_conn *conn)
152 ssize_t ret = read(conn->fd.fd, conn->u.read.buf, conn->u.read.len);
155 conn->u.read.buf += ret;
156 conn->u.read.len -= ret;
157 if (conn->u.read.len == 0)
158 return RESULT_FINISHED;
163 /* Queue a request to read into a buffer. */
164 struct io_plan *io_read_(struct io_conn *conn, void *data, size_t len,
165 struct io_plan *(*cb)(struct io_conn *, void *),
168 conn->u.read.buf = data;
169 conn->u.read.len = len;
172 conn->next_arg = arg;
173 conn->pollflag = POLLIN;
174 return to_ioplan(IO_IO);
177 static enum io_result do_read_partial(struct io_conn *conn)
179 ssize_t ret = read(conn->fd.fd, conn->u.readpart.buf,
180 *conn->u.readpart.lenp);
183 *conn->u.readpart.lenp = ret;
184 return RESULT_FINISHED;
187 /* Queue a partial request to read into a buffer. */
188 struct io_plan *io_read_partial_(struct io_conn *conn, void *data, size_t *len,
189 struct io_plan *(*cb)(struct io_conn *, void *),
192 conn->u.readpart.buf = data;
193 conn->u.readpart.lenp = len;
194 conn->io = do_read_partial;
196 conn->next_arg = arg;
197 conn->pollflag = POLLIN;
198 return to_ioplan(IO_IO);
201 static enum io_result do_write_partial(struct io_conn *conn)
203 ssize_t ret = write(conn->fd.fd, conn->u.writepart.buf,
204 *conn->u.writepart.lenp);
207 *conn->u.writepart.lenp = ret;
208 return RESULT_FINISHED;
211 /* Queue a partial write request. */
212 struct io_plan *io_write_partial_(struct io_conn *conn,
213 const void *data, size_t *len,
214 struct io_plan *(*cb)(struct io_conn*, void *),
217 conn->u.writepart.buf = data;
218 conn->u.writepart.lenp = len;
219 conn->io = do_write_partial;
221 conn->next_arg = arg;
222 conn->pollflag = POLLOUT;
223 return to_ioplan(IO_IO);
226 struct io_plan *io_idle(struct io_conn *conn)
229 return to_ioplan(IO_IDLE);
232 void io_wake_(struct io_conn *conn,
233 struct io_plan *(*fn)(struct io_conn *, void *), void *arg)
236 /* It might have finished, but we haven't called its finish() yet. */
237 if (conn->state == IO_FINISHED)
239 assert(conn->state == IO_IDLE);
241 conn->next_arg = arg;
242 conn->state = IO_NEXT;
243 backend_wakeup(conn);
246 static struct io_plan *do_next(struct io_conn *conn)
248 if (timeout_active(conn))
249 backend_del_timeout(conn);
250 return conn->next(conn, conn->next_arg);
253 struct io_plan *do_ready(struct io_conn *conn)
255 assert(conn->state == IO_IO);
256 switch (conn->io(conn)) {
258 return io_close(conn, NULL);
259 case RESULT_FINISHED:
260 return do_next(conn);
262 return to_ioplan(conn->state);
268 /* Useful next functions. */
269 /* Close the connection, we're done. */
270 struct io_plan *io_close(struct io_conn *conn, void *arg)
272 return to_ioplan(IO_FINISHED);
275 /* Exit the loop, returning this (non-NULL) arg. */
276 struct io_plan *io_break_(struct io_conn *conn, void *ret,
277 struct io_plan *(*fn)(struct io_conn *, void *),
280 io_loop_return = ret;
282 conn->next_arg = arg;
284 return to_ioplan(IO_NEXT);