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 {
44 * struct io_plan - returned from a setup function.
46 * A plan of what IO to do, when.
51 enum io_result (*io)(struct io_conn *conn);
52 struct io_plan (*next)(struct io_conn *, void *arg);
56 struct io_state_read read;
57 struct io_state_write write;
58 struct io_state_readpart readpart;
59 struct io_state_writepart writepart;
64 * io_new_conn - create a new connection.
65 * @fd: the file descriptor.
66 * @plan: the first I/O function.
67 * @finish: the function to call when it's closed or fails.
68 * @arg: the argument to @finish.
70 * This creates a connection which owns @fd. @plan will be called on the
71 * next io_loop(), and @finish will be called when an I/O operation
72 * fails, or you call io_close() on the connection.
74 * Returns NULL on error (and sets errno).
76 #define io_new_conn(fd, plan, finish, arg) \
77 io_new_conn_((fd), (plan), \
78 typesafe_cb_preargs(void, void *, (finish), (arg), \
81 struct io_conn *io_new_conn_(int fd,
83 void (*finish)(struct io_conn *, void *),
87 * io_new_listener - create a new accepting listener.
88 * @fd: the file descriptor.
89 * @init: the function to call for a new connection
90 * @arg: the argument to @init.
92 * When @fd becomes readable, we accept() and pass that fd to init().
94 * Returns NULL on error (and sets errno).
96 #define io_new_listener(fd, init, arg) \
97 io_new_listener_((fd), \
98 typesafe_cb_preargs(void, void *, \
102 struct io_listener *io_new_listener_(int fd,
103 void (*init)(int fd, void *arg),
107 * io_close_listener - delete a listener.
108 * @listener: the listener returned from io_new_listener.
110 * This closes the fd and frees @listener.
112 void io_close_listener(struct io_listener *listener);
115 * io_write - queue data to be written.
116 * @data: the data buffer.
117 * @len: the length to write.
118 * @cb: function to call once it's done.
121 * This will queue the data buffer for writing. Once it's all
122 * written, the @cb function will be called: on an error, the finish
123 * function is called instead.
125 * Note that the I/O may actually be done immediately.
127 #define io_write(data, len, cb, arg) \
128 io_write_((data), (len), \
129 typesafe_cb_preargs(struct io_plan, void *, \
130 (cb), (arg), struct io_conn *), \
132 struct io_plan io_write_(const void *data, size_t len,
133 struct io_plan (*cb)(struct io_conn *, void *),
137 * io_read - queue buffer to be read.
138 * @data: the data buffer.
139 * @len: the length to read.
140 * @cb: function to call once it's done.
143 * This will queue the data buffer for reading. Once it's all read,
144 * the @cb function will be called: on an error, the finish function
147 * Note that the I/O may actually be done immediately.
149 #define io_read(data, len, cb, arg) \
150 io_read_((data), (len), \
151 typesafe_cb_preargs(struct io_plan, void *, \
152 (cb), (arg), struct io_conn *), \
154 struct io_plan io_read_(void *data, size_t len,
155 struct io_plan (*cb)(struct io_conn *, void *),
160 * io_read_partial - queue buffer to be read (partial OK).
161 * @data: the data buffer.
162 * @len: the maximum length to read, set to the length actually read.
163 * @cb: function to call once it's done.
166 * This will queue the data buffer for reading. Once any data is
167 * read, @len is updated and the @cb function will be called: on an
168 * error, the finish function is called instead.
170 * Note that the I/O may actually be done immediately.
172 #define io_read_partial(data, len, cb, arg) \
173 io_read_partial_((data), (len), \
174 typesafe_cb_preargs(struct io_plan, void *, \
175 (cb), (arg), struct io_conn *), \
177 struct io_plan io_read_partial_(void *data, size_t *len,
178 struct io_plan (*cb)(struct io_conn *, void *),
182 * io_write_partial - queue data to be written (partial OK).
183 * @data: the data buffer.
184 * @len: the maximum length to write, set to the length actually written.
185 * @cb: function to call once it's done.
188 * This will queue the data buffer for writing. Once any data is
189 * written, @len is updated and the @cb function will be called: on an
190 * error, the finish function is called instead.
192 * Note that the I/O may actually be done immediately.
194 #define io_write_partial(data, len, cb, arg) \
195 io_write_partial_((data), (len), \
196 typesafe_cb_preargs(struct io_plan, void *, \
197 (cb), (arg), struct io_conn *), \
199 struct io_plan io_write_partial_(const void *data, size_t *len,
200 struct io_plan (*cb)(struct io_conn *, void*),
205 * io_idle - explicitly note that this connection will do nothing.
207 * This indicates the connection is idle: some other function will
208 * later call io_read/io_write etc. (or io_close) on it, in which case
211 struct io_plan io_idle(void);
214 * io_timeout - set timeout function if the callback doesn't fire.
215 * @conn: the current connection.
216 * @ts: how long until the timeout should be called.
218 * @arg: argument to @cb.
220 * If the usual next callback is not called for this connection before @ts,
221 * this function will be called. If next callback is called, the timeout
222 * is automatically removed.
224 * Returns false on allocation failure. A connection can only have one
227 #define io_timeout(conn, ts, fn, arg) \
228 io_timeout_((conn), (ts), \
229 typesafe_cb_preargs(struct io_plan, void *, \
233 bool io_timeout_(struct io_conn *conn, struct timespec ts,
234 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
237 * io_duplex - split an fd into two connections.
238 * @conn: a connection.
239 * @plan: the first I/O function to call.
240 * @finish: the function to call when it's closed or fails.
241 * @arg: the argument to @finish.
243 * Sometimes you want to be able to simultaneously read and write on a
244 * single fd, but io forces a linear call sequence. The solition is
245 * to have two connections for the same fd, and use one for read
246 * operations and one for write.
248 * You must io_close() both of them to close the fd.
250 #define io_duplex(conn, plan, finish, arg) \
251 io_duplex_((conn), (plan), \
252 typesafe_cb_preargs(void, void *, (finish), (arg), \
256 struct io_conn *io_duplex_(struct io_conn *conn,
258 void (*finish)(struct io_conn *, void *),
262 * io_wake - wake up an idle connection.
263 * @conn: an idle connection.
264 * @plan: the next I/O function for @conn.
266 * This makes @conn do I/O the next time around the io_loop().
268 void io_wake(struct io_conn *conn, struct io_plan plan);
271 * io_break - return from io_loop()
272 * @ret: non-NULL value to return from io_loop().
273 * @plan: I/O to perform on return (if any)
275 * This breaks out of the io_loop. As soon as the current @next
276 * function returns, any io_closed()'d connections will have their
277 * finish callbacks called, then io_loop() with return with @ret.
279 * If io_loop() is called again, then @plan will be carried out.
281 struct io_plan io_break(void *ret, struct io_plan plan);
283 /* FIXME: io_recvfrom/io_sendto */
286 * io_close - terminate a connection.
287 * @conn: any connection.
289 * The schedules a connection to be closed. It can be done on any
290 * connection, whether it has I/O queued or not (though that I/O may
291 * be performed first).
293 * It's common to 'return io_close(...)' from a @next function, but
294 * io_close can also be used as an argument to io_next().
296 struct io_plan io_close(struct io_conn *, void *unused);
299 * io_loop - process fds until all closed on io_break.
301 * This is the core loop; it exits with the io_break() arg, or NULL if
302 * all connections and listeners are closed.
305 #endif /* CCAN_IO_H */