]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.h
ccan/io: io_connect()
[ccan] / ccan / io / io.h
index 828fb666f50fcde0acdfbe107467c000ecedd2ba..772db9293ab068e7ed425ea282d6b7e0fc07b7ce 100644 (file)
@@ -18,7 +18,7 @@
  * Returns NULL on error (and sets errno).
  */
 #define io_new_conn(fd, plan)                          \
-       (io_plan_other(), io_new_conn_((fd), (plan)))
+       (io_plan_no_debug(), io_new_conn_((fd), (plan)))
 struct io_conn *io_new_conn_(int fd, struct io_plan plan);
 
 /**
@@ -83,10 +83,10 @@ void io_close_listener(struct io_listener *listener);
  * Note that the I/O may actually be done immediately.
  */
 #define io_write(data, len, cb, arg)                                   \
-       io_write_((data), (len),                                        \
-                 typesafe_cb_preargs(struct io_plan, void *,           \
-                                     (cb), (arg), struct io_conn *),   \
-                 (arg))
+       io_debug(io_write_((data), (len),                               \
+                          typesafe_cb_preargs(struct io_plan, void *,  \
+                                              (cb), (arg), struct io_conn *), \
+                          (arg)))
 struct io_plan io_write_(const void *data, size_t len,
                         struct io_plan (*cb)(struct io_conn *, void *),
                         void *arg);
@@ -105,10 +105,10 @@ struct io_plan io_write_(const void *data, size_t len,
  * Note that the I/O may actually be done immediately.
  */
 #define io_read(data, len, cb, arg)                                    \
-       io_read_((data), (len),                                         \
-                typesafe_cb_preargs(struct io_plan, void *,            \
-                                    (cb), (arg), struct io_conn *),    \
-                (arg))
+       io_debug(io_read_((data), (len),                                \
+                         typesafe_cb_preargs(struct io_plan, void *,   \
+                                             (cb), (arg), struct io_conn *), \
+                         (arg)))
 struct io_plan io_read_(void *data, size_t len,
                        struct io_plan (*cb)(struct io_conn *, void *),
                        void *arg);
@@ -128,10 +128,11 @@ struct io_plan io_read_(void *data, size_t len,
  * Note that the I/O may actually be done immediately.
  */
 #define io_read_partial(data, len, cb, arg)                            \
-       io_read_partial_((data), (len),                                 \
-                        typesafe_cb_preargs(struct io_plan, void *,    \
-                                            (cb), (arg), struct io_conn *), \
-                        (arg))
+       io_debug(io_read_partial_((data), (len),                        \
+                                 typesafe_cb_preargs(struct io_plan, void *, \
+                                                     (cb), (arg),      \
+                                                     struct io_conn *), \
+                                 (arg)))
 struct io_plan io_read_partial_(void *data, size_t *len,
                                struct io_plan (*cb)(struct io_conn *, void *),
                                void *arg);
@@ -150,21 +151,48 @@ struct io_plan io_read_partial_(void *data, size_t *len,
  * Note that the I/O may actually be done immediately.
  */
 #define io_write_partial(data, len, cb, arg)                           \
-       io_write_partial_((data), (len),                                \
-                         typesafe_cb_preargs(struct io_plan, void *,   \
-                                             (cb), (arg), struct io_conn *), \
-                         (arg))
+       io_debug(io_write_partial_((data), (len),                       \
+                                  typesafe_cb_preargs(struct io_plan, void *, \
+                                                      (cb), (arg),     \
+                                                      struct io_conn *), \
+                                  (arg)))
 struct io_plan io_write_partial_(const void *data, size_t *len,
                                 struct io_plan (*cb)(struct io_conn *, void*),
                                 void *arg);
 
+/**
+ * io_connect - plan to connect to a listening socket.
+ * @fd: file descriptor.
+ * @addr: where to connect.
+ * @cb: function to call once it's done.
+ * @arg: @cb argument
+ *
+ * This initiates a connection, and creates a plan for
+ * (asynchronously).  completing it.  Once complete, @len is updated
+ * and the @cb function will be called: on an error, the finish
+ * function is called instead.
+ *
+ * Note that the connect may actually be done immediately.
+ */
+struct addrinfo;
+#define io_connect(fd, addr, cb, arg)                                  \
+       io_debug(io_connect_((fd), (addr),                              \
+                            typesafe_cb_preargs(struct io_plan, void *, \
+                                                (cb), (arg),           \
+                                                struct io_conn *),     \
+                            (arg)))
+struct io_plan io_connect_(int fd, const struct addrinfo *addr,
+                          struct io_plan (*cb)(struct io_conn *, void*),
+                          void *arg);
+
 /**
  * io_idle - plan to do nothing.
  *
  * This indicates the connection is idle: io_wake() will be called later do
  * give the connection a new plan.
  */
-struct io_plan io_idle(void);
+#define io_idle() io_debug(io_idle_())
+struct io_plan io_idle_(void);
 
 /**
  * io_timeout - set timeout function if the callback doesn't complete.
@@ -202,7 +230,7 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts,
  * You must io_close() both of them to close the fd.
  */
 #define io_duplex(conn, plan)                          \
-       (io_plan_other(), io_duplex_((conn), (plan)))
+       (io_plan_no_debug(), io_duplex_((conn), (plan)))
 struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
 
 /**
@@ -212,7 +240,7 @@ struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
  *
  * This makes @conn ready to do I/O the next time around the io_loop().
  */
-#define io_wake(conn, plan) (io_plan_other(), io_wake_((conn), (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);
 
 /**
@@ -226,7 +254,7 @@ void io_wake_(struct io_conn *conn, struct io_plan plan);
  *
  * If io_loop() is called again, then @plan will be carried out.
  */
-#define io_break(ret, plan) (io_plan_other(), io_break_((ret), (plan)))
+#define io_break(ret, plan) (io_plan_no_debug(), io_break_((ret), (plan)))
 struct io_plan io_break_(void *ret, struct io_plan plan);
 
 /* FIXME: io_recvfrom/io_sendto */
@@ -236,7 +264,8 @@ struct io_plan io_break_(void *ret, struct io_plan plan);
  *
  * On return to io_loop, the connection will be closed.
  */
-struct io_plan io_close(void);
+#define io_close() io_debug(io_close_())
+struct io_plan io_close_(void);
 
 /**
  * io_close_cb - helper callback to close a connection.