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_wake - wake up and idle connection.
156 * @conn: an idle connection.
157 * @next: the next function to call once queued IO is complete.
158 * @arg: the argument to @next.
160 * This makes @conn run its @next function the next time around the
163 #define io_wake(conn, next, arg) \
165 typesafe_cb_preargs(struct io_op *, void *, \
166 (next), (arg), struct io_conn *), \
168 void io_wake_(struct io_conn *conn,
169 struct io_op *(*next)(struct io_conn *, void *), void *arg);
172 * io_break - return from io_loop()
173 * @arg: non-NULL value to return from io_loop().
174 * @next: what to call next (can be NULL if we expect no return).
176 * This breaks out of the io_loop. As soon as the current @next
177 * function returns, any io_closed()'d connections will have their
178 * finish callbacks called, then io_loop() with return with @arg.
180 * If io_loop() is called again, then @next will be called.
182 struct io_op *io_break(void *arg, struct io_next *next);
185 * io_next - indicate what callback to call next.
186 * @conn: this connection.
187 * @next: the next function to call once queued IO is complete.
188 * @arg: the argument to @next.
190 * Every @next (or @start) function should "return io_next(...);" once
191 * they have indicated what io to perform (eg. io_write, io_idle).
192 * The exception is io_close(), which can be used instead of io_next().
194 * Note that as an optimization, the next function may be called
195 * immediately, which is why this should be the last statement in your
198 #define io_next(conn, next, arg) \
200 typesafe_cb_preargs(struct io_op *, void *, \
201 (next), (arg), struct io_conn *), \
203 struct io_next *io_next_(struct io_conn *conn,
204 struct io_op *(*next)(struct io_conn *, void *arg),
207 /* FIXME: io_recvfrom/io_sendto */
210 * io_close - terminate a connection.
211 * @conn: any connection.
213 * The schedules a connection to be closed. It can be done on any
214 * connection, whether it has I/O queued or not (though that I/O may
215 * be performed first).
217 * It's common to 'return io_close(...)' from a @next function, but
218 * io_close can also be used as an argument to io_next().
220 struct io_op *io_close(struct io_conn *, void *unused);
223 * io_loop - process fds until all closed on io_break.
225 * This is the core loop; it exits with the io_break() arg, or NULL if
226 * all connections and listeners are closed.
229 #endif /* CCAN_IO_H */