1 /* Licensed under BSD-MIT - see LICENSE file for details */
4 #include <ccan/typesafe_cb/typesafe_cb.h>
9 * struct io_op - pointer to return from io functions.
11 * This undefined structure is just to help the compiler check that you
12 * really do return the result of an io-queueing method.
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_op *, 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_op *(*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_op *, 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_op *(*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_op *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_op *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_op *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_op *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_op *io_idle(struct io_conn *conn);
155 * io_duplex - split an fd into two connections.
156 * @conn: a connection.
157 * @start: the first function to call.
158 * @finish: the function to call when it's closed or fails.
159 * @arg: the argument to both @start and @finish.
161 * Sometimes you want to be able to simultaneously read and write on a
162 * single fd, but io forces a linear call sequence. The solition is
163 * to have two connections for the same fd, and use one for read
164 * operations and one for write.
166 * You must io_close() both of them to close the fd.
168 #define io_duplex(conn, start, finish, arg) \
170 typesafe_cb_preargs(struct io_op *, void *, \
171 (start), (arg), struct io_conn *), \
172 typesafe_cb_preargs(void, void *, (finish), (arg), \
176 struct io_conn *io_duplex_(struct io_conn *conn,
177 struct io_op *(*start)(struct io_conn *, void *),
178 void (*finish)(struct io_conn *, void *),
182 * io_wake - wake up and idle connection.
183 * @conn: an idle connection.
184 * @next: the next function to call once queued IO is complete.
185 * @arg: the argument to @next.
187 * This makes @conn run its @next function the next time around the
190 #define io_wake(conn, next, arg) \
192 typesafe_cb_preargs(struct io_op *, void *, \
193 (next), (arg), struct io_conn *), \
195 void io_wake_(struct io_conn *conn,
196 struct io_op *(*next)(struct io_conn *, void *), void *arg);
199 * io_break - return from io_loop()
200 * @arg: non-NULL value to return from io_loop().
201 * @next: what to call next (can be NULL if we expect no return).
203 * This breaks out of the io_loop. As soon as the current @next
204 * function returns, any io_closed()'d connections will have their
205 * finish callbacks called, then io_loop() with return with @arg.
207 * If io_loop() is called again, then @next will be called.
209 struct io_op *io_break(void *arg, struct io_next *next);
212 * io_next - indicate what callback to call next.
213 * @conn: this connection.
214 * @next: the next function to call once queued IO is complete.
215 * @arg: the argument to @next.
217 * Every @next (or @start) function should "return io_next(...);" once
218 * they have indicated what io to perform (eg. io_write, io_idle).
219 * The exception is io_close(), which can be used instead of io_next().
221 * Note that as an optimization, the next function may be called
222 * immediately, which is why this should be the last statement in your
225 #define io_next(conn, next, arg) \
227 typesafe_cb_preargs(struct io_op *, void *, \
228 (next), (arg), struct io_conn *), \
230 struct io_next *io_next_(struct io_conn *conn,
231 struct io_op *(*next)(struct io_conn *, void *arg),
234 /* FIXME: io_recvfrom/io_sendto */
237 * io_close - terminate a connection.
238 * @conn: any connection.
240 * The schedules a connection to be closed. It can be done on any
241 * connection, whether it has I/O queued or not (though that I/O may
242 * be performed first).
244 * It's common to 'return io_close(...)' from a @next function, but
245 * io_close can also be used as an argument to io_next().
247 struct io_op *io_close(struct io_conn *, void *unused);
250 * io_loop - process fds until all closed on io_break.
252 * This is the core loop; it exits with the io_break() arg, or NULL if
253 * all connections and listeners are closed.
256 #endif /* CCAN_IO_H */