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 * io_new_conn - create a new connection.
12 * @fd: the file descriptor.
13 * @plan: the first I/O to perform.
15 * This creates a connection which owns @fd. @plan will be called on the
18 * Returns NULL on error (and sets errno).
22 * struct io_conn *conn;
25 * // Plan is to close the fd immediately.
26 * conn = io_new_conn(fd[0], io_close());
30 #define io_new_conn(fd, plan) \
31 (io_plan_no_debug(), io_new_conn_((fd), (plan)))
32 struct io_conn *io_new_conn_(int fd, struct io_plan plan);
35 * io_set_finish - set finish function on a connection.
36 * @conn: the connection.
37 * @finish: the function to call when it's closed or fails.
38 * @arg: the argument to @finish.
40 * @finish will be called when an I/O operation fails, or you call
41 * io_close() on the connection. errno will be set to the value
42 * after the failed I/O, or at the call to io_close(). The fd
43 * will be closed (unless a duplex) before @finish is called.
46 * static void finish(struct io_conn *conn, void *unused)
48 * // errno is not 0 after success, so this is a bit useless.
49 * printf("Conn %p closed with errno %i\n", conn, errno);
52 * io_set_finish(conn, finish, NULL);
54 #define io_set_finish(conn, finish, arg) \
55 io_set_finish_((conn), \
56 typesafe_cb_preargs(void, void *, \
60 void io_set_finish_(struct io_conn *conn,
61 void (*finish)(struct io_conn *, void *),
65 * io_new_listener - create a new accepting listener.
66 * @fd: the file descriptor.
67 * @init: the function to call for a new connection
68 * @arg: the argument to @init.
70 * When @fd becomes readable, we accept() and pass that fd to init().
72 * Returns NULL on error (and sets errno).
75 * #include <sys/types.h>
76 * #include <sys/socket.h>
79 * static void start_conn(int fd, char *msg)
81 * printf("%s fd %i\n", msg, fd);
85 * // Set up a listening socket, return it.
86 * static struct io_listener *do_listen(const char *port)
88 * struct addrinfo *addrinfo, hints;
91 * memset(&hints, 0, sizeof(hints));
92 * hints.ai_family = AF_UNSPEC;
93 * hints.ai_socktype = SOCK_STREAM;
94 * hints.ai_flags = AI_PASSIVE;
95 * hints.ai_protocol = 0;
97 * if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
100 * fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
101 * addrinfo->ai_protocol);
105 * freeaddrinfo(addrinfo);
106 * setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
107 * if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
111 * if (listen(fd, 1) != 0) {
115 * return io_new_listener(fd, start_conn, (char *)"Got one!");
118 #define io_new_listener(fd, init, arg) \
119 io_new_listener_((fd), \
120 typesafe_cb_preargs(void, void *, \
124 struct io_listener *io_new_listener_(int fd,
125 void (*init)(int fd, void *arg),
129 * io_close_listener - delete a listener.
130 * @listener: the listener returned from io_new_listener.
132 * This closes the fd and frees @listener.
136 * struct io_listener *l = do_listen("8111");
139 * io_close_listener(l);
142 void io_close_listener(struct io_listener *listener);
145 * io_write - plan to write data.
146 * @data: the data buffer.
147 * @len: the length to write.
148 * @cb: function to call once it's done.
151 * This creates a plan write out a data buffer. Once it's all
152 * written, the @cb function will be called: on an error, the finish
153 * function is called instead.
155 * Note that the I/O may actually be done immediately.
158 * static void start_conn_with_write(int fd, const char *msg)
160 * // Write message, then close.
161 * io_new_conn(fd, io_write(msg, strlen(msg), io_close_cb, NULL));
164 #define io_write(data, len, cb, arg) \
165 io_debug(io_write_((data), (len), \
166 typesafe_cb_preargs(struct io_plan, void *, \
167 (cb), (arg), struct io_conn *), \
169 struct io_plan io_write_(const void *data, size_t len,
170 struct io_plan (*cb)(struct io_conn *, void *),
174 * io_read - plan to read data.
175 * @data: the data buffer.
176 * @len: the length to read.
177 * @cb: function to call once it's done.
180 * This creates a plan to read data into a buffer. Once it's all
181 * read, the @cb function will be called: on an error, the finish
182 * function is called instead.
184 * Note that the I/O may actually be done immediately.
187 * static void start_conn_with_read(int fd, char msg[12])
189 * // Read message, then close.
190 * io_new_conn(fd, io_read(msg, 12, io_close_cb, NULL));
193 #define io_read(data, len, cb, arg) \
194 io_debug(io_read_((data), (len), \
195 typesafe_cb_preargs(struct io_plan, void *, \
196 (cb), (arg), struct io_conn *), \
198 struct io_plan io_read_(void *data, size_t len,
199 struct io_plan (*cb)(struct io_conn *, void *),
204 * io_read_partial - plan to read some data.
205 * @data: the data buffer.
206 * @len: the maximum length to read, set to the length actually read.
207 * @cb: function to call once it's done.
210 * This creates a plan to read data into a buffer. Once any data is
211 * read, @len is updated and the @cb function will be called: on an
212 * error, the finish function is called instead.
214 * Note that the I/O may actually be done immediately.
222 * static struct io_plan dump_and_close(struct io_conn *conn, struct buf *b)
224 * printf("Partial read: '%*s'\n", (int)b->len, b->buf);
229 * static void start_conn_with_part_read(int fd, void *unused)
231 * struct buf *b = malloc(sizeof(*b));
233 * // Read message, then dump and close.
234 * b->len = sizeof(b->buf);
235 * io_new_conn(fd, io_read_partial(b->buf, &b->len, dump_and_close, b));
238 #define io_read_partial(data, len, cb, arg) \
239 io_debug(io_read_partial_((data), (len), \
240 typesafe_cb_preargs(struct io_plan, void *, \
244 struct io_plan io_read_partial_(void *data, size_t *len,
245 struct io_plan (*cb)(struct io_conn *, void *),
249 * io_write_partial - plan to write some data.
250 * @data: the data buffer.
251 * @len: the maximum length to write, set to the length actually written.
252 * @cb: function to call once it's done.
255 * This creates a plan to write data from a buffer. Once any data is
256 * written, @len is updated and the @cb function will be called: on an
257 * error, the finish function is called instead.
259 * Note that the I/O may actually be done immediately.
267 * static struct io_plan show_remainder(struct io_conn *conn, struct buf *b)
269 * printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
274 * static void start_conn_with_part_read(int fd, void *unused)
276 * struct buf *b = malloc(sizeof(*b));
278 * // Write message, then dump and close.
279 * b->len = sizeof(b->buf);
280 * strcpy(b->buf, "Hello world");
281 * io_new_conn(fd, io_write_partial(b->buf, &b->len, show_remainder, b));
284 #define io_write_partial(data, len, cb, arg) \
285 io_debug(io_write_partial_((data), (len), \
286 typesafe_cb_preargs(struct io_plan, void *, \
290 struct io_plan io_write_partial_(const void *data, size_t *len,
291 struct io_plan (*cb)(struct io_conn *, void*),
295 * io_always - plan to immediately call next callback.
296 * @cb: function to call.
299 * Sometimes it's neater to plan a callback rather than call it directly;
300 * for example, if you only need to read data for one path and not another.
303 * static void start_conn_with_nothing(int fd)
305 * // Silly example: close on next time around loop.
306 * io_new_conn(fd, io_always(io_close_cb, NULL));
309 #define io_always(cb, arg) \
310 io_debug(io_always_(typesafe_cb_preargs(struct io_plan, void *, \
314 struct io_plan io_always_(struct io_plan (*cb)(struct io_conn *, void *),
318 * io_connect - plan to connect to a listening socket.
319 * @fd: file descriptor.
320 * @addr: where to connect.
321 * @cb: function to call once it's done.
324 * This initiates a connection, and creates a plan for
325 * (asynchronously). completing it. Once complete, @len is updated
326 * and the @cb function will be called: on an error, the finish
327 * function is called instead.
329 * Note that the connect may actually be done immediately.
332 * #include <sys/types.h>
333 * #include <sys/socket.h>
336 * // Write, then close socket.
337 * static struct io_plan start_write(struct io_conn *conn, void *unused)
339 * return io_write("hello", 5, io_close_cb, NULL);
345 * struct addrinfo *addrinfo;
347 * fd = socket(AF_INET, SOCK_STREAM, 0);
348 * getaddrinfo("localhost", "8111", NULL, &addrinfo);
349 * io_new_conn(fd, io_connect(fd, addrinfo, start_write, NULL));
352 #define io_connect(fd, addr, cb, arg) \
353 io_debug(io_connect_((fd), (addr), \
354 typesafe_cb_preargs(struct io_plan, void *, \
358 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
359 struct io_plan (*cb)(struct io_conn *, void*),
363 * io_idle - plan to do nothing.
365 * This indicates the connection is idle: io_wake() will be called later do
366 * give the connection a new plan.
369 * struct io_conn *sleeper;
370 * sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
374 #define io_idle() io_debug(io_idle_())
375 struct io_plan io_idle_(void);
378 * io_timeout - set timeout function if the callback doesn't complete.
379 * @conn: the current connection.
380 * @ts: how long until the timeout should be called.
381 * @cb: callback to call.
382 * @arg: argument to @cb.
384 * If the usual next callback is not called for this connection before @ts,
385 * this function will be called. If next callback is called, the timeout
386 * is automatically removed.
388 * Returns false on allocation failure. A connection can only have one
392 * static struct io_plan close_on_timeout(struct io_conn *conn, char *msg)
394 * printf("%s\n", msg);
399 * io_timeout(sleeper, time_from_msec(100),
400 * close_on_timeout, (char *)"Bye!");
402 #define io_timeout(conn, ts, fn, arg) \
403 io_timeout_((conn), (ts), \
404 typesafe_cb_preargs(struct io_plan, void *, \
408 bool io_timeout_(struct io_conn *conn, struct timespec ts,
409 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
412 * io_duplex - split an fd into two connections.
413 * @conn: a connection.
414 * @plan: the first I/O function to call.
416 * Sometimes you want to be able to simultaneously read and write on a
417 * single fd, but io forces a linear call sequence. The solution is
418 * to have two connections for the same fd, and use one for read
419 * operations and one for write.
421 * You must io_close() both of them to close the fd.
424 * static void setup_read_write(int fd,
425 * char greet_in[5], const char greet_out[5])
427 * struct io_conn *writer, *reader;
429 * // Read their greeting and send ours at the same time.
430 * writer = io_new_conn(fd,
431 * io_write(greet_out, 5, io_close_cb, NULL));
432 * reader = io_duplex(writer,
433 * io_read(greet_in, 5, io_close_cb, NULL));
434 * if (!reader || !writer)
438 #define io_duplex(conn, plan) \
439 (io_plan_no_debug(), io_duplex_((conn), (plan)))
440 struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
443 * io_wake - wake up an idle connection.
444 * @conn: an idle connection.
445 * @plan: the next I/O plan for @conn.
447 * This makes @conn ready to do I/O the next time around the io_loop().
450 * struct io_conn *sleeper;
451 * sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
453 * io_wake(sleeper, io_write("junk", 4, io_close_cb, NULL));
455 #define io_wake(conn, plan) (io_plan_no_debug(), io_wake_((conn), (plan)))
456 void io_wake_(struct io_conn *conn, struct io_plan plan);
459 * io_is_idle - is a connection idle?
461 * This can be useful for complex protocols, eg. where you want a connection
462 * to send something, so you queue it and wake it if it's idle.
465 * struct io_conn *sleeper;
466 * sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
468 * assert(io_is_idle(sleeper));
469 * io_wake(sleeper, io_write("junk", 4, io_close_cb, NULL));
471 bool io_is_idle(const struct io_conn *conn);
474 * io_break - return from io_loop()
475 * @ret: non-NULL value to return from io_loop().
476 * @plan: I/O to perform on return (if any)
478 * This breaks out of the io_loop. As soon as the current @next
479 * function returns, any io_closed()'d connections will have their
480 * finish callbacks called, then io_loop() with return with @ret.
482 * If io_loop() is called again, then @plan will be carried out.
485 * static struct io_plan fail_on_timeout(struct io_conn *conn, char *msg)
487 * return io_break(msg, io_close());
490 #define io_break(ret, plan) (io_plan_no_debug(), io_break_((ret), (plan)))
491 struct io_plan io_break_(void *ret, struct io_plan plan);
493 /* FIXME: io_recvfrom/io_sendto */
496 * io_close - plan to close a connection.
498 * On return to io_loop, the connection will be closed.
501 * static struct io_plan close_on_timeout(struct io_conn *conn, const char *msg)
503 * printf("closing: %s\n", msg);
507 #define io_close() io_debug(io_close_())
508 struct io_plan io_close_(void);
511 * io_close_cb - helper callback to close a connection.
512 * @conn: the connection.
514 * This schedules a connection to be closed; designed to be used as
515 * a callback function.
518 * #define close_on_timeout io_close_cb
520 struct io_plan io_close_cb(struct io_conn *, void *unused);
523 * io_close_other - close different connection next time around the I/O loop.
524 * @conn: the connection to close.
526 * This is used to force a different connection to close: no more I/O will
527 * happen on @conn, even if it's pending.
529 * It's a bug to use this on the current connection!
532 * static void stop_connection(struct io_conn *conn)
534 * printf("forcing stop on connection\n");
535 * io_close_other(conn);
538 void io_close_other(struct io_conn *conn);
541 * io_loop - process fds until all closed on io_break.
543 * This is the core loop; it exits with the io_break() arg, or NULL if
544 * all connections and listeners are closed.
552 * io_conn_fd - get the fd from a connection.
553 * @conn: the connection.
555 * Sometimes useful, eg for getsockname().
557 int io_conn_fd(const struct io_conn *conn);
560 * io_set_alloc - set alloc/realloc/free function for io to use.
561 * @allocfn: allocator function
562 * @reallocfn: reallocator function, ptr may be NULL, size never 0.
563 * @freefn: free function
565 * By default io uses malloc/realloc/free, and returns NULL if they fail.
566 * You can set your own variants here.
568 void io_set_alloc(void *(*allocfn)(size_t size),
569 void *(*reallocfn)(void *ptr, size_t size),
570 void (*freefn)(void *ptr));
571 #endif /* CCAN_IO_H */