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 void (*init)(int fd, void *arg),
19 struct io_listener *l = malloc(sizeof(*l));
24 l->fd.listener = true;
28 if (!add_listener(l)) {
35 void io_close_listener(struct io_listener *l)
42 struct io_conn *io_new_conn_(int fd,
44 void (*finish)(struct io_conn *, void *),
47 struct io_conn *conn = malloc(sizeof(*conn));
52 conn->fd.listener = false;
55 conn->finish = finish;
56 conn->finish_arg = arg;
59 if (!add_conn(conn)) {
66 struct io_conn *io_duplex_(struct io_conn *old,
68 void (*finish)(struct io_conn *, void *),
75 conn = malloc(sizeof(*conn));
79 conn->fd.listener = false;
80 conn->fd.fd = old->fd.fd;
83 conn->finish = finish;
84 conn->finish_arg = arg;
86 if (!add_duplex(conn)) {
94 bool io_timeout_(struct io_conn *conn, struct timespec ts,
95 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
100 conn->timeout = malloc(sizeof(*conn->timeout));
104 assert(!timeout_active(conn));
106 conn->timeout->next = cb;
107 conn->timeout->next_arg = arg;
108 backend_add_timeout(conn, ts);
112 static enum io_result do_write(struct io_conn *conn)
114 ssize_t ret = write(conn->fd.fd, conn->plan.u.write.buf, conn->plan.u.write.len);
118 conn->plan.u.write.buf += ret;
119 conn->plan.u.write.len -= ret;
120 if (conn->plan.u.write.len == 0)
121 return RESULT_FINISHED;
126 /* Queue some data to be written. */
127 struct io_plan io_write_(const void *data, size_t len,
128 struct io_plan (*cb)(struct io_conn *, void *),
134 plan.u.write.buf = data;
135 plan.u.write.len = len;
139 plan.pollflag = POLLOUT;
143 static enum io_result do_read(struct io_conn *conn)
145 ssize_t ret = read(conn->fd.fd, conn->plan.u.read.buf,
146 conn->plan.u.read.len);
149 conn->plan.u.read.buf += ret;
150 conn->plan.u.read.len -= ret;
151 if (conn->plan.u.read.len == 0)
152 return RESULT_FINISHED;
157 /* Queue a request to read into a buffer. */
158 struct io_plan io_read_(void *data, size_t len,
159 struct io_plan (*cb)(struct io_conn *, void *),
165 plan.u.read.buf = data;
166 plan.u.read.len = len;
170 plan.pollflag = POLLIN;
174 static enum io_result do_read_partial(struct io_conn *conn)
176 ssize_t ret = read(conn->fd.fd, conn->plan.u.readpart.buf,
177 *conn->plan.u.readpart.lenp);
180 *conn->plan.u.readpart.lenp = ret;
181 return RESULT_FINISHED;
184 /* Queue a partial request to read into a buffer. */
185 struct io_plan io_read_partial_(void *data, size_t *len,
186 struct io_plan (*cb)(struct io_conn *, void *),
192 plan.u.readpart.buf = data;
193 plan.u.readpart.lenp = len;
194 plan.io = do_read_partial;
197 plan.pollflag = POLLIN;
202 static enum io_result do_write_partial(struct io_conn *conn)
204 ssize_t ret = write(conn->fd.fd, conn->plan.u.writepart.buf,
205 *conn->plan.u.writepart.lenp);
208 *conn->plan.u.writepart.lenp = ret;
209 return RESULT_FINISHED;
212 /* Queue a partial write request. */
213 struct io_plan io_write_partial_(const void *data, size_t *len,
214 struct io_plan (*cb)(struct io_conn*, void *),
220 plan.u.writepart.buf = data;
221 plan.u.writepart.lenp = len;
222 plan.io = do_write_partial;
225 plan.pollflag = POLLOUT;
230 struct io_plan io_idle(void)
236 /* Never called (overridded by io_wake), but NULL means closing */
237 plan.next = io_close;
242 void io_wake(struct io_conn *conn, struct io_plan plan)
245 /* It might be closing, but we haven't called its finish() yet. */
246 if (!conn->plan.next)
248 /* It was idle, right? */
249 assert(!conn->plan.io);
251 backend_wakeup(conn);
254 static struct io_plan do_next(struct io_conn *conn)
256 if (timeout_active(conn))
257 backend_del_timeout(conn);
258 return conn->plan.next(conn, conn->plan.next_arg);
261 struct io_plan do_ready(struct io_conn *conn)
263 switch (conn->plan.io(conn)) {
265 return io_close(conn, NULL);
266 case RESULT_FINISHED:
267 return do_next(conn);
275 /* Useful next functions. */
276 /* Close the connection, we're done. */
277 struct io_plan io_close(struct io_conn *conn, void *arg)
282 /* This means we're closing. */
288 /* Exit the loop, returning this (non-NULL) arg. */
289 struct io_plan io_break(void *ret, struct io_plan plan)
292 io_loop_return = ret;