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 void io_plan_debug(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_wake(struct io_conn *conn)
54 struct io_listener *io_new_listener_(int fd,
55 void (*init)(int fd, void *arg),
58 struct io_listener *l = malloc(sizeof(*l));
63 l->fd.listener = true;
67 if (!add_listener(l)) {
74 void io_close_listener(struct io_listener *l)
81 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
83 struct io_conn *conn = malloc(sizeof(*conn));
88 conn->fd.listener = false;
92 conn->finish_arg = NULL;
95 if (!add_conn(conn)) {
102 void io_set_finish_(struct io_conn *conn,
103 void (*finish)(struct io_conn *, void *),
106 conn->finish = finish;
107 conn->finish_arg = arg;
110 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
112 struct io_conn *conn;
114 assert(!old->duplex);
116 conn = malloc(sizeof(*conn));
120 conn->fd.listener = false;
121 conn->fd.fd = old->fd.fd;
125 conn->finish_arg = NULL;
126 conn->timeout = NULL;
127 if (!add_duplex(conn)) {
135 bool io_timeout_(struct io_conn *conn, struct timespec ts,
136 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
140 if (!conn->timeout) {
141 conn->timeout = malloc(sizeof(*conn->timeout));
145 assert(!timeout_active(conn));
147 conn->timeout->next = cb;
148 conn->timeout->next_arg = arg;
149 backend_add_timeout(conn, ts);
153 /* Returns true if we're finished. */
154 static int do_write(int fd, struct io_plan *plan)
156 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
160 plan->u.write.buf += ret;
161 plan->u.write.len -= ret;
162 return (plan->u.write.len == 0);
165 /* Queue some data to be written. */
166 struct io_plan io_write_(const void *data, size_t len,
167 struct io_plan (*cb)(struct io_conn *, void *),
173 plan.u.write.buf = data;
174 plan.u.write.len = len;
178 plan.pollflag = POLLOUT;
180 io_plan_debug(&plan);
184 static int do_read(int fd, struct io_plan *plan)
186 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
190 plan->u.read.buf += ret;
191 plan->u.read.len -= ret;
192 return (plan->u.read.len == 0);
195 /* Queue a request to read into a buffer. */
196 struct io_plan io_read_(void *data, size_t len,
197 struct io_plan (*cb)(struct io_conn *, void *),
203 plan.u.read.buf = data;
204 plan.u.read.len = len;
208 plan.pollflag = POLLIN;
210 io_plan_debug(&plan);
214 static int do_read_partial(int fd, struct io_plan *plan)
216 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
220 *plan->u.readpart.lenp = ret;
224 /* Queue a partial request to read into a buffer. */
225 struct io_plan io_read_partial_(void *data, size_t *len,
226 struct io_plan (*cb)(struct io_conn *, void *),
232 plan.u.readpart.buf = data;
233 plan.u.readpart.lenp = len;
234 plan.io = do_read_partial;
237 plan.pollflag = POLLIN;
239 io_plan_debug(&plan);
243 static int do_write_partial(int fd, struct io_plan *plan)
245 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
249 *plan->u.writepart.lenp = ret;
253 /* Queue a partial write request. */
254 struct io_plan io_write_partial_(const void *data, size_t *len,
255 struct io_plan (*cb)(struct io_conn*, void *),
261 plan.u.writepart.buf = data;
262 plan.u.writepart.lenp = len;
263 plan.io = do_write_partial;
266 plan.pollflag = POLLOUT;
268 io_plan_debug(&plan);
272 struct io_plan io_idle(void)
278 /* Never called (overridden by io_wake), but NULL means closing */
279 plan.next = (void *)io_idle;
281 io_plan_debug(&plan);
285 void io_wake_(struct io_conn *conn, struct io_plan plan)
288 /* It might be closing, but we haven't called its finish() yet. */
289 if (!conn->plan.next)
291 /* It was idle, right? */
292 assert(!conn->plan.io);
294 backend_plan_changed(conn);
299 void io_ready(struct io_conn *conn)
301 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
302 case -1: /* Failure means a new plan: close up. */
304 conn->plan = io_close();
305 backend_plan_changed(conn);
308 case 0: /* Keep going with plan. */
310 case 1: /* Done: get next plan. */
312 if (timeout_active(conn))
313 backend_del_timeout(conn);
314 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
315 backend_plan_changed(conn);
320 /* Close the connection, we're done. */
321 struct io_plan io_close(void)
326 /* This means we're closing. */
328 plan.u.close.saved_errno = errno;
330 io_plan_debug(&plan);
334 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
339 /* Exit the loop, returning this (non-NULL) arg. */
340 struct io_plan io_break_(void *ret, struct io_plan plan)
343 io_loop_return = ret;