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 {
32 * struct io_plan - returned from a setup function.
34 * A plan of what IO to do, when.
38 /* Only NULL if idle. */
39 bool (*io)(int fd, struct io_plan *plan);
40 /* Only NULL if closing. */
41 struct io_plan (*next)(struct io_conn *, void *arg);
45 struct io_state_read read;
46 struct io_state_write write;
47 struct io_state_readpart readpart;
48 struct io_state_writepart writepart;
53 * io_new_conn - create a new connection.
54 * @fd: the file descriptor.
55 * @plan: the first I/O function.
56 * @finish: the function to call when it's closed or fails.
57 * @arg: the argument to @finish.
59 * This creates a connection which owns @fd. @plan will be called on the
60 * next io_loop(), and @finish will be called when an I/O operation
61 * fails, or you call io_close() on the connection.
63 * Returns NULL on error (and sets errno).
65 #define io_new_conn(fd, plan, finish, arg) \
66 io_new_conn_((fd), (plan), \
67 typesafe_cb_preargs(void, void *, (finish), (arg), \
70 struct io_conn *io_new_conn_(int fd,
72 void (*finish)(struct io_conn *, void *),
76 * io_new_listener - create a new accepting listener.
77 * @fd: the file descriptor.
78 * @init: the function to call for a new connection
79 * @arg: the argument to @init.
81 * When @fd becomes readable, we accept() and pass that fd to init().
83 * Returns NULL on error (and sets errno).
85 #define io_new_listener(fd, init, arg) \
86 io_new_listener_((fd), \
87 typesafe_cb_preargs(void, void *, \
91 struct io_listener *io_new_listener_(int fd,
92 void (*init)(int fd, void *arg),
96 * io_close_listener - delete a listener.
97 * @listener: the listener returned from io_new_listener.
99 * This closes the fd and frees @listener.
101 void io_close_listener(struct io_listener *listener);
104 * io_write - queue data to be written.
105 * @data: the data buffer.
106 * @len: the length to write.
107 * @cb: function to call once it's done.
110 * This will queue the data buffer for writing. Once it's all
111 * written, the @cb function will be called: on an error, the finish
112 * function is called instead.
114 * Note that the I/O may actually be done immediately.
116 #define io_write(data, len, cb, arg) \
117 io_write_((data), (len), \
118 typesafe_cb_preargs(struct io_plan, void *, \
119 (cb), (arg), struct io_conn *), \
121 struct io_plan io_write_(const void *data, size_t len,
122 struct io_plan (*cb)(struct io_conn *, void *),
126 * io_read - queue buffer to be read.
127 * @data: the data buffer.
128 * @len: the length to read.
129 * @cb: function to call once it's done.
132 * This will queue the data buffer for reading. Once it's all read,
133 * the @cb function will be called: on an error, the finish function
136 * Note that the I/O may actually be done immediately.
138 #define io_read(data, len, cb, arg) \
139 io_read_((data), (len), \
140 typesafe_cb_preargs(struct io_plan, void *, \
141 (cb), (arg), struct io_conn *), \
143 struct io_plan io_read_(void *data, size_t len,
144 struct io_plan (*cb)(struct io_conn *, void *),
149 * io_read_partial - queue buffer to be read (partial OK).
150 * @data: the data buffer.
151 * @len: the maximum length to read, set to the length actually read.
152 * @cb: function to call once it's done.
155 * This will queue the data buffer for reading. Once any data is
156 * read, @len is updated and the @cb function will be called: on an
157 * error, the finish function is called instead.
159 * Note that the I/O may actually be done immediately.
161 #define io_read_partial(data, len, cb, arg) \
162 io_read_partial_((data), (len), \
163 typesafe_cb_preargs(struct io_plan, void *, \
164 (cb), (arg), struct io_conn *), \
166 struct io_plan io_read_partial_(void *data, size_t *len,
167 struct io_plan (*cb)(struct io_conn *, void *),
171 * io_write_partial - queue data to be written (partial OK).
172 * @data: the data buffer.
173 * @len: the maximum length to write, set to the length actually written.
174 * @cb: function to call once it's done.
177 * This will queue the data buffer for writing. Once any data is
178 * written, @len is updated and the @cb function will be called: on an
179 * error, the finish function is called instead.
181 * Note that the I/O may actually be done immediately.
183 #define io_write_partial(data, len, cb, arg) \
184 io_write_partial_((data), (len), \
185 typesafe_cb_preargs(struct io_plan, void *, \
186 (cb), (arg), struct io_conn *), \
188 struct io_plan io_write_partial_(const void *data, size_t *len,
189 struct io_plan (*cb)(struct io_conn *, void*),
194 * io_idle - explicitly note that this connection will do nothing.
196 * This indicates the connection is idle: some other function will
197 * later call io_read/io_write etc. (or io_close) on it, in which case
200 struct io_plan io_idle(void);
203 * io_timeout - set timeout function if the callback doesn't fire.
204 * @conn: the current connection.
205 * @ts: how long until the timeout should be called.
207 * @arg: argument to @cb.
209 * If the usual next callback is not called for this connection before @ts,
210 * this function will be called. If next callback is called, the timeout
211 * is automatically removed.
213 * Returns false on allocation failure. A connection can only have one
216 #define io_timeout(conn, ts, fn, arg) \
217 io_timeout_((conn), (ts), \
218 typesafe_cb_preargs(struct io_plan, void *, \
222 bool io_timeout_(struct io_conn *conn, struct timespec ts,
223 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
226 * io_duplex - split an fd into two connections.
227 * @conn: a connection.
228 * @plan: the first I/O function to call.
229 * @finish: the function to call when it's closed or fails.
230 * @arg: the argument to @finish.
232 * Sometimes you want to be able to simultaneously read and write on a
233 * single fd, but io forces a linear call sequence. The solition is
234 * to have two connections for the same fd, and use one for read
235 * operations and one for write.
237 * You must io_close() both of them to close the fd.
239 #define io_duplex(conn, plan, finish, arg) \
240 io_duplex_((conn), (plan), \
241 typesafe_cb_preargs(void, void *, (finish), (arg), \
245 struct io_conn *io_duplex_(struct io_conn *conn,
247 void (*finish)(struct io_conn *, void *),
251 * io_wake - wake up an idle connection.
252 * @conn: an idle connection.
253 * @plan: the next I/O function for @conn.
255 * This makes @conn do I/O the next time around the io_loop().
257 void io_wake(struct io_conn *conn, struct io_plan plan);
260 * io_break - return from io_loop()
261 * @ret: non-NULL value to return from io_loop().
262 * @plan: I/O to perform on return (if any)
264 * This breaks out of the io_loop. As soon as the current @next
265 * function returns, any io_closed()'d connections will have their
266 * finish callbacks called, then io_loop() with return with @ret.
268 * If io_loop() is called again, then @plan will be carried out.
270 struct io_plan io_break(void *ret, struct io_plan plan);
272 /* FIXME: io_recvfrom/io_sendto */
275 * io_close - terminate a connection.
276 * @conn: any connection.
278 * The schedules a connection to be closed. It can be done on any
279 * connection, whether it has I/O queued or not (though that I/O may
280 * be performed first).
282 * It's common to 'return io_close(...)' from a @next function, but
283 * io_close can also be used as an argument to io_next().
285 struct io_plan io_close(struct io_conn *, void *unused);
288 * io_loop - process fds until all closed on io_break.
290 * This is the core loop; it exits with the io_break() arg, or NULL if
291 * all connections and listeners are closed.
294 #endif /* CCAN_IO_H */