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,
83 void (*finish)(struct io_conn *, void *),
86 struct io_conn *conn = malloc(sizeof(*conn));
91 conn->fd.listener = false;
94 conn->finish = finish;
95 conn->finish_arg = arg;
98 if (!add_conn(conn)) {
105 struct io_conn *io_duplex_(struct io_conn *old,
107 void (*finish)(struct io_conn *, void *),
110 struct io_conn *conn;
112 assert(!old->duplex);
114 conn = malloc(sizeof(*conn));
118 conn->fd.listener = false;
119 conn->fd.fd = old->fd.fd;
122 conn->finish = finish;
123 conn->finish_arg = arg;
124 conn->timeout = NULL;
125 if (!add_duplex(conn)) {
133 bool io_timeout_(struct io_conn *conn, struct timespec ts,
134 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
138 if (!conn->timeout) {
139 conn->timeout = malloc(sizeof(*conn->timeout));
143 assert(!timeout_active(conn));
145 conn->timeout->next = cb;
146 conn->timeout->next_arg = arg;
147 backend_add_timeout(conn, ts);
151 /* Returns true if we're finished. */
152 static bool do_write(int fd, struct io_plan *plan)
154 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
156 /* Override next function to close us. */
157 plan->next = io_close;
161 plan->u.write.buf += ret;
162 plan->u.write.len -= ret;
163 return (plan->u.write.len == 0);
166 /* Queue some data to be written. */
167 struct io_plan io_write_(const void *data, size_t len,
168 struct io_plan (*cb)(struct io_conn *, void *),
174 plan.u.write.buf = data;
175 plan.u.write.len = len;
179 plan.pollflag = POLLOUT;
181 io_plan_debug(&plan);
185 static bool do_read(int fd, struct io_plan *plan)
187 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
189 /* Override next function to close us. */
190 plan->next = io_close;
194 plan->u.read.buf += ret;
195 plan->u.read.len -= ret;
196 return (plan->u.read.len == 0);
199 /* Queue a request to read into a buffer. */
200 struct io_plan io_read_(void *data, size_t len,
201 struct io_plan (*cb)(struct io_conn *, void *),
207 plan.u.read.buf = data;
208 plan.u.read.len = len;
212 plan.pollflag = POLLIN;
214 io_plan_debug(&plan);
218 static bool do_read_partial(int fd, struct io_plan *plan)
220 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
222 /* Override next function to close us. */
223 plan->next = io_close;
227 *plan->u.readpart.lenp = ret;
231 /* Queue a partial request to read into a buffer. */
232 struct io_plan io_read_partial_(void *data, size_t *len,
233 struct io_plan (*cb)(struct io_conn *, void *),
239 plan.u.readpart.buf = data;
240 plan.u.readpart.lenp = len;
241 plan.io = do_read_partial;
244 plan.pollflag = POLLIN;
246 io_plan_debug(&plan);
250 static bool do_write_partial(int fd, struct io_plan *plan)
252 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
254 /* Override next function to close us. */
255 plan->next = io_close;
259 *plan->u.writepart.lenp = ret;
263 /* Queue a partial write request. */
264 struct io_plan io_write_partial_(const void *data, size_t *len,
265 struct io_plan (*cb)(struct io_conn*, void *),
271 plan.u.writepart.buf = data;
272 plan.u.writepart.lenp = len;
273 plan.io = do_write_partial;
276 plan.pollflag = POLLOUT;
278 io_plan_debug(&plan);
282 struct io_plan io_idle(void)
288 /* Never called (overridded by io_wake), but NULL means closing */
289 plan.next = io_close;
291 io_plan_debug(&plan);
295 void io_wake_(struct io_conn *conn, struct io_plan plan)
298 /* It might be closing, but we haven't called its finish() yet. */
299 if (!conn->plan.next)
301 /* It was idle, right? */
302 assert(!conn->plan.io);
304 backend_plan_changed(conn);
309 void io_ready(struct io_conn *conn)
311 if (conn->plan.io(conn->fd.fd, &conn->plan)) {
313 if (timeout_active(conn))
314 backend_del_timeout(conn);
315 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
316 backend_plan_changed(conn);
321 /* Useful next functions. */
322 /* Close the connection, we're done. */
323 struct io_plan io_close(struct io_conn *conn, void *arg)
328 /* This means we're closing. */
331 io_plan_debug(&plan);
335 /* Exit the loop, returning this (non-NULL) arg. */
336 struct io_plan io_break_(void *ret, struct io_plan plan)
339 io_loop_return = ret;