1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <ccan/time/time.h>
11 struct io_state_read {
16 struct io_state_write {
21 struct io_state_readpart {
26 struct io_state_writepart {
39 IO_NEXT, /* eg starting, woken from idle, return from io_break. */
45 * struct io_plan - returned from a setup function.
47 * A plan of what IO to do, when.
52 enum io_result (*io)(struct io_conn *conn);
53 struct io_plan (*next)(struct io_conn *, void *arg);
57 struct io_state_read read;
58 struct io_state_write write;
59 struct io_state_readpart readpart;
60 struct io_state_writepart writepart;
65 * io_new_conn - create a new connection.
66 * @fd: the file descriptor.
67 * @start: the first function to call.
68 * @finish: the function to call when it's closed or fails.
69 * @arg: the argument to both @start and @finish.
71 * This creates a connection which owns @fd. @start will be called on the
72 * next return to io_loop(), and @finish will be called when an I/O operation
73 * fails, or you call io_close() on the connection.
75 * The @start function must call one of the io queueing functions
76 * (eg. io_read, io_write) and return the next function to call once
77 * that is done using io_next(). The alternative is to call io_close().
79 * Returns NULL on error (and sets errno).
81 #define io_new_conn(fd, start, finish, arg) \
83 typesafe_cb_preargs(struct io_plan, void *, \
84 (start), (arg), struct io_conn *), \
85 typesafe_cb_preargs(void, void *, (finish), (arg), \
88 struct io_conn *io_new_conn_(int fd,
89 struct io_plan (*start)(struct io_conn *, void *),
90 void (*finish)(struct io_conn *, void *),
94 * io_new_listener - create a new accepting listener.
95 * @fd: the file descriptor.
96 * @start: the first function to call on new connections.
97 * @finish: the function to call when the connection is closed or fails.
98 * @arg: the argument to both @start and @finish.
100 * When @fd becomes readable, we accept() and turn that fd into a new
103 * Returns NULL on error (and sets errno).
105 #define io_new_listener(fd, start, finish, arg) \
106 io_new_listener_((fd), \
107 typesafe_cb_preargs(struct io_plan, void *, \
110 typesafe_cb_preargs(void, void *, (finish), \
111 (arg), struct io_conn *), \
113 struct io_listener *io_new_listener_(int fd,
114 struct io_plan (*start)(struct io_conn *,
116 void (*finish)(struct io_conn *,
121 * io_close_listener - delete a listener.
122 * @listener: the listener returned from io_new_listener.
124 * This closes the fd and frees @listener.
126 void io_close_listener(struct io_listener *listener);
129 * io_write - queue data to be written.
130 * @conn: the current connection.
131 * @data: the data buffer.
132 * @len: the length to write.
133 * @cb: function to call once it's done.
136 * This will queue the data buffer for writing. Once it's all
137 * written, the @cb function will be called: on an error, the finish
138 * function is called instead.
140 * Note that the I/O may actually be done immediately.
142 #define io_write(conn, data, len, cb, arg) \
143 io_write_((conn), (data), (len), \
144 typesafe_cb_preargs(struct io_plan, void *, \
145 (cb), (arg), struct io_conn *), \
147 struct io_plan io_write_(struct io_conn *conn, const void *data, size_t len,
148 struct io_plan (*cb)(struct io_conn *, void *),
152 * io_read - queue buffer to be read.
153 * @conn: the current connection.
154 * @data: the data buffer.
155 * @len: the length to read.
156 * @cb: function to call once it's done.
159 * This will queue the data buffer for reading. Once it's all read,
160 * the @cb function will be called: on an error, the finish function
163 * Note that the I/O may actually be done immediately.
165 #define io_read(conn, data, len, cb, arg) \
166 io_read_((conn), (data), (len), \
167 typesafe_cb_preargs(struct io_plan, void *, \
168 (cb), (arg), struct io_conn *), \
170 struct io_plan io_read_(struct io_conn *conn, void *data, size_t len,
171 struct io_plan (*cb)(struct io_conn *, void *),
176 * io_read_partial - queue buffer to be read (partial OK).
177 * @conn: the current connection.
178 * @data: the data buffer.
179 * @len: the maximum length to read, set to the length actually read.
180 * @cb: function to call once it's done.
183 * This will queue the data buffer for reading. Once any data is
184 * read, @len is updated and the @cb function will be called: on an
185 * error, the finish function is called instead.
187 * Note that the I/O may actually be done immediately.
189 #define io_read_partial(conn, data, len, cb, arg) \
190 io_read_partial_((conn), (data), (len), \
191 typesafe_cb_preargs(struct io_plan, void *, \
192 (cb), (arg), struct io_conn *), \
194 struct io_plan io_read_partial_(struct io_conn *conn, void *data, size_t *len,
195 struct io_plan (*cb)(struct io_conn *, void *),
199 * io_write_partial - queue data to be written (partial OK).
200 * @conn: the current connection.
201 * @data: the data buffer.
202 * @len: the maximum length to write, set to the length actually written.
203 * @cb: function to call once it's done.
206 * This will queue the data buffer for writing. Once any data is
207 * written, @len is updated and the @cb function will be called: on an
208 * error, the finish function is called instead.
210 * Note that the I/O may actually be done immediately.
212 #define io_write_partial(conn, data, len, cb, arg) \
213 io_write_partial_((conn), (data), (len), \
214 typesafe_cb_preargs(struct io_plan, void *, \
215 (cb), (arg), struct io_conn *), \
217 struct io_plan io_write_partial_(struct io_conn *conn,
218 const void *data, size_t *len,
219 struct io_plan (*cb)(struct io_conn *, void*),
224 * io_idle - explicitly note that this connection will do nothing.
225 * @conn: the current connection.
227 * This indicates the connection is idle: some other function will
228 * later call io_read/io_write etc. (or io_close) on it, in which case
231 struct io_plan io_idle(struct io_conn *conn);
234 * io_timeout - set timeout function if the callback doesn't fire.
235 * @conn: the current connection.
236 * @ts: how long until the timeout should be called.
238 * @arg: argument to @cb.
240 * If the usual next callback is not called for this connection before @ts,
241 * this function will be called. If next callback is called, the timeout
242 * is automatically removed.
244 * Returns false on allocation failure. A connection can only have one
247 #define io_timeout(conn, ts, fn, arg) \
248 io_timeout_((conn), (ts), \
249 typesafe_cb_preargs(struct io_plan, void *, \
253 bool io_timeout_(struct io_conn *conn, struct timespec ts,
254 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
257 * io_duplex - split an fd into two connections.
258 * @conn: a connection.
259 * @start: the first function to call.
260 * @finish: the function to call when it's closed or fails.
261 * @arg: the argument to both @start and @finish.
263 * Sometimes you want to be able to simultaneously read and write on a
264 * single fd, but io forces a linear call sequence. The solition is
265 * to have two connections for the same fd, and use one for read
266 * operations and one for write.
268 * You must io_close() both of them to close the fd.
270 #define io_duplex(conn, start, finish, arg) \
272 typesafe_cb_preargs(struct io_plan, void *, \
273 (start), (arg), struct io_conn *), \
274 typesafe_cb_preargs(void, void *, (finish), (arg), \
278 struct io_conn *io_duplex_(struct io_conn *conn,
279 struct io_plan (*start)(struct io_conn *, void *),
280 void (*finish)(struct io_conn *, void *),
284 * io_wake - wake up and idle connection.
285 * @conn: an idle connection.
286 * @fn: the next function to call once queued IO is complete.
287 * @arg: the argument to @next.
289 * This makes @conn run its @next function the next time around the
292 #define io_wake(conn, fn, arg) \
294 typesafe_cb_preargs(struct io_plan, void *, \
295 (fn), (arg), struct io_conn *), \
297 void io_wake_(struct io_conn *conn,
298 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
301 * io_break - return from io_loop()
302 * @conn: the current connection.
303 * @ret: non-NULL value to return from io_loop().
304 * @cb: function to call once on return
307 * This breaks out of the io_loop. As soon as the current @next
308 * function returns, any io_closed()'d connections will have their
309 * finish callbacks called, then io_loop() with return with @ret.
311 * If io_loop() is called again, then @cb will be called.
313 #define io_break(conn, ret, fn, arg) \
314 io_break_((conn), (ret), \
315 typesafe_cb_preargs(struct io_plan, void *, \
316 (fn), (arg), struct io_conn *), \
318 struct io_plan io_break_(struct io_conn *conn, void *ret,
319 struct io_plan (*fn)(struct io_conn *, void *),
322 /* FIXME: io_recvfrom/io_sendto */
325 * io_close - terminate a connection.
326 * @conn: any connection.
328 * The schedules a connection to be closed. It can be done on any
329 * connection, whether it has I/O queued or not (though that I/O may
330 * be performed first).
332 * It's common to 'return io_close(...)' from a @next function, but
333 * io_close can also be used as an argument to io_next().
335 struct io_plan io_close(struct io_conn *, void *unused);
338 * io_loop - process fds until all closed on io_break.
340 * This is the core loop; it exits with the io_break() arg, or NULL if
341 * all connections and listeners are closed.
344 #endif /* CCAN_IO_H */