1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <ccan/time/time.h>
10 * struct io_plan - pointer to return from a setup function.
12 * A plan of what IO to do, when.
17 * struct io_next - pointer to what we're going to do next.
19 * Bundles up callbacks, generated by io_next().
24 * io_new_conn - create a new connection.
25 * @fd: the file descriptor.
26 * @start: the first function to call.
27 * @finish: the function to call when it's closed or fails.
28 * @arg: the argument to both @start and @finish.
30 * This creates a connection which owns @fd. @start will be called on the
31 * next return to io_loop(), and @finish will be called when an I/O operation
32 * fails, or you call io_close() on the connection.
34 * The @start function must call one of the io queueing functions
35 * (eg. io_read, io_write) and return the next function to call once
36 * that is done using io_next(). The alternative is to call io_close().
38 * Returns NULL on error (and sets errno).
40 #define io_new_conn(fd, start, finish, arg) \
42 typesafe_cb_preargs(struct io_plan *, void *, \
43 (start), (arg), struct io_conn *), \
44 typesafe_cb_preargs(void, void *, (finish), (arg), \
47 struct io_conn *io_new_conn_(int fd,
48 struct io_plan *(*start)(struct io_conn *, void *),
49 void (*finish)(struct io_conn *, void *),
53 * io_new_listener - create a new accepting listener.
54 * @fd: the file descriptor.
55 * @start: the first function to call on new connections.
56 * @finish: the function to call when the connection is closed or fails.
57 * @arg: the argument to both @start and @finish.
59 * When @fd becomes readable, we accept() and turn that fd into a new
62 * Returns NULL on error (and sets errno).
64 #define io_new_listener(fd, start, finish, arg) \
65 io_new_listener_((fd), \
66 typesafe_cb_preargs(struct io_plan *, void *, \
69 typesafe_cb_preargs(void, void *, (finish), \
70 (arg), struct io_conn *), \
72 struct io_listener *io_new_listener_(int fd,
73 struct io_plan *(*start)(struct io_conn *,
75 void (*finish)(struct io_conn *,
80 * io_close_listener - delete a listener.
81 * @listener: the listener returned from io_new_listener.
83 * This closes the fd and frees @listener.
85 void io_close_listener(struct io_listener *listener);
88 * io_write - queue data to be written.
89 * @data: the data buffer.
90 * @len: the length to write.
91 * @next: what to call next.
93 * This will queue the data buffer for writing. Once it's all written, the
94 * function registered with io_next() will be called: on an error, the finish
95 * function is called instead.
97 * Note that the I/O may actually be done immediately.
99 struct io_plan *io_write(const void *data, size_t len, struct io_next *next);
102 * io_read - queue buffer to be read.
103 * @data: the data buffer.
104 * @len: the length to read.
105 * @next: what to call next.
107 * This will queue the data buffer for reading. Once it's all read, the
108 * function registered with io_next() will be called: on an error, the finish
109 * function is called instead.
111 * Note that the I/O may actually be done immediately.
113 struct io_plan *io_read(void *data, size_t len, struct io_next *next);
116 * io_read_partial - queue buffer to be read (partial OK).
117 * @data: the data buffer.
118 * @len: the maximum length to read, set to the length actually read.
119 * @next: what to call next.
121 * This will queue the data buffer for reading. Once any data is
122 * read, @len is updated and the function registered with io_next()
123 * will be called: on an error, the finish function is called instead.
125 * Note that the I/O may actually be done immediately.
127 struct io_plan *io_read_partial(void *data, size_t *len, struct io_next *next);
130 * io_write_partial - queue data to be written (partial OK).
131 * @data: the data buffer.
132 * @len: the maximum length to write, set to the length actually written.
133 * @next: what to call next.
135 * This will queue the data buffer for writing. Once any data is
136 * written, @len is updated and the function registered with io_next()
137 * will be called: on an error, the finish function is called instead.
139 * Note that the I/O may actually be done immediately.
141 struct io_plan *io_write_partial(const void *data, size_t *len,
142 struct io_next *next);
145 * io_idle - explicitly note that this connection will do nothing.
146 * @conn: the current connection.
148 * This indicates the connection is idle: some other function will
149 * later call io_read/io_write etc. (or io_close) on it, in which case
152 struct io_plan *io_idle(struct io_conn *conn);
155 * io_timeout - set timeout function if the callback doesn't fire.
156 * @conn: the current connection.
157 * @ts: how long until the timeout should be called.
158 * @next: function to call.
159 * @arg: argument to @next.
161 * If the usual next callback is not called for this connection before @ts,
162 * this function will be called. If next callback is called, the timeout
163 * is automatically removed.
165 * Returns false on allocation failure. A connection can only have one
168 #define io_timeout(conn, ts, next, arg) \
169 io_timeout_((conn), (ts), \
170 typesafe_cb_preargs(struct io_plan *, void *, \
175 bool io_timeout_(struct io_conn *conn, struct timespec ts,
176 struct io_plan *(*next)(struct io_conn *, void *), void *arg);
179 * io_duplex - split an fd into two connections.
180 * @conn: a connection.
181 * @start: the first function to call.
182 * @finish: the function to call when it's closed or fails.
183 * @arg: the argument to both @start and @finish.
185 * Sometimes you want to be able to simultaneously read and write on a
186 * single fd, but io forces a linear call sequence. The solition is
187 * to have two connections for the same fd, and use one for read
188 * operations and one for write.
190 * You must io_close() both of them to close the fd.
192 #define io_duplex(conn, start, finish, arg) \
194 typesafe_cb_preargs(struct io_plan *, void *, \
195 (start), (arg), struct io_conn *), \
196 typesafe_cb_preargs(void, void *, (finish), (arg), \
200 struct io_conn *io_duplex_(struct io_conn *conn,
201 struct io_plan *(*start)(struct io_conn *, void *),
202 void (*finish)(struct io_conn *, void *),
206 * io_wake - wake up and idle connection.
207 * @conn: an idle connection.
208 * @next: the next function to call once queued IO is complete.
209 * @arg: the argument to @next.
211 * This makes @conn run its @next function the next time around the
214 #define io_wake(conn, next, arg) \
216 typesafe_cb_preargs(struct io_plan *, void *, \
217 (next), (arg), struct io_conn *), \
219 void io_wake_(struct io_conn *conn,
220 struct io_plan *(*next)(struct io_conn *, void *), void *arg);
223 * io_break - return from io_loop()
224 * @arg: non-NULL value to return from io_loop().
225 * @next: what to call next (can be NULL if we expect no return).
227 * This breaks out of the io_loop. As soon as the current @next
228 * function returns, any io_closed()'d connections will have their
229 * finish callbacks called, then io_loop() with return with @arg.
231 * If io_loop() is called again, then @next will be called.
233 struct io_plan *io_break(void *arg, struct io_next *next);
236 * io_next - indicate what callback to call next.
237 * @conn: this connection.
238 * @next: the next function to call once queued IO is complete.
239 * @arg: the argument to @next.
241 * Every @next (or @start) function should "return io_next(...);" once
242 * they have indicated what io to perform (eg. io_write, io_idle).
243 * The exception is io_close(), which can be used instead of io_next().
245 * Note that as an optimization, the next function may be called
246 * immediately, which is why this should be the last statement in your
249 #define io_next(conn, next, arg) \
251 typesafe_cb_preargs(struct io_plan *, void *, \
252 (next), (arg), struct io_conn *), \
254 struct io_next *io_next_(struct io_conn *conn,
255 struct io_plan *(*next)(struct io_conn *, void *arg),
258 /* FIXME: io_recvfrom/io_sendto */
261 * io_close - terminate a connection.
262 * @conn: any connection.
264 * The schedules a connection to be closed. It can be done on any
265 * connection, whether it has I/O queued or not (though that I/O may
266 * be performed first).
268 * It's common to 'return io_close(...)' from a @next function, but
269 * io_close can also be used as an argument to io_next().
271 struct io_plan *io_close(struct io_conn *, void *unused);
274 * io_loop - process fds until all closed on io_break.
276 * This is the core loop; it exits with the io_break() arg, or NULL if
277 * all connections and listeners are closed.
280 #endif /* CCAN_IO_H */