]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.h
io: io_always, and zero-length operations support.
[ccan] / ccan / io / io.h
index df0764bd17e1ff7f50633ffe91a45de4c02c6a22..bcdb11fdd0e0c1f1e9a08aac55a13e0afe888d92 100644 (file)
@@ -290,6 +290,29 @@ struct io_plan io_write_partial_(const void *data, size_t *len,
                                 struct io_plan (*cb)(struct io_conn *, void*),
                                 void *arg);
 
+/**
+ * io_always - plan to immediately call next callback.
+ * @cb: function to call.
+ * @arg: @cb argument
+ *
+ * Sometimes it's neater to plan a callback rather than call it directly;
+ * for example, if you only need to read data for one path and not another.
+ *
+ * Example:
+ * static void start_conn_with_nothing(int fd)
+ * {
+ *     // Silly example: close on next time around loop.
+ *     io_new_conn(fd, io_always(io_close_cb, NULL));
+ * }
+ */
+#define io_always(cb, arg)                                             \
+       io_debug(io_always_(typesafe_cb_preargs(struct io_plan, void *, \
+                                               (cb), (arg),            \
+                                               struct io_conn *),      \
+                           (arg)))
+struct io_plan io_always_(struct io_plan (*cb)(struct io_conn *, void *),
+                         void *arg);
+
 /**
  * io_connect - plan to connect to a listening socket.
  * @fd: file descriptor.
@@ -394,7 +417,7 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts,
  * to have two connections for the same fd, and use one for read
  * operations and one for write.
  *
- * Returning io_close() on one will close both fds!
+ * You must io_close() both of them to close the fd.
  *
  * Example:
  *     static void setup_read_write(int fd,
@@ -495,6 +518,24 @@ struct io_plan io_close_(void);
  */
 struct io_plan io_close_cb(struct io_conn *, void *unused);
 
+/**
+ * io_close_other - close different connection next time around the I/O loop.
+ * @conn: the connection to close.
+ *
+ * This is used to force a different connection to close: no more I/O will
+ * happen on @conn, even if it's pending.
+ *
+ * It's a bug to use this on the current connection!
+ *
+ * Example:
+ * static void stop_connection(struct io_conn *conn)
+ * {
+ *     printf("forcing stop on connection\n");
+ *     io_close_other(conn);
+ * }
+ */
+void io_close_other(struct io_conn *conn);
+
 /**
  * io_loop - process fds until all closed on io_break.
  *