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 bool do_write(int fd, struct io_plan *plan)
156 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
158 /* Override next function to close us. */
159 plan->next = io_close;
163 plan->u.write.buf += ret;
164 plan->u.write.len -= ret;
165 return (plan->u.write.len == 0);
168 /* Queue some data to be written. */
169 struct io_plan io_write_(const void *data, size_t len,
170 struct io_plan (*cb)(struct io_conn *, void *),
176 plan.u.write.buf = data;
177 plan.u.write.len = len;
181 plan.pollflag = POLLOUT;
183 io_plan_debug(&plan);
187 static bool do_read(int fd, struct io_plan *plan)
189 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
191 /* Override next function to close us. */
192 plan->next = io_close;
196 plan->u.read.buf += ret;
197 plan->u.read.len -= ret;
198 return (plan->u.read.len == 0);
201 /* Queue a request to read into a buffer. */
202 struct io_plan io_read_(void *data, size_t len,
203 struct io_plan (*cb)(struct io_conn *, void *),
209 plan.u.read.buf = data;
210 plan.u.read.len = len;
214 plan.pollflag = POLLIN;
216 io_plan_debug(&plan);
220 static bool do_read_partial(int fd, struct io_plan *plan)
222 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
224 /* Override next function to close us. */
225 plan->next = io_close;
229 *plan->u.readpart.lenp = ret;
233 /* Queue a partial request to read into a buffer. */
234 struct io_plan io_read_partial_(void *data, size_t *len,
235 struct io_plan (*cb)(struct io_conn *, void *),
241 plan.u.readpart.buf = data;
242 plan.u.readpart.lenp = len;
243 plan.io = do_read_partial;
246 plan.pollflag = POLLIN;
248 io_plan_debug(&plan);
252 static bool do_write_partial(int fd, struct io_plan *plan)
254 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
256 /* Override next function to close us. */
257 plan->next = io_close;
261 *plan->u.writepart.lenp = ret;
265 /* Queue a partial write request. */
266 struct io_plan io_write_partial_(const void *data, size_t *len,
267 struct io_plan (*cb)(struct io_conn*, void *),
273 plan.u.writepart.buf = data;
274 plan.u.writepart.lenp = len;
275 plan.io = do_write_partial;
278 plan.pollflag = POLLOUT;
280 io_plan_debug(&plan);
284 struct io_plan io_idle(void)
290 /* Never called (overridded by io_wake), but NULL means closing */
291 plan.next = io_close;
293 io_plan_debug(&plan);
297 void io_wake_(struct io_conn *conn, struct io_plan plan)
300 /* It might be closing, but we haven't called its finish() yet. */
301 if (!conn->plan.next)
303 /* It was idle, right? */
304 assert(!conn->plan.io);
306 backend_plan_changed(conn);
311 void io_ready(struct io_conn *conn)
313 if (conn->plan.io(conn->fd.fd, &conn->plan)) {
315 if (timeout_active(conn))
316 backend_del_timeout(conn);
317 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
318 backend_plan_changed(conn);
323 /* Useful next functions. */
324 /* Close the connection, we're done. */
325 struct io_plan io_close(struct io_conn *conn, void *arg)
330 /* This means we're closing. */
333 io_plan_debug(&plan);
337 /* Exit the loop, returning this (non-NULL) arg. */
338 struct io_plan io_break_(void *ret, struct io_plan plan)
341 io_loop_return = ret;