1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
15 struct io_listener *io_new_listener_(int fd,
16 void (*init)(int fd, void *arg),
19 struct io_listener *l = malloc(sizeof(*l));
24 l->fd.listener = true;
28 if (!add_listener(l)) {
35 void io_close_listener(struct io_listener *l)
42 struct io_conn *io_new_conn_(int fd,
44 void (*finish)(struct io_conn *, void *),
47 struct io_conn *conn = malloc(sizeof(*conn));
52 conn->fd.listener = false;
55 conn->finish = finish;
56 conn->finish_arg = arg;
59 if (!add_conn(conn)) {
66 struct io_conn *io_duplex_(struct io_conn *old,
68 void (*finish)(struct io_conn *, void *),
75 conn = malloc(sizeof(*conn));
79 conn->fd.listener = false;
80 conn->fd.fd = old->fd.fd;
83 conn->finish = finish;
84 conn->finish_arg = arg;
86 if (!add_duplex(conn)) {
94 bool io_timeout_(struct io_conn *conn, struct timespec ts,
95 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
100 conn->timeout = malloc(sizeof(*conn->timeout));
104 assert(!timeout_active(conn));
106 conn->timeout->next = cb;
107 conn->timeout->next_arg = arg;
108 backend_add_timeout(conn, ts);
112 /* Returns true if we're finished. */
113 static bool do_write(int fd, struct io_plan *plan)
115 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
117 /* Override next function to close us. */
118 plan->next = io_close;
122 plan->u.write.buf += ret;
123 plan->u.write.len -= ret;
124 return (plan->u.write.len == 0);
127 /* Queue some data to be written. */
128 struct io_plan io_write_(const void *data, size_t len,
129 struct io_plan (*cb)(struct io_conn *, void *),
135 plan.u.write.buf = data;
136 plan.u.write.len = len;
140 plan.pollflag = POLLOUT;
144 static bool do_read(int fd, struct io_plan *plan)
146 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
148 /* Override next function to close us. */
149 plan->next = io_close;
153 plan->u.read.buf += ret;
154 plan->u.read.len -= ret;
155 return (plan->u.read.len == 0);
158 /* Queue a request to read into a buffer. */
159 struct io_plan io_read_(void *data, size_t len,
160 struct io_plan (*cb)(struct io_conn *, void *),
166 plan.u.read.buf = data;
167 plan.u.read.len = len;
171 plan.pollflag = POLLIN;
175 static bool do_read_partial(int fd, struct io_plan *plan)
177 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
179 /* Override next function to close us. */
180 plan->next = io_close;
184 *plan->u.readpart.lenp = ret;
188 /* Queue a partial request to read into a buffer. */
189 struct io_plan io_read_partial_(void *data, size_t *len,
190 struct io_plan (*cb)(struct io_conn *, void *),
196 plan.u.readpart.buf = data;
197 plan.u.readpart.lenp = len;
198 plan.io = do_read_partial;
201 plan.pollflag = POLLIN;
206 static bool do_write_partial(int fd, struct io_plan *plan)
208 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
210 /* Override next function to close us. */
211 plan->next = io_close;
215 *plan->u.writepart.lenp = ret;
219 /* Queue a partial write request. */
220 struct io_plan io_write_partial_(const void *data, size_t *len,
221 struct io_plan (*cb)(struct io_conn*, void *),
227 plan.u.writepart.buf = data;
228 plan.u.writepart.lenp = len;
229 plan.io = do_write_partial;
232 plan.pollflag = POLLOUT;
237 struct io_plan io_idle(void)
243 /* Never called (overridded by io_wake), but NULL means closing */
244 plan.next = io_close;
249 void io_wake(struct io_conn *conn, struct io_plan plan)
252 /* It might be closing, but we haven't called its finish() yet. */
253 if (!conn->plan.next)
255 /* It was idle, right? */
256 assert(!conn->plan.io);
258 backend_plan_changed(conn);
261 void io_ready(struct io_conn *conn)
263 if (conn->plan.io(conn->fd.fd, &conn->plan)) {
264 if (timeout_active(conn))
265 backend_del_timeout(conn);
266 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
267 backend_plan_changed(conn);
271 /* Useful next functions. */
272 /* Close the connection, we're done. */
273 struct io_plan io_close(struct io_conn *conn, void *arg)
278 /* This means we're closing. */
284 /* Exit the loop, returning this (non-NULL) arg. */
285 struct io_plan io_break(void *ret, struct io_plan plan)
288 io_loop_return = ret;