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_op - pointer to return from io functions.
12 * This undefined structure is just to help the compiler check that you
13 * really do return the result of an io-queueing method.
18 * struct io_next - pointer to what we're going to do next.
20 * Bundles up callbacks, generated by io_next().
25 * io_new_conn - create a new connection.
26 * @fd: the file descriptor.
27 * @start: the first function to call.
28 * @finish: the function to call when it's closed or fails.
29 * @arg: the argument to both @start and @finish.
31 * This creates a connection which owns @fd. @start will be called on the
32 * next return to io_loop(), and @finish will be called when an I/O operation
33 * fails, or you call io_close() on the connection.
35 * The @start function must call one of the io queueing functions
36 * (eg. io_read, io_write) and return the next function to call once
37 * that is done using io_next(). The alternative is to call io_close().
39 * Returns NULL on error (and sets errno).
41 #define io_new_conn(fd, start, finish, arg) \
43 typesafe_cb_preargs(struct io_op *, void *, \
44 (start), (arg), struct io_conn *), \
45 typesafe_cb_preargs(void, void *, (finish), (arg), \
48 struct io_conn *io_new_conn_(int fd,
49 struct io_op *(*start)(struct io_conn *, void *),
50 void (*finish)(struct io_conn *, void *),
54 * io_new_listener - create a new accepting listener.
55 * @fd: the file descriptor.
56 * @start: the first function to call on new connections.
57 * @finish: the function to call when the connection is closed or fails.
58 * @arg: the argument to both @start and @finish.
60 * When @fd becomes readable, we accept() and turn that fd into a new
63 * Returns NULL on error (and sets errno).
65 #define io_new_listener(fd, start, finish, arg) \
66 io_new_listener_((fd), \
67 typesafe_cb_preargs(struct io_op *, void *, \
70 typesafe_cb_preargs(void, void *, (finish), \
71 (arg), struct io_conn *), \
73 struct io_listener *io_new_listener_(int fd,
74 struct io_op *(*start)(struct io_conn *,
76 void (*finish)(struct io_conn *,
81 * io_close_listener - delete a listener.
82 * @listener: the listener returned from io_new_listener.
84 * This closes the fd and frees @listener.
86 void io_close_listener(struct io_listener *listener);
89 * io_write - queue data to be written.
90 * @data: the data buffer.
91 * @len: the length to write.
92 * @next: what to call next.
94 * This will queue the data buffer for writing. Once it's all written, the
95 * function registered with io_next() will be called: on an error, the finish
96 * function is called instead.
98 * Note that the I/O may actually be done immediately.
100 struct io_op *io_write(const void *data, size_t len, struct io_next *next);
103 * io_read - queue buffer to be read.
104 * @data: the data buffer.
105 * @len: the length to read.
106 * @next: what to call next.
108 * This will queue the data buffer for reading. Once it's all read, the
109 * function registered with io_next() will be called: on an error, the finish
110 * function is called instead.
112 * Note that the I/O may actually be done immediately.
114 struct io_op *io_read(void *data, size_t len, struct io_next *next);
117 * io_read_partial - queue buffer to be read (partial OK).
118 * @data: the data buffer.
119 * @len: the maximum length to read, set to the length actually read.
120 * @next: what to call next.
122 * This will queue the data buffer for reading. Once any data is
123 * read, @len is updated and the function registered with io_next()
124 * will be called: on an error, the finish function is called instead.
126 * Note that the I/O may actually be done immediately.
128 struct io_op *io_read_partial(void *data, size_t *len, struct io_next *next);
131 * io_write_partial - queue data to be written (partial OK).
132 * @data: the data buffer.
133 * @len: the maximum length to write, set to the length actually written.
134 * @next: what to call next.
136 * This will queue the data buffer for writing. Once any data is
137 * written, @len is updated and the function registered with io_next()
138 * will be called: on an error, the finish function is called instead.
140 * Note that the I/O may actually be done immediately.
142 struct io_op *io_write_partial(const void *data, size_t *len,
143 struct io_next *next);
146 * io_idle - explicitly note that this connection will do nothing.
147 * @conn: the current connection.
149 * This indicates the connection is idle: some other function will
150 * later call io_read/io_write etc. (or io_close) on it, in which case
153 struct io_op *io_idle(struct io_conn *conn);
156 * io_timeout - set timeout function if the callback doesn't fire.
157 * @conn: the current connection.
158 * @ts: how long until the timeout should be called.
159 * @next: function to call.
160 * @arg: argument to @next.
162 * If the usual next callback is not called for this connection before @ts,
163 * this function will be called. If next callback is called, the timeout
164 * is automatically removed.
166 * Returns false on allocation failure. A connection can only have one
169 #define io_timeout(conn, ts, next, arg) \
170 io_timeout_((conn), (ts), \
171 typesafe_cb_preargs(struct io_op *, void *, \
176 bool io_timeout_(struct io_conn *conn, struct timespec ts,
177 struct io_op *(*next)(struct io_conn *, void *), void *arg);
180 * io_duplex - split an fd into two connections.
181 * @conn: a connection.
182 * @start: the first function to call.
183 * @finish: the function to call when it's closed or fails.
184 * @arg: the argument to both @start and @finish.
186 * Sometimes you want to be able to simultaneously read and write on a
187 * single fd, but io forces a linear call sequence. The solition is
188 * to have two connections for the same fd, and use one for read
189 * operations and one for write.
191 * You must io_close() both of them to close the fd.
193 #define io_duplex(conn, start, finish, arg) \
195 typesafe_cb_preargs(struct io_op *, void *, \
196 (start), (arg), struct io_conn *), \
197 typesafe_cb_preargs(void, void *, (finish), (arg), \
201 struct io_conn *io_duplex_(struct io_conn *conn,
202 struct io_op *(*start)(struct io_conn *, void *),
203 void (*finish)(struct io_conn *, void *),
207 * io_wake - wake up and idle connection.
208 * @conn: an idle connection.
209 * @next: the next function to call once queued IO is complete.
210 * @arg: the argument to @next.
212 * This makes @conn run its @next function the next time around the
215 #define io_wake(conn, next, arg) \
217 typesafe_cb_preargs(struct io_op *, void *, \
218 (next), (arg), struct io_conn *), \
220 void io_wake_(struct io_conn *conn,
221 struct io_op *(*next)(struct io_conn *, void *), void *arg);
224 * io_break - return from io_loop()
225 * @arg: non-NULL value to return from io_loop().
226 * @next: what to call next (can be NULL if we expect no return).
228 * This breaks out of the io_loop. As soon as the current @next
229 * function returns, any io_closed()'d connections will have their
230 * finish callbacks called, then io_loop() with return with @arg.
232 * If io_loop() is called again, then @next will be called.
234 struct io_op *io_break(void *arg, struct io_next *next);
237 * io_next - indicate what callback to call next.
238 * @conn: this connection.
239 * @next: the next function to call once queued IO is complete.
240 * @arg: the argument to @next.
242 * Every @next (or @start) function should "return io_next(...);" once
243 * they have indicated what io to perform (eg. io_write, io_idle).
244 * The exception is io_close(), which can be used instead of io_next().
246 * Note that as an optimization, the next function may be called
247 * immediately, which is why this should be the last statement in your
250 #define io_next(conn, next, arg) \
252 typesafe_cb_preargs(struct io_op *, void *, \
253 (next), (arg), struct io_conn *), \
255 struct io_next *io_next_(struct io_conn *conn,
256 struct io_op *(*next)(struct io_conn *, void *arg),
259 /* FIXME: io_recvfrom/io_sendto */
262 * io_close - terminate a connection.
263 * @conn: any connection.
265 * The schedules a connection to be closed. It can be done on any
266 * connection, whether it has I/O queued or not (though that I/O may
267 * be performed first).
269 * It's common to 'return io_close(...)' from a @next function, but
270 * io_close can also be used as an argument to io_next().
272 struct io_op *io_close(struct io_conn *, void *unused);
275 * io_loop - process fds until all closed on io_break.
277 * This is the core loop; it exits with the io_break() arg, or NULL if
278 * all connections and listeners are closed.
281 #endif /* CCAN_IO_H */