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 * @plan: the first I/O function.
68 * @finish: the function to call when it's closed or fails.
69 * @arg: the argument to @finish.
71 * This creates a connection which owns @fd. @plan will be called on the
72 * next io_loop(), and @finish will be called when an I/O operation
73 * fails, or you call io_close() on the connection.
75 * Returns NULL on error (and sets errno).
77 #define io_new_conn(fd, plan, finish, arg) \
78 io_new_conn_((fd), (plan), \
79 typesafe_cb_preargs(void, void *, (finish), (arg), \
82 struct io_conn *io_new_conn_(int fd,
84 void (*finish)(struct io_conn *, void *),
88 * io_new_listener - create a new accepting listener.
89 * @fd: the file descriptor.
90 * @init: the function to call for a new connection
91 * @arg: the argument to @init.
93 * When @fd becomes readable, we accept() and pass that fd to init().
95 * Returns NULL on error (and sets errno).
97 #define io_new_listener(fd, init, arg) \
98 io_new_listener_((fd), \
99 typesafe_cb_preargs(void, void *, \
103 struct io_listener *io_new_listener_(int fd,
104 void (*init)(int fd, void *arg),
108 * io_close_listener - delete a listener.
109 * @listener: the listener returned from io_new_listener.
111 * This closes the fd and frees @listener.
113 void io_close_listener(struct io_listener *listener);
116 * io_write - queue data to be written.
117 * @data: the data buffer.
118 * @len: the length to write.
119 * @cb: function to call once it's done.
122 * This will queue the data buffer for writing. Once it's all
123 * written, the @cb function will be called: on an error, the finish
124 * function is called instead.
126 * Note that the I/O may actually be done immediately.
128 #define io_write(data, len, cb, arg) \
129 io_write_((data), (len), \
130 typesafe_cb_preargs(struct io_plan, void *, \
131 (cb), (arg), struct io_conn *), \
133 struct io_plan io_write_(const void *data, size_t len,
134 struct io_plan (*cb)(struct io_conn *, void *),
138 * io_read - queue buffer to be read.
139 * @data: the data buffer.
140 * @len: the length to read.
141 * @cb: function to call once it's done.
144 * This will queue the data buffer for reading. Once it's all read,
145 * the @cb function will be called: on an error, the finish function
148 * Note that the I/O may actually be done immediately.
150 #define io_read(data, len, cb, arg) \
151 io_read_((data), (len), \
152 typesafe_cb_preargs(struct io_plan, void *, \
153 (cb), (arg), struct io_conn *), \
155 struct io_plan io_read_(void *data, size_t len,
156 struct io_plan (*cb)(struct io_conn *, void *),
161 * io_read_partial - queue buffer to be read (partial OK).
162 * @data: the data buffer.
163 * @len: the maximum length to read, set to the length actually read.
164 * @cb: function to call once it's done.
167 * This will queue the data buffer for reading. Once any data is
168 * read, @len is updated and the @cb function will be called: on an
169 * error, the finish function is called instead.
171 * Note that the I/O may actually be done immediately.
173 #define io_read_partial(data, len, cb, arg) \
174 io_read_partial_((data), (len), \
175 typesafe_cb_preargs(struct io_plan, void *, \
176 (cb), (arg), struct io_conn *), \
178 struct io_plan io_read_partial_(void *data, size_t *len,
179 struct io_plan (*cb)(struct io_conn *, void *),
183 * io_write_partial - queue data to be written (partial OK).
184 * @data: the data buffer.
185 * @len: the maximum length to write, set to the length actually written.
186 * @cb: function to call once it's done.
189 * This will queue the data buffer for writing. Once any data is
190 * written, @len is updated and the @cb function will be called: on an
191 * error, the finish function is called instead.
193 * Note that the I/O may actually be done immediately.
195 #define io_write_partial(data, len, cb, arg) \
196 io_write_partial_((data), (len), \
197 typesafe_cb_preargs(struct io_plan, void *, \
198 (cb), (arg), struct io_conn *), \
200 struct io_plan io_write_partial_(const void *data, size_t *len,
201 struct io_plan (*cb)(struct io_conn *, void*),
206 * io_idle - explicitly note that this connection will do nothing.
208 * This indicates the connection is idle: some other function will
209 * later call io_read/io_write etc. (or io_close) on it, in which case
212 struct io_plan io_idle(void);
215 * io_timeout - set timeout function if the callback doesn't fire.
216 * @conn: the current connection.
217 * @ts: how long until the timeout should be called.
219 * @arg: argument to @cb.
221 * If the usual next callback is not called for this connection before @ts,
222 * this function will be called. If next callback is called, the timeout
223 * is automatically removed.
225 * Returns false on allocation failure. A connection can only have one
228 #define io_timeout(conn, ts, fn, arg) \
229 io_timeout_((conn), (ts), \
230 typesafe_cb_preargs(struct io_plan, void *, \
234 bool io_timeout_(struct io_conn *conn, struct timespec ts,
235 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
238 * io_duplex - split an fd into two connections.
239 * @conn: a connection.
240 * @plan: the first I/O function to call.
241 * @finish: the function to call when it's closed or fails.
242 * @arg: the argument to @finish.
244 * Sometimes you want to be able to simultaneously read and write on a
245 * single fd, but io forces a linear call sequence. The solition is
246 * to have two connections for the same fd, and use one for read
247 * operations and one for write.
249 * You must io_close() both of them to close the fd.
251 #define io_duplex(conn, plan, finish, arg) \
252 io_duplex_((conn), (plan), \
253 typesafe_cb_preargs(void, void *, (finish), (arg), \
257 struct io_conn *io_duplex_(struct io_conn *conn,
259 void (*finish)(struct io_conn *, void *),
263 * io_wake - wake up an idle connection.
264 * @conn: an idle connection.
265 * @plan: the next I/O function for @conn.
267 * This makes @conn do I/O the next time around the io_loop().
269 void io_wake(struct io_conn *conn, struct io_plan plan);
272 * io_break - return from io_loop()
273 * @ret: non-NULL value to return from io_loop().
274 * @plan: I/O to perform on return (if any)
276 * This breaks out of the io_loop. As soon as the current @next
277 * function returns, any io_closed()'d connections will have their
278 * finish callbacks called, then io_loop() with return with @ret.
280 * If io_loop() is called again, then @plan will be carried out.
282 struct io_plan io_break(void *ret, struct io_plan plan);
284 /* FIXME: io_recvfrom/io_sendto */
287 * io_close - terminate a connection.
288 * @conn: any connection.
290 * The schedules a connection to be closed. It can be done on any
291 * connection, whether it has I/O queued or not (though that I/O may
292 * be performed first).
294 * It's common to 'return io_close(...)' from a @next function, but
295 * io_close can also be used as an argument to io_next().
297 struct io_plan io_close(struct io_conn *, void *unused);
300 * io_loop - process fds until all closed on io_break.
302 * This is the core loop; it exits with the io_break() arg, or NULL if
303 * all connections and listeners are closed.
306 #endif /* CCAN_IO_H */