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 int (*io)(int fd, struct io_plan *plan);
20 /* Only NULL if closing. */
21 struct io_plan (*next)(struct io_conn *, void *arg);
60 extern bool io_plan_for_other;
61 extern bool (*io_debug)(struct io_conn *conn);
62 #define io_plan_other() ((io_plan_for_other = true))
63 void io_plan_debug(struct io_plan *plan);
65 #define io_plan_other() (void)0
66 static inline void io_plan_debug(struct io_plan *plan) { }
70 * io_new_conn - create a new connection.
71 * @fd: the file descriptor.
72 * @plan: the first I/O function.
74 * This creates a connection which owns @fd. @plan will be called on the
77 * Returns NULL on error (and sets errno).
79 #define io_new_conn(fd, plan) \
80 (io_plan_other(), io_new_conn_((fd), (plan)))
81 struct io_conn *io_new_conn_(int fd, struct io_plan plan);
84 * io_set_finish - set finish function on a connection.
85 * @conn: the connection.
86 * @finish: the function to call when it's closed or fails.
87 * @arg: the argument to @finish.
89 * @finish will be called when an I/O operation fails, or you call
90 * io_close() on the connection. errno will be set to the value
91 * after the failed I/O, or at the call to io_close().
93 #define io_set_finish(conn, finish, arg) \
94 io_set_finish_((conn), \
95 typesafe_cb_preargs(void, void *, \
99 void io_set_finish_(struct io_conn *conn,
100 void (*finish)(struct io_conn *, void *),
104 * io_new_listener - create a new accepting listener.
105 * @fd: the file descriptor.
106 * @init: the function to call for a new connection
107 * @arg: the argument to @init.
109 * When @fd becomes readable, we accept() and pass that fd to init().
111 * Returns NULL on error (and sets errno).
113 #define io_new_listener(fd, init, arg) \
114 io_new_listener_((fd), \
115 typesafe_cb_preargs(void, void *, \
119 struct io_listener *io_new_listener_(int fd,
120 void (*init)(int fd, void *arg),
124 * io_close_listener - delete a listener.
125 * @listener: the listener returned from io_new_listener.
127 * This closes the fd and frees @listener.
129 void io_close_listener(struct io_listener *listener);
132 * io_write - queue data to be written.
133 * @data: the data buffer.
134 * @len: the length to write.
135 * @cb: function to call once it's done.
138 * This will queue the data buffer for writing. Once it's all
139 * written, the @cb function will be called: on an error, the finish
140 * function is called instead.
142 * Note that the I/O may actually be done immediately.
144 #define io_write(data, len, cb, arg) \
145 io_write_((data), (len), \
146 typesafe_cb_preargs(struct io_plan, void *, \
147 (cb), (arg), struct io_conn *), \
149 struct io_plan io_write_(const void *data, size_t len,
150 struct io_plan (*cb)(struct io_conn *, void *),
154 * io_read - queue buffer to be read.
155 * @data: the data buffer.
156 * @len: the length to read.
157 * @cb: function to call once it's done.
160 * This will queue the data buffer for reading. Once it's all read,
161 * the @cb function will be called: on an error, the finish function
164 * Note that the I/O may actually be done immediately.
166 #define io_read(data, len, cb, arg) \
167 io_read_((data), (len), \
168 typesafe_cb_preargs(struct io_plan, void *, \
169 (cb), (arg), struct io_conn *), \
171 struct io_plan io_read_(void *data, size_t len,
172 struct io_plan (*cb)(struct io_conn *, void *),
177 * io_read_partial - queue buffer to be read (partial OK).
178 * @data: the data buffer.
179 * @len: the maximum length to read, set to the length actually read.
180 * @cb: function to call once it's done.
183 * This will queue the data buffer for reading. Once any data is
184 * read, @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_read_partial(data, len, cb, arg) \
190 io_read_partial_((data), (len), \
191 typesafe_cb_preargs(struct io_plan, void *, \
192 (cb), (arg), struct io_conn *), \
194 struct io_plan io_read_partial_(void *data, size_t *len,
195 struct io_plan (*cb)(struct io_conn *, void *),
199 * io_write_partial - queue data to be written (partial OK).
200 * @data: the data buffer.
201 * @len: the maximum length to write, set to the length actually written.
202 * @cb: function to call once it's done.
205 * This will queue the data buffer for writing. Once any data is
206 * written, @len is updated and the @cb function will be called: on an
207 * error, the finish function is called instead.
209 * Note that the I/O may actually be done immediately.
211 #define io_write_partial(data, len, cb, arg) \
212 io_write_partial_((data), (len), \
213 typesafe_cb_preargs(struct io_plan, void *, \
214 (cb), (arg), struct io_conn *), \
216 struct io_plan io_write_partial_(const void *data, size_t *len,
217 struct io_plan (*cb)(struct io_conn *, void*),
222 * io_idle - explicitly note that this connection will do nothing.
224 * This indicates the connection is idle: some other function will
225 * later call io_read/io_write etc. (or io_close) on it, in which case
228 struct io_plan io_idle(void);
231 * io_timeout - set timeout function if the callback doesn't fire.
232 * @conn: the current connection.
233 * @ts: how long until the timeout should be called.
235 * @arg: argument to @cb.
237 * If the usual next callback is not called for this connection before @ts,
238 * this function will be called. If next callback is called, the timeout
239 * is automatically removed.
241 * Returns false on allocation failure. A connection can only have one
244 #define io_timeout(conn, ts, fn, arg) \
245 io_timeout_((conn), (ts), \
246 typesafe_cb_preargs(struct io_plan, void *, \
250 bool io_timeout_(struct io_conn *conn, struct timespec ts,
251 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
254 * io_duplex - split an fd into two connections.
255 * @conn: a connection.
256 * @plan: the first I/O function to call.
258 * Sometimes you want to be able to simultaneously read and write on a
259 * single fd, but io forces a linear call sequence. The solition is
260 * to have two connections for the same fd, and use one for read
261 * operations and one for write.
263 * You must io_close() both of them to close the fd.
265 #define io_duplex(conn, plan) \
266 (io_plan_other(), io_duplex_((conn), (plan)))
268 struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
271 * io_wake - wake up an idle connection.
272 * @conn: an idle connection.
273 * @plan: the next I/O function for @conn.
275 * This makes @conn do I/O the next time around the io_loop().
277 #define io_wake(conn, plan) (io_plan_other(), io_wake_((conn), (plan)))
278 void io_wake_(struct io_conn *conn, struct io_plan plan);
281 * io_break - return from io_loop()
282 * @ret: non-NULL value to return from io_loop().
283 * @plan: I/O to perform on return (if any)
285 * This breaks out of the io_loop. As soon as the current @next
286 * function returns, any io_closed()'d connections will have their
287 * finish callbacks called, then io_loop() with return with @ret.
289 * If io_loop() is called again, then @plan will be carried out.
291 #define io_break(ret, plan) (io_plan_other(), io_break_((ret), (plan)))
292 struct io_plan io_break_(void *ret, struct io_plan plan);
294 /* FIXME: io_recvfrom/io_sendto */
297 * io_close - plan to close a connection.
299 * On return to io_loop, the connection will be closed.
301 struct io_plan io_close(void);
304 * io_close_cb - helper callback to close a connection.
305 * @conn: the connection.
307 * This schedules a connection to be closed; designed to be used as
308 * a callback function.
310 struct io_plan io_close_cb(struct io_conn *, void *unused);
313 * io_loop - process fds until all closed on io_break.
315 * This is the core loop; it exits with the io_break() arg, or NULL if
316 * all connections and listeners are closed.
319 #endif /* CCAN_IO_H */