X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fio%2Fio.h;h=8199bda9802050f747440f66ec1da97f987fcb47;hp=9ba46b67b497a93af6c429954a4e3013ba2e4742;hb=3a7b8a8a8081ebbb6457527de376dec6264bc381;hpb=737705f0c2ec60ea5b51ca55299488d86db37b5d diff --git a/ccan/io/io.h b/ccan/io/io.h index 9ba46b67..8199bda9 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -8,39 +8,6 @@ struct io_conn; -struct io_state_read { - char *buf; - size_t len; -}; - -struct io_state_write { - const char *buf; - size_t len; -}; - -struct io_state_readpart { - char *buf; - size_t *lenp; -}; - -struct io_state_writepart { - const char *buf; - size_t *lenp; -}; - -enum io_result { - RESULT_AGAIN, - RESULT_FINISHED, - RESULT_CLOSE -}; - -enum io_state { - IO_IO, - IO_NEXT, /* eg starting, woken from idle, return from io_break. */ - IO_IDLE, - IO_FINISHED -}; - /** * struct io_plan - returned from a setup function. * @@ -48,47 +15,86 @@ enum io_state { */ struct io_plan { int pollflag; - enum io_state state; - enum io_result (*io)(struct io_conn *conn); + /* Only NULL if idle. */ + int (*io)(int fd, struct io_plan *plan); + /* Only NULL if closing. */ struct io_plan (*next)(struct io_conn *, void *arg); void *next_arg; union { - struct io_state_read read; - struct io_state_write write; - struct io_state_readpart readpart; - struct io_state_writepart writepart; + struct { + char *buf; + size_t len; + } read; + struct { + const char *buf; + size_t len; + } write; + struct { + char *buf; + size_t *lenp; + } readpart; + struct { + const char *buf; + size_t *lenp; + } writepart; + struct { + void *p; + size_t len; + } ptr_len; + struct { + void *p1; + void *p2; + } ptr_ptr; + struct { + size_t len1; + size_t len2; + } len_len; } u; }; +#ifdef DEBUG +extern bool io_plan_for_other; +extern bool (*io_debug)(struct io_conn *conn); +#define io_plan_other() ((io_plan_for_other = true)) +void io_plan_debug(struct io_plan *plan); +#else +#define io_plan_other() (void)0 +static inline void io_plan_debug(struct io_plan *plan) { } +#endif + /** * io_new_conn - create a new connection. * @fd: the file descriptor. - * @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. - * - * This creates a connection which owns @fd. @start will be called on the - * next return to io_loop(), and @finish will be called when an I/O operation - * fails, or you call io_close() on the connection. + * @plan: the first I/O function. * - * The @start function must call one of the io queueing functions - * (eg. io_read, io_write) and return the next function to call once - * that is done using io_next(). The alternative is to call io_close(). + * This creates a connection which owns @fd. @plan will be called on the + * next io_loop(). * * Returns NULL on error (and sets errno). */ -#define io_new_conn(fd, start, finish, arg) \ - io_new_conn_((fd), \ - typesafe_cb_preargs(struct io_plan, void *, \ - (start), (arg), struct io_conn *), \ - typesafe_cb_preargs(void, void *, (finish), (arg), \ - struct io_conn *), \ - (arg)) -struct io_conn *io_new_conn_(int fd, - struct io_plan (*start)(struct io_conn *, void *), - void (*finish)(struct io_conn *, void *), - void *arg); +#define io_new_conn(fd, plan) \ + (io_plan_other(), io_new_conn_((fd), (plan))) +struct io_conn *io_new_conn_(int fd, struct io_plan plan); + +/** + * io_set_finish - set finish function on a connection. + * @conn: the connection. + * @finish: the function to call when it's closed or fails. + * @arg: the argument to @finish. + * + * @finish will be called when an I/O operation fails, or you call + * io_close() on the connection. + */ +#define io_set_finish(conn, finish, arg) \ + io_set_finish_((conn), \ + typesafe_cb_preargs(void, void *, \ + (finish), (arg), \ + struct io_conn *), \ + (arg)) +void io_set_finish_(struct io_conn *conn, + void (*finish)(struct io_conn *, void *), + void *arg); /** * io_new_listener - create a new accepting listener. @@ -243,9 +249,7 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts, /** * 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. + * @plan: the first I/O function to call. * * 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 @@ -254,71 +258,52 @@ 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, start, finish, arg) \ - io_duplex_((conn), \ - typesafe_cb_preargs(struct io_plan, void *, \ - (start), (arg), struct io_conn *), \ - typesafe_cb_preargs(void, void *, (finish), (arg), \ - struct io_conn *), \ - (arg)) +#define io_duplex(conn, plan) \ + (io_plan_other(), io_duplex_((conn), (plan))) -struct io_conn *io_duplex_(struct io_conn *conn, - struct io_plan (*start)(struct io_conn *, void *), - void (*finish)(struct io_conn *, void *), - void *arg); +struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan); /** - * io_wake - wake up and idle connection. + * io_wake - wake up an idle connection. * @conn: an idle connection. - * @fn: the next function to call once queued IO is complete. - * @arg: the argument to @next. + * @plan: the next I/O function for @conn. * - * This makes @conn run its @next function the next time around the - * io_loop(). + * This makes @conn do I/O the next time around the io_loop(). */ -#define io_wake(conn, fn, arg) \ - io_wake_((conn), \ - typesafe_cb_preargs(struct io_plan, void *, \ - (fn), (arg), struct io_conn *), \ - (arg)) -void io_wake_(struct io_conn *conn, - struct io_plan (*fn)(struct io_conn *, void *), void *arg); +#define io_wake(conn, plan) (io_plan_other(), io_wake_((conn), (plan))) +void io_wake_(struct io_conn *conn, struct io_plan plan); /** * io_break - return from io_loop() * @ret: non-NULL value to return from io_loop(). - * @cb: function to call once on return - * @arg: @cb argument + * @plan: I/O to perform on return (if any) * * This breaks out of the io_loop. As soon as the current @next * function returns, any io_closed()'d connections will have their * finish callbacks called, then io_loop() with return with @ret. * - * If io_loop() is called again, then @cb will be called. + * If io_loop() is called again, then @plan will be carried out. */ -#define io_break(ret, fn, arg) \ - io_break_((ret), \ - typesafe_cb_preargs(struct io_plan, void *, \ - (fn), (arg), struct io_conn *), \ - (arg)) -struct io_plan io_break_(void *ret, - struct io_plan (*fn)(struct io_conn *, void *), - void *arg); +#define io_break(ret, plan) (io_plan_other(), io_break_((ret), (plan))) +struct io_plan io_break_(void *ret, struct io_plan plan); /* FIXME: io_recvfrom/io_sendto */ /** - * io_close - terminate a connection. - * @conn: any connection. + * io_close - plan to close a connection. * - * The schedules a connection to be closed. It can be done on any - * connection, whether it has I/O queued or not (though that I/O may - * be performed first). + * On return to io_loop, the connection will be closed. + */ +struct io_plan io_close(void); + +/** + * io_close_cb - helper callback to close a connection. + * @conn: the connection. * - * It's common to 'return io_close(...)' from a @next function, but - * io_close can also be used as an argument to io_next(). + * This schedules a connection to be closed; designed to be used as + * a callback function. */ -struct io_plan io_close(struct io_conn *, void *unused); +struct io_plan io_close_cb(struct io_conn *, void *unused); /** * io_loop - process fds until all closed on io_break.