]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.h
ccan/io: io_duplex.
[ccan] / ccan / io / io.h
index 49b6a25e83bba172e483e6c7749056744146ef52..5ca9731dc517226fb23a1b2620b23a79d749a809 100644 (file)
@@ -151,6 +151,33 @@ struct io_op *io_write_partial(const void *data, size_t *len,
  */
 struct io_op *io_idle(struct io_conn *conn);
 
+/**
+ * io_duplex - split an fd into two connections.
+ * @conn: a connection.
+ * @start: the first function to call.
+ * @finish: the function to call when it's closed or fails.
+ * @arg: the argument to both @start and @finish.
+ *
+ * Sometimes you want to be able to simultaneously read and write on a
+ * single fd, but io forces a linear call sequence.  The solition is
+ * to have two connections for the same fd, and use one for read
+ * operations and one for write.
+ *
+ * You must io_close() both of them to close the fd.
+ */
+#define io_duplex(conn, start, finish, arg)                            \
+       io_duplex_((conn),                                              \
+                  typesafe_cb_preargs(struct io_op *, void *,          \
+                                      (start), (arg), struct io_conn *), \
+                  typesafe_cb_preargs(void, void *, (finish), (arg),   \
+                                      struct io_conn *),               \
+                  (arg))
+
+struct io_conn *io_duplex_(struct io_conn *conn,
+                          struct io_op *(*start)(struct io_conn *, void *),
+                          void (*finish)(struct io_conn *, void *),
+                          void *arg);
+
 /**
  * io_wake - wake up and idle connection.
  * @conn: an idle connection.