1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
16 /* Set to skip the next plan. */
18 /* The current connection to apply plan to. */
19 struct io_conn *current;
20 /* User-defined function to select which connection(s) to debug. */
21 bool (*io_debug_conn)(struct io_conn *conn);
22 /* Set when we wake up an connection we are debugging. */
25 struct io_plan io_debug(struct io_plan plan)
27 if (io_plan_nodebug) {
28 io_plan_nodebug = false;
32 if (!io_debug_conn || !current)
35 if (!io_debug_conn(current) && !io_debug_wakeup)
38 io_debug_wakeup = false;
40 backend_plan_changed(current);
42 /* Call back into the loop immediately. */
43 io_loop_return = io_loop();
47 static void debug_io_wake(struct io_conn *conn)
49 /* We want linear if we wake a debugged connection, too. */
50 if (io_debug_conn && io_debug_conn(conn))
51 io_debug_wakeup = true;
54 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
55 static void io_plan_debug_again(void)
57 io_plan_nodebug = false;
60 static void debug_io_wake(struct io_conn *conn)
63 static void io_plan_debug_again(void)
68 struct io_listener *io_new_listener_(int fd,
69 void (*init)(int fd, void *arg),
72 struct io_listener *l = malloc(sizeof(*l));
77 l->fd.listener = true;
81 if (!add_listener(l)) {
88 void io_close_listener(struct io_listener *l)
95 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
97 struct io_conn *conn = malloc(sizeof(*conn));
99 io_plan_debug_again();
104 conn->fd.listener = false;
108 conn->finish_arg = NULL;
110 conn->timeout = NULL;
111 if (!add_conn(conn)) {
118 void io_set_finish_(struct io_conn *conn,
119 void (*finish)(struct io_conn *, void *),
122 conn->finish = finish;
123 conn->finish_arg = arg;
126 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
128 struct io_conn *conn;
130 io_plan_debug_again();
132 assert(!old->duplex);
134 conn = malloc(sizeof(*conn));
138 conn->fd.listener = false;
139 conn->fd.fd = old->fd.fd;
143 conn->finish_arg = NULL;
144 conn->timeout = NULL;
145 if (!add_duplex(conn)) {
153 bool io_timeout_(struct io_conn *conn, struct timespec ts,
154 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
158 if (!conn->timeout) {
159 conn->timeout = malloc(sizeof(*conn->timeout));
163 assert(!timeout_active(conn));
165 conn->timeout->next = cb;
166 conn->timeout->next_arg = arg;
167 backend_add_timeout(conn, ts);
171 /* Returns true if we're finished. */
172 static int do_write(int fd, struct io_plan *plan)
174 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
178 plan->u.write.buf += ret;
179 plan->u.write.len -= ret;
180 return (plan->u.write.len == 0);
183 /* Queue some data to be written. */
184 struct io_plan io_write_(const void *data, size_t len,
185 struct io_plan (*cb)(struct io_conn *, void *),
191 plan.u.write.buf = data;
192 plan.u.write.len = len;
196 plan.pollflag = POLLOUT;
201 static int do_read(int fd, struct io_plan *plan)
203 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
207 plan->u.read.buf += ret;
208 plan->u.read.len -= ret;
209 return (plan->u.read.len == 0);
212 /* Queue a request to read into a buffer. */
213 struct io_plan io_read_(void *data, size_t len,
214 struct io_plan (*cb)(struct io_conn *, void *),
220 plan.u.read.buf = data;
221 plan.u.read.len = len;
225 plan.pollflag = POLLIN;
230 static int do_read_partial(int fd, struct io_plan *plan)
232 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
236 *plan->u.readpart.lenp = ret;
240 /* Queue a partial request to read into a buffer. */
241 struct io_plan io_read_partial_(void *data, size_t *len,
242 struct io_plan (*cb)(struct io_conn *, void *),
248 plan.u.readpart.buf = data;
249 plan.u.readpart.lenp = len;
250 plan.io = do_read_partial;
253 plan.pollflag = POLLIN;
258 static int do_write_partial(int fd, struct io_plan *plan)
260 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
264 *plan->u.writepart.lenp = ret;
268 /* Queue a partial write request. */
269 struct io_plan io_write_partial_(const void *data, size_t *len,
270 struct io_plan (*cb)(struct io_conn*, void *),
276 plan.u.writepart.buf = data;
277 plan.u.writepart.lenp = len;
278 plan.io = do_write_partial;
281 plan.pollflag = POLLOUT;
286 struct io_plan io_idle_(void)
292 /* Never called (overridden by io_wake), but NULL means closing */
293 plan.next = (void *)io_idle_;
298 void io_wake_(struct io_conn *conn, struct io_plan plan)
301 io_plan_debug_again();
303 /* It might be closing, but we haven't called its finish() yet. */
304 if (!conn->plan.next)
306 /* It was idle, right? */
307 assert(!conn->plan.io);
309 backend_plan_changed(conn);
314 void io_ready(struct io_conn *conn)
316 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
317 case -1: /* Failure means a new plan: close up. */
319 conn->plan = io_close();
320 backend_plan_changed(conn);
323 case 0: /* Keep going with plan. */
325 case 1: /* Done: get next plan. */
327 if (timeout_active(conn))
328 backend_del_timeout(conn);
329 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
330 backend_plan_changed(conn);
335 /* Close the connection, we're done. */
336 struct io_plan io_close_(void)
341 /* This means we're closing. */
343 plan.u.close.saved_errno = errno;
348 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
353 /* Exit the loop, returning this (non-NULL) arg. */
354 struct io_plan io_break_(void *ret, struct io_plan plan)
356 io_plan_debug_again();
359 io_loop_return = ret;