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 {
38 * struct io_plan - returned from a setup function.
40 * A plan of what IO to do, when.
44 /* Only NULL if idle. */
45 enum io_result (*io)(struct io_conn *conn);
46 /* Only NULL if closing. */
47 struct io_plan (*next)(struct io_conn *, void *arg);
51 struct io_state_read read;
52 struct io_state_write write;
53 struct io_state_readpart readpart;
54 struct io_state_writepart writepart;
59 * io_new_conn - create a new connection.
60 * @fd: the file descriptor.
61 * @plan: the first I/O function.
62 * @finish: the function to call when it's closed or fails.
63 * @arg: the argument to @finish.
65 * This creates a connection which owns @fd. @plan will be called on the
66 * next io_loop(), and @finish will be called when an I/O operation
67 * fails, or you call io_close() on the connection.
69 * Returns NULL on error (and sets errno).
71 #define io_new_conn(fd, plan, finish, arg) \
72 io_new_conn_((fd), (plan), \
73 typesafe_cb_preargs(void, void *, (finish), (arg), \
76 struct io_conn *io_new_conn_(int fd,
78 void (*finish)(struct io_conn *, void *),
82 * io_new_listener - create a new accepting listener.
83 * @fd: the file descriptor.
84 * @init: the function to call for a new connection
85 * @arg: the argument to @init.
87 * When @fd becomes readable, we accept() and pass that fd to init().
89 * Returns NULL on error (and sets errno).
91 #define io_new_listener(fd, init, arg) \
92 io_new_listener_((fd), \
93 typesafe_cb_preargs(void, void *, \
97 struct io_listener *io_new_listener_(int fd,
98 void (*init)(int fd, void *arg),
102 * io_close_listener - delete a listener.
103 * @listener: the listener returned from io_new_listener.
105 * This closes the fd and frees @listener.
107 void io_close_listener(struct io_listener *listener);
110 * io_write - queue data to be written.
111 * @data: the data buffer.
112 * @len: the length to write.
113 * @cb: function to call once it's done.
116 * This will queue the data buffer for writing. Once it's all
117 * written, the @cb function will be called: on an error, the finish
118 * function is called instead.
120 * Note that the I/O may actually be done immediately.
122 #define io_write(data, len, cb, arg) \
123 io_write_((data), (len), \
124 typesafe_cb_preargs(struct io_plan, void *, \
125 (cb), (arg), struct io_conn *), \
127 struct io_plan io_write_(const void *data, size_t len,
128 struct io_plan (*cb)(struct io_conn *, void *),
132 * io_read - queue buffer to be read.
133 * @data: the data buffer.
134 * @len: the length to read.
135 * @cb: function to call once it's done.
138 * This will queue the data buffer for reading. Once it's all read,
139 * the @cb function will be called: on an error, the finish function
142 * Note that the I/O may actually be done immediately.
144 #define io_read(data, len, cb, arg) \
145 io_read_((data), (len), \
146 typesafe_cb_preargs(struct io_plan, void *, \
147 (cb), (arg), struct io_conn *), \
149 struct io_plan io_read_(void *data, size_t len,
150 struct io_plan (*cb)(struct io_conn *, void *),
155 * io_read_partial - queue buffer to be read (partial OK).
156 * @data: the data buffer.
157 * @len: the maximum length to read, set to the length actually read.
158 * @cb: function to call once it's done.
161 * This will queue the data buffer for reading. Once any data is
162 * read, @len is updated and the @cb function will be called: on an
163 * error, the finish function is called instead.
165 * Note that the I/O may actually be done immediately.
167 #define io_read_partial(data, len, cb, arg) \
168 io_read_partial_((data), (len), \
169 typesafe_cb_preargs(struct io_plan, void *, \
170 (cb), (arg), struct io_conn *), \
172 struct io_plan io_read_partial_(void *data, size_t *len,
173 struct io_plan (*cb)(struct io_conn *, void *),
177 * io_write_partial - queue data to be written (partial OK).
178 * @data: the data buffer.
179 * @len: the maximum length to write, set to the length actually written.
180 * @cb: function to call once it's done.
183 * This will queue the data buffer for writing. Once any data is
184 * written, @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_write_partial(data, len, cb, arg) \
190 io_write_partial_((data), (len), \
191 typesafe_cb_preargs(struct io_plan, void *, \
192 (cb), (arg), struct io_conn *), \
194 struct io_plan io_write_partial_(const void *data, size_t *len,
195 struct io_plan (*cb)(struct io_conn *, void*),
200 * io_idle - explicitly note that this connection will do nothing.
202 * This indicates the connection is idle: some other function will
203 * later call io_read/io_write etc. (or io_close) on it, in which case
206 struct io_plan io_idle(void);
209 * io_timeout - set timeout function if the callback doesn't fire.
210 * @conn: the current connection.
211 * @ts: how long until the timeout should be called.
213 * @arg: argument to @cb.
215 * If the usual next callback is not called for this connection before @ts,
216 * this function will be called. If next callback is called, the timeout
217 * is automatically removed.
219 * Returns false on allocation failure. A connection can only have one
222 #define io_timeout(conn, ts, fn, arg) \
223 io_timeout_((conn), (ts), \
224 typesafe_cb_preargs(struct io_plan, void *, \
228 bool io_timeout_(struct io_conn *conn, struct timespec ts,
229 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
232 * io_duplex - split an fd into two connections.
233 * @conn: a connection.
234 * @plan: the first I/O function to call.
235 * @finish: the function to call when it's closed or fails.
236 * @arg: the argument to @finish.
238 * Sometimes you want to be able to simultaneously read and write on a
239 * single fd, but io forces a linear call sequence. The solition is
240 * to have two connections for the same fd, and use one for read
241 * operations and one for write.
243 * You must io_close() both of them to close the fd.
245 #define io_duplex(conn, plan, finish, arg) \
246 io_duplex_((conn), (plan), \
247 typesafe_cb_preargs(void, void *, (finish), (arg), \
251 struct io_conn *io_duplex_(struct io_conn *conn,
253 void (*finish)(struct io_conn *, void *),
257 * io_wake - wake up an idle connection.
258 * @conn: an idle connection.
259 * @plan: the next I/O function for @conn.
261 * This makes @conn do I/O the next time around the io_loop().
263 void io_wake(struct io_conn *conn, struct io_plan plan);
266 * io_break - return from io_loop()
267 * @ret: non-NULL value to return from io_loop().
268 * @plan: I/O to perform on return (if any)
270 * This breaks out of the io_loop. As soon as the current @next
271 * function returns, any io_closed()'d connections will have their
272 * finish callbacks called, then io_loop() with return with @ret.
274 * If io_loop() is called again, then @plan will be carried out.
276 struct io_plan io_break(void *ret, struct io_plan plan);
278 /* FIXME: io_recvfrom/io_sendto */
281 * io_close - terminate a connection.
282 * @conn: any connection.
284 * The schedules a connection to be closed. It can be done on any
285 * connection, whether it has I/O queued or not (though that I/O may
286 * be performed first).
288 * It's common to 'return io_close(...)' from a @next function, but
289 * io_close can also be used as an argument to io_next().
291 struct io_plan io_close(struct io_conn *, void *unused);
294 * io_loop - process fds until all closed on io_break.
296 * This is the core loop; it exits with the io_break() arg, or NULL if
297 * all connections and listeners are closed.
300 #endif /* CCAN_IO_H */