1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <ccan/time/time.h>
12 * struct io_plan - returned from a setup function.
14 * A plan of what IO to do, when.
18 /* Only NULL if idle. */
19 bool (*io)(int fd, struct io_plan *plan);
20 /* Only NULL if closing. */
21 struct io_plan (*next)(struct io_conn *, void *arg);
57 extern bool io_plan_for_other;
58 extern bool (*io_debug)(struct io_conn *conn);
59 #define io_plan_other() ((io_plan_for_other = true))
60 void io_plan_debug(struct io_plan *plan);
62 #define io_plan_other() (void)0
63 static inline void io_plan_debug(struct io_plan *plan) { }
67 * io_new_conn - create a new connection.
68 * @fd: the file descriptor.
69 * @plan: the first I/O function.
70 * @finish: the function to call when it's closed or fails.
71 * @arg: the argument to @finish.
73 * This creates a connection which owns @fd. @plan will be called on the
74 * next io_loop(), and @finish will be called when an I/O operation
75 * fails, or you call io_close() on the connection.
77 * Returns NULL on error (and sets errno).
79 #define io_new_conn(fd, plan, finish, arg) \
80 (io_plan_other(), io_new_conn_((fd), (plan), \
81 typesafe_cb_preargs(void, void *, \
85 struct io_conn *io_new_conn_(int fd,
87 void (*finish)(struct io_conn *, void *),
91 * io_new_listener - create a new accepting listener.
92 * @fd: the file descriptor.
93 * @init: the function to call for a new connection
94 * @arg: the argument to @init.
96 * When @fd becomes readable, we accept() and pass that fd to init().
98 * Returns NULL on error (and sets errno).
100 #define io_new_listener(fd, init, arg) \
101 io_new_listener_((fd), \
102 typesafe_cb_preargs(void, void *, \
106 struct io_listener *io_new_listener_(int fd,
107 void (*init)(int fd, void *arg),
111 * io_close_listener - delete a listener.
112 * @listener: the listener returned from io_new_listener.
114 * This closes the fd and frees @listener.
116 void io_close_listener(struct io_listener *listener);
119 * io_write - queue data to be written.
120 * @data: the data buffer.
121 * @len: the length to write.
122 * @cb: function to call once it's done.
125 * This will queue the data buffer for writing. Once it's all
126 * written, the @cb function will be called: on an error, the finish
127 * function is called instead.
129 * Note that the I/O may actually be done immediately.
131 #define io_write(data, len, cb, arg) \
132 io_write_((data), (len), \
133 typesafe_cb_preargs(struct io_plan, void *, \
134 (cb), (arg), struct io_conn *), \
136 struct io_plan io_write_(const void *data, size_t len,
137 struct io_plan (*cb)(struct io_conn *, void *),
141 * io_read - queue buffer to be read.
142 * @data: the data buffer.
143 * @len: the length to read.
144 * @cb: function to call once it's done.
147 * This will queue the data buffer for reading. Once it's all read,
148 * the @cb function will be called: on an error, the finish function
151 * Note that the I/O may actually be done immediately.
153 #define io_read(data, len, cb, arg) \
154 io_read_((data), (len), \
155 typesafe_cb_preargs(struct io_plan, void *, \
156 (cb), (arg), struct io_conn *), \
158 struct io_plan io_read_(void *data, size_t len,
159 struct io_plan (*cb)(struct io_conn *, void *),
164 * io_read_partial - queue buffer to be read (partial OK).
165 * @data: the data buffer.
166 * @len: the maximum length to read, set to the length actually read.
167 * @cb: function to call once it's done.
170 * This will queue the data buffer for reading. Once any data is
171 * read, @len is updated and the @cb function will be called: on an
172 * error, the finish function is called instead.
174 * Note that the I/O may actually be done immediately.
176 #define io_read_partial(data, len, cb, arg) \
177 io_read_partial_((data), (len), \
178 typesafe_cb_preargs(struct io_plan, void *, \
179 (cb), (arg), struct io_conn *), \
181 struct io_plan io_read_partial_(void *data, size_t *len,
182 struct io_plan (*cb)(struct io_conn *, void *),
186 * io_write_partial - queue data to be written (partial OK).
187 * @data: the data buffer.
188 * @len: the maximum length to write, set to the length actually written.
189 * @cb: function to call once it's done.
192 * This will queue the data buffer for writing. Once any data is
193 * written, @len is updated and the @cb function will be called: on an
194 * error, the finish function is called instead.
196 * Note that the I/O may actually be done immediately.
198 #define io_write_partial(data, len, cb, arg) \
199 io_write_partial_((data), (len), \
200 typesafe_cb_preargs(struct io_plan, void *, \
201 (cb), (arg), struct io_conn *), \
203 struct io_plan io_write_partial_(const void *data, size_t *len,
204 struct io_plan (*cb)(struct io_conn *, void*),
209 * io_idle - explicitly note that this connection will do nothing.
211 * This indicates the connection is idle: some other function will
212 * later call io_read/io_write etc. (or io_close) on it, in which case
215 struct io_plan io_idle(void);
218 * io_timeout - set timeout function if the callback doesn't fire.
219 * @conn: the current connection.
220 * @ts: how long until the timeout should be called.
222 * @arg: argument to @cb.
224 * If the usual next callback is not called for this connection before @ts,
225 * this function will be called. If next callback is called, the timeout
226 * is automatically removed.
228 * Returns false on allocation failure. A connection can only have one
231 #define io_timeout(conn, ts, fn, arg) \
232 io_timeout_((conn), (ts), \
233 typesafe_cb_preargs(struct io_plan, void *, \
237 bool io_timeout_(struct io_conn *conn, struct timespec ts,
238 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
241 * io_duplex - split an fd into two connections.
242 * @conn: a connection.
243 * @plan: the first I/O function to call.
244 * @finish: the function to call when it's closed or fails.
245 * @arg: the argument to @finish.
247 * Sometimes you want to be able to simultaneously read and write on a
248 * single fd, but io forces a linear call sequence. The solition is
249 * to have two connections for the same fd, and use one for read
250 * operations and one for write.
252 * You must io_close() both of them to close the fd.
254 #define io_duplex(conn, plan, finish, arg) \
255 (io_plan_other(), io_duplex_((conn), (plan), \
256 typesafe_cb_preargs(void, void *, \
261 struct io_conn *io_duplex_(struct io_conn *conn,
263 void (*finish)(struct io_conn *, void *),
267 * io_wake - wake up an idle connection.
268 * @conn: an idle connection.
269 * @plan: the next I/O function for @conn.
271 * This makes @conn do I/O the next time around the io_loop().
273 #define io_wake(conn, plan) (io_plan_other(), io_wake_((conn), (plan)))
274 void io_wake_(struct io_conn *conn, struct io_plan plan);
277 * io_break - return from io_loop()
278 * @ret: non-NULL value to return from io_loop().
279 * @plan: I/O to perform on return (if any)
281 * This breaks out of the io_loop. As soon as the current @next
282 * function returns, any io_closed()'d connections will have their
283 * finish callbacks called, then io_loop() with return with @ret.
285 * If io_loop() is called again, then @plan will be carried out.
287 #define io_break(ret, plan) (io_plan_other(), io_break_((ret), (plan)))
288 struct io_plan io_break_(void *ret, struct io_plan plan);
290 /* FIXME: io_recvfrom/io_sendto */
293 * io_close - terminate a connection.
294 * @conn: any connection.
296 * The schedules a connection to be closed. It can be done on any
297 * connection, whether it has I/O queued or not (though that I/O may
298 * be performed first).
300 * It's common to 'return io_close(...)' from a @next function, but
301 * io_close can also be used as an argument to io_next().
303 struct io_plan io_close(struct io_conn *, void *unused);
306 * io_loop - process fds until all closed on io_break.
308 * This is the core loop; it exits with the io_break() arg, or NULL if
309 * all connections and listeners are closed.
312 #endif /* CCAN_IO_H */