1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
16 bool io_plan_for_other;
17 struct io_conn *current;
18 bool (*io_debug)(struct io_conn *conn);
21 static void debug_io_plan(struct io_plan *plan)
23 if (io_plan_for_other) {
24 io_plan_for_other = false;
28 if (!io_debug || !current)
31 if (!io_debug(current) && !io_debug_wakeup)
34 io_debug_wakeup = false;
35 current->plan = *plan;
36 backend_plan_changed(current);
38 /* Call back into the loop immediately. */
39 io_loop_return = io_loop();
42 static void debug_io_wake(struct io_conn *conn)
44 /* We want linear if we wake a debugged connection, too. */
45 if (io_debug && io_debug(conn))
46 io_debug_wakeup = true;
49 static void debug_io_plan(struct io_plan *plan)
52 static void debug_io_wake(struct io_conn *conn)
57 struct io_listener *io_new_listener_(int fd,
58 void (*init)(int fd, void *arg),
61 struct io_listener *l = malloc(sizeof(*l));
66 l->fd.listener = true;
70 if (!add_listener(l)) {
77 void io_close_listener(struct io_listener *l)
84 struct io_conn *io_new_conn_(int fd,
86 void (*finish)(struct io_conn *, void *),
89 struct io_conn *conn = malloc(sizeof(*conn));
94 conn->fd.listener = false;
97 conn->finish = finish;
98 conn->finish_arg = arg;
100 conn->timeout = NULL;
101 if (!add_conn(conn)) {
108 struct io_conn *io_duplex_(struct io_conn *old,
110 void (*finish)(struct io_conn *, void *),
113 struct io_conn *conn;
115 assert(!old->duplex);
117 conn = malloc(sizeof(*conn));
121 conn->fd.listener = false;
122 conn->fd.fd = old->fd.fd;
125 conn->finish = finish;
126 conn->finish_arg = arg;
127 conn->timeout = NULL;
128 if (!add_duplex(conn)) {
136 bool io_timeout_(struct io_conn *conn, struct timespec ts,
137 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
141 if (!conn->timeout) {
142 conn->timeout = malloc(sizeof(*conn->timeout));
146 assert(!timeout_active(conn));
148 conn->timeout->next = cb;
149 conn->timeout->next_arg = arg;
150 backend_add_timeout(conn, ts);
154 /* Returns true if we're finished. */
155 static bool do_write(int fd, struct io_plan *plan)
157 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
159 /* Override next function to close us. */
160 plan->next = io_close;
164 plan->u.write.buf += ret;
165 plan->u.write.len -= ret;
166 return (plan->u.write.len == 0);
169 /* Queue some data to be written. */
170 struct io_plan io_write_(const void *data, size_t len,
171 struct io_plan (*cb)(struct io_conn *, void *),
177 plan.u.write.buf = data;
178 plan.u.write.len = len;
182 plan.pollflag = POLLOUT;
184 debug_io_plan(&plan);
188 static bool do_read(int fd, struct io_plan *plan)
190 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
192 /* Override next function to close us. */
193 plan->next = io_close;
197 plan->u.read.buf += ret;
198 plan->u.read.len -= ret;
199 return (plan->u.read.len == 0);
202 /* Queue a request to read into a buffer. */
203 struct io_plan io_read_(void *data, size_t len,
204 struct io_plan (*cb)(struct io_conn *, void *),
210 plan.u.read.buf = data;
211 plan.u.read.len = len;
215 plan.pollflag = POLLIN;
217 debug_io_plan(&plan);
221 static bool do_read_partial(int fd, struct io_plan *plan)
223 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
225 /* Override next function to close us. */
226 plan->next = io_close;
230 *plan->u.readpart.lenp = ret;
234 /* Queue a partial request to read into a buffer. */
235 struct io_plan io_read_partial_(void *data, size_t *len,
236 struct io_plan (*cb)(struct io_conn *, void *),
242 plan.u.readpart.buf = data;
243 plan.u.readpart.lenp = len;
244 plan.io = do_read_partial;
247 plan.pollflag = POLLIN;
249 debug_io_plan(&plan);
253 static bool do_write_partial(int fd, struct io_plan *plan)
255 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
257 /* Override next function to close us. */
258 plan->next = io_close;
262 *plan->u.writepart.lenp = ret;
266 /* Queue a partial write request. */
267 struct io_plan io_write_partial_(const void *data, size_t *len,
268 struct io_plan (*cb)(struct io_conn*, void *),
274 plan.u.writepart.buf = data;
275 plan.u.writepart.lenp = len;
276 plan.io = do_write_partial;
279 plan.pollflag = POLLOUT;
281 debug_io_plan(&plan);
285 struct io_plan io_idle(void)
291 /* Never called (overridded by io_wake), but NULL means closing */
292 plan.next = io_close;
294 debug_io_plan(&plan);
298 void io_wake_(struct io_conn *conn, struct io_plan plan)
301 /* It might be closing, but we haven't called its finish() yet. */
302 if (!conn->plan.next)
304 /* It was idle, right? */
305 assert(!conn->plan.io);
307 backend_plan_changed(conn);
312 void io_ready(struct io_conn *conn)
314 if (conn->plan.io(conn->fd.fd, &conn->plan)) {
316 if (timeout_active(conn))
317 backend_del_timeout(conn);
318 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
319 backend_plan_changed(conn);
324 /* Useful next functions. */
325 /* Close the connection, we're done. */
326 struct io_plan io_close(struct io_conn *conn, void *arg)
331 /* This means we're closing. */
334 debug_io_plan(&plan);
338 /* Exit the loop, returning this (non-NULL) arg. */
339 struct io_plan io_break_(void *ret, struct io_plan plan)
342 io_loop_return = ret;