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().
45 * static void finish(struct io_conn *conn, void *unused)
47 * // errno is not 0 after success, so this is a bit useless.
48 * printf("Conn %p closed with errno %i\n", conn, errno);
51 * io_set_finish(conn, finish, NULL);
53 #define io_set_finish(conn, finish, arg) \
54 io_set_finish_((conn), \
55 typesafe_cb_preargs(void, void *, \
59 void io_set_finish_(struct io_conn *conn,
60 void (*finish)(struct io_conn *, void *),
64 * io_new_listener - create a new accepting listener.
65 * @fd: the file descriptor.
66 * @init: the function to call for a new connection
67 * @arg: the argument to @init.
69 * When @fd becomes readable, we accept() and pass that fd to init().
71 * Returns NULL on error (and sets errno).
74 * #include <sys/types.h>
75 * #include <sys/socket.h>
78 * static void start_conn(int fd, char *msg)
80 * printf("%s fd %i\n", msg, fd);
84 * // Set up a listening socket, return it.
85 * static struct io_listener *do_listen(const char *port)
87 * struct addrinfo *addrinfo, hints;
90 * memset(&hints, 0, sizeof(hints));
91 * hints.ai_family = AF_UNSPEC;
92 * hints.ai_socktype = SOCK_STREAM;
93 * hints.ai_flags = AI_PASSIVE;
94 * hints.ai_protocol = 0;
96 * if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
99 * fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
100 * addrinfo->ai_protocol);
104 * freeaddrinfo(addrinfo);
105 * setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
106 * if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
110 * if (listen(fd, 1) != 0) {
114 * return io_new_listener(fd, start_conn, (char *)"Got one!");
117 #define io_new_listener(fd, init, arg) \
118 io_new_listener_((fd), \
119 typesafe_cb_preargs(void, void *, \
123 struct io_listener *io_new_listener_(int fd,
124 void (*init)(int fd, void *arg),
128 * io_close_listener - delete a listener.
129 * @listener: the listener returned from io_new_listener.
131 * This closes the fd and frees @listener.
135 * struct io_listener *l = do_listen("8111");
138 * io_close_listener(l);
141 void io_close_listener(struct io_listener *listener);
144 * io_write - plan to write data.
145 * @data: the data buffer.
146 * @len: the length to write.
147 * @cb: function to call once it's done.
150 * This creates a plan write out a data buffer. Once it's all
151 * written, the @cb function will be called: on an error, the finish
152 * function is called instead.
154 * Note that the I/O may actually be done immediately.
157 * static void start_conn_with_write(int fd, const char *msg)
159 * // Write message, then close.
160 * io_new_conn(fd, io_write(msg, strlen(msg), io_close_cb, NULL));
163 #define io_write(data, len, cb, arg) \
164 io_debug(io_write_((data), (len), \
165 typesafe_cb_preargs(struct io_plan, void *, \
166 (cb), (arg), struct io_conn *), \
168 struct io_plan io_write_(const void *data, size_t len,
169 struct io_plan (*cb)(struct io_conn *, void *),
173 * io_read - plan to read data.
174 * @data: the data buffer.
175 * @len: the length to read.
176 * @cb: function to call once it's done.
179 * This creates a plan to read data into a buffer. Once it's all
180 * read, the @cb function will be called: on an error, the finish
181 * function is called instead.
183 * Note that the I/O may actually be done immediately.
186 * static void start_conn_with_read(int fd, char msg[12])
188 * // Read message, then close.
189 * io_new_conn(fd, io_read(msg, 12, io_close_cb, NULL));
192 #define io_read(data, len, cb, arg) \
193 io_debug(io_read_((data), (len), \
194 typesafe_cb_preargs(struct io_plan, void *, \
195 (cb), (arg), struct io_conn *), \
197 struct io_plan io_read_(void *data, size_t len,
198 struct io_plan (*cb)(struct io_conn *, void *),
203 * io_read_partial - plan to read some data.
204 * @data: the data buffer.
205 * @len: the maximum length to read, set to the length actually read.
206 * @cb: function to call once it's done.
209 * This creates a plan to read data into a buffer. Once any data is
210 * read, @len is updated and the @cb function will be called: on an
211 * error, the finish function is called instead.
213 * Note that the I/O may actually be done immediately.
221 * static struct io_plan dump_and_close(struct io_conn *conn, struct buf *b)
223 * printf("Partial read: '%*s'\n", (int)b->len, b->buf);
228 * static void start_conn_with_part_read(int fd, void *unused)
230 * struct buf *b = malloc(sizeof(*b));
232 * // Read message, then dump and close.
233 * b->len = sizeof(b->buf);
234 * io_new_conn(fd, io_read_partial(b->buf, &b->len, dump_and_close, b));
237 #define io_read_partial(data, len, cb, arg) \
238 io_debug(io_read_partial_((data), (len), \
239 typesafe_cb_preargs(struct io_plan, void *, \
243 struct io_plan io_read_partial_(void *data, size_t *len,
244 struct io_plan (*cb)(struct io_conn *, void *),
248 * io_write_partial - plan to write some data.
249 * @data: the data buffer.
250 * @len: the maximum length to write, set to the length actually written.
251 * @cb: function to call once it's done.
254 * This creates a plan to write data from a buffer. Once any data is
255 * written, @len is updated and the @cb function will be called: on an
256 * error, the finish function is called instead.
258 * Note that the I/O may actually be done immediately.
266 * static struct io_plan show_remainder(struct io_conn *conn, struct buf *b)
268 * printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
273 * static void start_conn_with_part_read(int fd, void *unused)
275 * struct buf *b = malloc(sizeof(*b));
277 * // Write message, then dump and close.
278 * b->len = sizeof(b->buf);
279 * strcpy(b->buf, "Hello world");
280 * io_new_conn(fd, io_write_partial(b->buf, &b->len, show_remainder, b));
283 #define io_write_partial(data, len, cb, arg) \
284 io_debug(io_write_partial_((data), (len), \
285 typesafe_cb_preargs(struct io_plan, void *, \
289 struct io_plan io_write_partial_(const void *data, size_t *len,
290 struct io_plan (*cb)(struct io_conn *, void*),
294 * io_connect - plan to connect to a listening socket.
295 * @fd: file descriptor.
296 * @addr: where to connect.
297 * @cb: function to call once it's done.
300 * This initiates a connection, and creates a plan for
301 * (asynchronously). completing it. Once complete, @len is updated
302 * and the @cb function will be called: on an error, the finish
303 * function is called instead.
305 * Note that the connect may actually be done immediately.
308 * #include <sys/types.h>
309 * #include <sys/socket.h>
312 * // Write, then close socket.
313 * static struct io_plan start_write(struct io_conn *conn, void *unused)
315 * return io_write("hello", 5, io_close_cb, NULL);
321 * struct addrinfo *addrinfo;
323 * fd = socket(AF_INET, SOCK_STREAM, 0);
324 * getaddrinfo("localhost", "8111", NULL, &addrinfo);
325 * io_new_conn(fd, io_connect(fd, addrinfo, start_write, NULL));
328 #define io_connect(fd, addr, cb, arg) \
329 io_debug(io_connect_((fd), (addr), \
330 typesafe_cb_preargs(struct io_plan, void *, \
334 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
335 struct io_plan (*cb)(struct io_conn *, void*),
339 * io_idle - plan to do nothing.
341 * This indicates the connection is idle: io_wake() will be called later do
342 * give the connection a new plan.
345 * struct io_conn *sleeper;
346 * sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
350 #define io_idle() io_debug(io_idle_())
351 struct io_plan io_idle_(void);
354 * io_timeout - set timeout function if the callback doesn't complete.
355 * @conn: the current connection.
356 * @ts: how long until the timeout should be called.
357 * @cb: callback to call.
358 * @arg: argument to @cb.
360 * If the usual next callback is not called for this connection before @ts,
361 * this function will be called. If next callback is called, the timeout
362 * is automatically removed.
364 * Returns false on allocation failure. A connection can only have one
368 * static struct io_plan close_on_timeout(struct io_conn *conn, char *msg)
370 * printf("%s\n", msg);
375 * io_timeout(sleeper, time_from_msec(100),
376 * close_on_timeout, (char *)"Bye!");
378 #define io_timeout(conn, ts, fn, arg) \
379 io_timeout_((conn), (ts), \
380 typesafe_cb_preargs(struct io_plan, void *, \
384 bool io_timeout_(struct io_conn *conn, struct timespec ts,
385 struct io_plan (*fn)(struct io_conn *, void *), void *arg);
388 * io_duplex - split an fd into two connections.
389 * @conn: a connection.
390 * @plan: the first I/O function to call.
392 * Sometimes you want to be able to simultaneously read and write on a
393 * single fd, but io forces a linear call sequence. The solution is
394 * to have two connections for the same fd, and use one for read
395 * operations and one for write.
397 * You must io_close() both of them to close the fd.
400 * static void setup_read_write(int fd,
401 * char greet_in[5], const char greet_out[5])
403 * struct io_conn *writer, *reader;
405 * // Read their greeting and send ours at the same time.
406 * writer = io_new_conn(fd,
407 * io_write(greet_out, 5, io_close_cb, NULL));
408 * reader = io_duplex(writer,
409 * io_read(greet_in, 5, io_close_cb, NULL));
410 * if (!reader || !writer)
414 #define io_duplex(conn, plan) \
415 (io_plan_no_debug(), io_duplex_((conn), (plan)))
416 struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
419 * io_wake - wake up an idle connection.
420 * @conn: an idle connection.
421 * @plan: the next I/O plan for @conn.
423 * This makes @conn ready to do I/O the next time around the io_loop().
426 * struct io_conn *sleeper;
427 * sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
429 * io_wake(sleeper, io_write("junk", 4, io_close_cb, NULL));
431 #define io_wake(conn, plan) (io_plan_no_debug(), io_wake_((conn), (plan)))
432 void io_wake_(struct io_conn *conn, struct io_plan plan);
435 * io_break - return from io_loop()
436 * @ret: non-NULL value to return from io_loop().
437 * @plan: I/O to perform on return (if any)
439 * This breaks out of the io_loop. As soon as the current @next
440 * function returns, any io_closed()'d connections will have their
441 * finish callbacks called, then io_loop() with return with @ret.
443 * If io_loop() is called again, then @plan will be carried out.
446 * static struct io_plan fail_on_timeout(struct io_conn *conn, char *msg)
448 * return io_break(msg, io_close());
451 #define io_break(ret, plan) (io_plan_no_debug(), io_break_((ret), (plan)))
452 struct io_plan io_break_(void *ret, struct io_plan plan);
454 /* FIXME: io_recvfrom/io_sendto */
457 * io_close - plan to close a connection.
459 * On return to io_loop, the connection will be closed.
462 * static struct io_plan close_on_timeout(struct io_conn *conn, const char *msg)
464 * printf("closing: %s\n", msg);
468 #define io_close() io_debug(io_close_())
469 struct io_plan io_close_(void);
472 * io_close_cb - helper callback to close a connection.
473 * @conn: the connection.
475 * This schedules a connection to be closed; designed to be used as
476 * a callback function.
479 * #define close_on_timeout io_close_cb
481 struct io_plan io_close_cb(struct io_conn *, void *unused);
484 * io_loop - process fds until all closed on io_break.
486 * This is the core loop; it exits with the io_break() arg, or NULL if
487 * all connections and listeners are closed.
495 * io_set_alloc - set alloc/realloc/free function for io to use.
496 * @allocfn: allocator function
497 * @reallocfn: reallocator function, ptr may be NULL, size never 0.
498 * @freefn: free function
500 * By default io uses malloc/realloc/free, and returns NULL if they fail.
501 * You can set your own variants here.
503 void io_set_alloc(void *(*allocfn)(size_t size),
504 void *(*reallocfn)(void *ptr, size_t size),
505 void (*freefn)(void *ptr));
506 #endif /* CCAN_IO_H */