]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.h
io: call finish function after fd is actually closed.
[ccan] / ccan / io / io.h
index b5ffdd243bfd8a1710e551741261bb9159923cff..ecbeb0b093bf6f0c43054b9a67e1233cff41bf66 100644 (file)
@@ -39,7 +39,8 @@ struct io_conn *io_new_conn_(int fd, struct io_plan plan);
  *
  * @finish will be called when an I/O operation fails, or you call
  * io_close() on the connection.  errno will be set to the value
- * after the failed I/O, or at the call to io_close().
+ * after the failed I/O, or at the call to io_close().  The fd
+ * will be closed (unless a duplex) before @finish is called.
  *
  * Example:
  * static void finish(struct io_conn *conn, void *unused)
@@ -290,6 +291,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.
@@ -431,6 +455,21 @@ struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
 #define io_wake(conn, plan) (io_plan_no_debug(), io_wake_((conn), (plan)))
 void io_wake_(struct io_conn *conn, struct io_plan plan);
 
+/**
+ * io_is_idle - is a connection idle?
+ *
+ * This can be useful for complex protocols, eg. where you want a connection
+ * to send something, so you queue it and wake it if it's idle.
+ *
+ * Example:
+ *     struct io_conn *sleeper;
+ *     sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
+ *
+ *     assert(io_is_idle(sleeper));
+ *     io_wake(sleeper, io_write("junk", 4, io_close_cb, NULL));
+ */
+bool io_is_idle(const struct io_conn *conn);
+
 /**
  * io_break - return from io_loop()
  * @ret: non-NULL value to return from io_loop().
@@ -480,6 +519,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.
  *
@@ -490,4 +547,25 @@ struct io_plan io_close_cb(struct io_conn *, void *unused);
  *     io_loop();
  */
 void *io_loop(void);
+
+/**
+ * io_conn_fd - get the fd from a connection.
+ * @conn: the connection.
+ *
+ * Sometimes useful, eg for getsockname().
+ */
+int io_conn_fd(const struct io_conn *conn);
+
+/**
+ * io_set_alloc - set alloc/realloc/free function for io to use.
+ * @allocfn: allocator function
+ * @reallocfn: reallocator function, ptr may be NULL, size never 0.
+ * @freefn: free function
+ *
+ * By default io uses malloc/realloc/free, and returns NULL if they fail.
+ * You can set your own variants here.
+ */
+void io_set_alloc(void *(*allocfn)(size_t size),
+                 void *(*reallocfn)(void *ptr, size_t size),
+                 void (*freefn)(void *ptr));
 #endif /* CCAN_IO_H */