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 {
43 * struct io_plan - returned from a setup function.
45 * A plan of what IO to do, when.
50 enum io_result (*io)(struct io_conn *conn);
51 struct io_plan (*next)(struct io_conn *, void *arg);
55 struct io_state_read read;
56 struct io_state_write write;
57 struct io_state_readpart readpart;
58 struct io_state_writepart writepart;
63 * io_new_conn - create a new connection.
64 * @fd: the file descriptor.
65 * @plan: the first I/O function.
66 * @finish: the function to call when it's closed or fails.
67 * @arg: the argument to @finish.
69 * This creates a connection which owns @fd. @plan will be called on the
70 * next io_loop(), and @finish will be called when an I/O operation
71 * fails, or you call io_close() on the connection.
73 * Returns NULL on error (and sets errno).
75 #define io_new_conn(fd, plan, finish, arg) \
76 io_new_conn_((fd), (plan), \
77 typesafe_cb_preargs(void, void *, (finish), (arg), \
80 struct io_conn *io_new_conn_(int fd,
82 void (*finish)(struct io_conn *, void *),
86 * io_new_listener - create a new accepting listener.
87 * @fd: the file descriptor.
88 * @init: the function to call for a new connection
89 * @arg: the argument to @init.
91 * When @fd becomes readable, we accept() and pass that fd to init().
93 * Returns NULL on error (and sets errno).
95 #define io_new_listener(fd, init, arg) \
96 io_new_listener_((fd), \
97 typesafe_cb_preargs(void, void *, \
101 struct io_listener *io_new_listener_(int fd,
102 void (*init)(int fd, void *arg),
106 * io_close_listener - delete a listener.
107 * @listener: the listener returned from io_new_listener.
109 * This closes the fd and frees @listener.
111 void io_close_listener(struct io_listener *listener);
114 * io_write - queue data to be written.
115 * @data: the data buffer.
116 * @len: the length to write.
117 * @cb: function to call once it's done.
120 * This will queue the data buffer for writing. Once it's all
121 * written, the @cb function will be called: on an error, the finish
122 * function is called instead.
124 * Note that the I/O may actually be done immediately.
126 #define io_write(data, len, cb, arg) \
127 io_write_((data), (len), \
128 typesafe_cb_preargs(struct io_plan, void *, \
129 (cb), (arg), struct io_conn *), \
131 struct io_plan io_write_(const void *data, size_t len,
132 struct io_plan (*cb)(struct io_conn *, void *),
136 * io_read - queue buffer to be read.
137 * @data: the data buffer.
138 * @len: the length to read.
139 * @cb: function to call once it's done.
142 * This will queue the data buffer for reading. Once it's all read,
143 * the @cb function will be called: on an error, the finish function
146 * Note that the I/O may actually be done immediately.
148 #define io_read(data, len, cb, arg) \
149 io_read_((data), (len), \
150 typesafe_cb_preargs(struct io_plan, void *, \
151 (cb), (arg), struct io_conn *), \
153 struct io_plan io_read_(void *data, size_t len,
154 struct io_plan (*cb)(struct io_conn *, void *),
159 * io_read_partial - queue buffer to be read (partial OK).
160 * @data: the data buffer.
161 * @len: the maximum length to read, set to the length actually read.
162 * @cb: function to call once it's done.
165 * This will queue the data buffer for reading. Once any data is
166 * read, @len is updated and the @cb function will be called: on an
167 * error, the finish function is called instead.
169 * Note that the I/O may actually be done immediately.
171 #define io_read_partial(data, len, cb, arg) \
172 io_read_partial_((data), (len), \
173 typesafe_cb_preargs(struct io_plan, void *, \
174 (cb), (arg), struct io_conn *), \
176 struct io_plan io_read_partial_(void *data, size_t *len,
177 struct io_plan (*cb)(struct io_conn *, void *),
181 * io_write_partial - queue data to be written (partial OK).
182 * @data: the data buffer.
183 * @len: the maximum length to write, set to the length actually written.
184 * @cb: function to call once it's done.
187 * This will queue the data buffer for writing. Once any data is
188 * written, @len is updated and the @cb function will be called: on an
189 * error, the finish function is called instead.
191 * Note that the I/O may actually be done immediately.
193 #define io_write_partial(data, len, cb, arg) \
194 io_write_partial_((data), (len), \
195 typesafe_cb_preargs(struct io_plan, void *, \
196 (cb), (arg), struct io_conn *), \
198 struct io_plan io_write_partial_(const void *data, size_t *len,
199 struct io_plan (*cb)(struct io_conn *, void*),
204 * io_idle - explicitly note that this connection will do nothing.
206 * This indicates the connection is idle: some other function will
207 * later call io_read/io_write etc. (or io_close) on it, in which case
210 struct io_plan io_idle(void);
213 * io_timeout - set timeout function if the callback doesn't fire.
214 * @conn: the current connection.
215 * @ts: how long until the timeout should be called.
217 * @arg: argument to @cb.
219 * If the usual next callback is not called for this connection before @ts,
220 * this function will be called. If next callback is called, the timeout
221 * is automatically removed.
223 * Returns false on allocation failure. A connection can only have one
226 #define io_timeout(conn, ts, fn, arg) \
227 io_timeout_((conn), (ts), \
228 typesafe_cb_preargs(struct io_plan, void *, \
232 bool io_timeout_(struct io_conn *conn, struct timespec ts,
233 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
236 * io_duplex - split an fd into two connections.
237 * @conn: a connection.
238 * @plan: the first I/O function to call.
239 * @finish: the function to call when it's closed or fails.
240 * @arg: the argument to @finish.
242 * Sometimes you want to be able to simultaneously read and write on a
243 * single fd, but io forces a linear call sequence. The solition is
244 * to have two connections for the same fd, and use one for read
245 * operations and one for write.
247 * You must io_close() both of them to close the fd.
249 #define io_duplex(conn, plan, finish, arg) \
250 io_duplex_((conn), (plan), \
251 typesafe_cb_preargs(void, void *, (finish), (arg), \
255 struct io_conn *io_duplex_(struct io_conn *conn,
257 void (*finish)(struct io_conn *, void *),
261 * io_wake - wake up an idle connection.
262 * @conn: an idle connection.
263 * @plan: the next I/O function for @conn.
265 * This makes @conn do I/O the next time around the io_loop().
267 void io_wake(struct io_conn *conn, struct io_plan plan);
270 * io_break - return from io_loop()
271 * @ret: non-NULL value to return from io_loop().
272 * @plan: I/O to perform on return (if any)
274 * This breaks out of the io_loop. As soon as the current @next
275 * function returns, any io_closed()'d connections will have their
276 * finish callbacks called, then io_loop() with return with @ret.
278 * If io_loop() is called again, then @plan will be carried out.
280 struct io_plan io_break(void *ret, struct io_plan plan);
282 /* FIXME: io_recvfrom/io_sendto */
285 * io_close - terminate a connection.
286 * @conn: any connection.
288 * The schedules a connection to be closed. It can be done on any
289 * connection, whether it has I/O queued or not (though that I/O may
290 * be performed first).
292 * It's common to 'return io_close(...)' from a @next function, but
293 * io_close can also be used as an argument to io_next().
295 struct io_plan io_close(struct io_conn *, void *unused);
298 * io_loop - process fds until all closed on io_break.
300 * This is the core loop; it exits with the io_break() arg, or NULL if
301 * all connections and listeners are closed.
304 #endif /* CCAN_IO_H */