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 * @data: the data buffer.
131 * @len: the length to write.
132 * @cb: function to call once it's done.
135 * This will queue the data buffer for writing. Once it's all
136 * written, the @cb function will be called: on an error, the finish
137 * function is called instead.
139 * Note that the I/O may actually be done immediately.
141 #define io_write(data, len, cb, arg) \
142 io_write_((data), (len), \
143 typesafe_cb_preargs(struct io_plan, void *, \
144 (cb), (arg), struct io_conn *), \
146 struct io_plan io_write_(const void *data, size_t len,
147 struct io_plan (*cb)(struct io_conn *, void *),
151 * io_read - queue buffer to be read.
152 * @data: the data buffer.
153 * @len: the length to read.
154 * @cb: function to call once it's done.
157 * This will queue the data buffer for reading. Once it's all read,
158 * the @cb function will be called: on an error, the finish function
161 * Note that the I/O may actually be done immediately.
163 #define io_read(data, len, cb, arg) \
164 io_read_((data), (len), \
165 typesafe_cb_preargs(struct io_plan, void *, \
166 (cb), (arg), struct io_conn *), \
168 struct io_plan io_read_(void *data, size_t len,
169 struct io_plan (*cb)(struct io_conn *, void *),
174 * io_read_partial - queue buffer to be read (partial OK).
175 * @data: the data buffer.
176 * @len: the maximum length to read, set to the length actually read.
177 * @cb: function to call once it's done.
180 * This will queue the data buffer for reading. Once any data is
181 * read, @len is updated and the @cb function will be called: on an
182 * error, the finish function is called instead.
184 * Note that the I/O may actually be done immediately.
186 #define io_read_partial(data, len, cb, arg) \
187 io_read_partial_((data), (len), \
188 typesafe_cb_preargs(struct io_plan, void *, \
189 (cb), (arg), struct io_conn *), \
191 struct io_plan io_read_partial_(void *data, size_t *len,
192 struct io_plan (*cb)(struct io_conn *, void *),
196 * io_write_partial - queue data to be written (partial OK).
197 * @data: the data buffer.
198 * @len: the maximum length to write, set to the length actually written.
199 * @cb: function to call once it's done.
202 * This will queue the data buffer for writing. Once any data is
203 * written, @len is updated and the @cb function will be called: on an
204 * error, the finish function is called instead.
206 * Note that the I/O may actually be done immediately.
208 #define io_write_partial(data, len, cb, arg) \
209 io_write_partial_((data), (len), \
210 typesafe_cb_preargs(struct io_plan, void *, \
211 (cb), (arg), struct io_conn *), \
213 struct io_plan io_write_partial_(const void *data, size_t *len,
214 struct io_plan (*cb)(struct io_conn *, void*),
219 * io_idle - explicitly note that this connection will do nothing.
221 * This indicates the connection is idle: some other function will
222 * later call io_read/io_write etc. (or io_close) on it, in which case
225 struct io_plan io_idle(void);
228 * io_timeout - set timeout function if the callback doesn't fire.
229 * @conn: the current connection.
230 * @ts: how long until the timeout should be called.
232 * @arg: argument to @cb.
234 * If the usual next callback is not called for this connection before @ts,
235 * this function will be called. If next callback is called, the timeout
236 * is automatically removed.
238 * Returns false on allocation failure. A connection can only have one
241 #define io_timeout(conn, ts, fn, arg) \
242 io_timeout_((conn), (ts), \
243 typesafe_cb_preargs(struct io_plan, void *, \
247 bool io_timeout_(struct io_conn *conn, struct timespec ts,
248 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
251 * io_duplex - split an fd into two connections.
252 * @conn: a connection.
253 * @start: the first function to call.
254 * @finish: the function to call when it's closed or fails.
255 * @arg: the argument to both @start and @finish.
257 * Sometimes you want to be able to simultaneously read and write on a
258 * single fd, but io forces a linear call sequence. The solition is
259 * to have two connections for the same fd, and use one for read
260 * operations and one for write.
262 * You must io_close() both of them to close the fd.
264 #define io_duplex(conn, start, finish, arg) \
266 typesafe_cb_preargs(struct io_plan, void *, \
267 (start), (arg), struct io_conn *), \
268 typesafe_cb_preargs(void, void *, (finish), (arg), \
272 struct io_conn *io_duplex_(struct io_conn *conn,
273 struct io_plan (*start)(struct io_conn *, void *),
274 void (*finish)(struct io_conn *, void *),
278 * io_wake - wake up and idle connection.
279 * @conn: an idle connection.
280 * @fn: the next function to call once queued IO is complete.
281 * @arg: the argument to @next.
283 * This makes @conn run its @next function the next time around the
286 #define io_wake(conn, fn, arg) \
288 typesafe_cb_preargs(struct io_plan, void *, \
289 (fn), (arg), struct io_conn *), \
291 void io_wake_(struct io_conn *conn,
292 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
295 * io_break - return from io_loop()
296 * @ret: non-NULL value to return from io_loop().
297 * @cb: function to call once on return
300 * This breaks out of the io_loop. As soon as the current @next
301 * function returns, any io_closed()'d connections will have their
302 * finish callbacks called, then io_loop() with return with @ret.
304 * If io_loop() is called again, then @cb will be called.
306 #define io_break(ret, fn, arg) \
308 typesafe_cb_preargs(struct io_plan, void *, \
309 (fn), (arg), struct io_conn *), \
311 struct io_plan io_break_(void *ret,
312 struct io_plan (*fn)(struct io_conn *, void *),
315 /* FIXME: io_recvfrom/io_sendto */
318 * io_close - terminate a connection.
319 * @conn: any connection.
321 * The schedules a connection to be closed. It can be done on any
322 * connection, whether it has I/O queued or not (though that I/O may
323 * be performed first).
325 * It's common to 'return io_close(...)' from a @next function, but
326 * io_close can also be used as an argument to io_next().
328 struct io_plan io_close(struct io_conn *, void *unused);
331 * io_loop - process fds until all closed on io_break.
333 * This is the core loop; it exits with the io_break() arg, or NULL if
334 * all connections and listeners are closed.
337 #endif /* CCAN_IO_H */