]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
5ca9731dc517226fb23a1b2620b23a79d749a809
[ccan] / ccan / io / io.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_IO_H
3 #define CCAN_IO_H
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <stdbool.h>
6 #include <unistd.h>
7
8 /**
9  * struct io_op - pointer to return from io functions.
10  *
11  * This undefined structure is just to help the compiler check that you
12  * really do return the result of an io-queueing method.
13  */
14 struct io_op;
15
16 /**
17  * struct io_next - pointer to what we're going to do next.
18  *
19  * Bundles up callbacks, generated by io_next().
20  */
21 struct io_next;
22
23 /**
24  * io_new_conn - create a new connection.
25  * @fd: the file descriptor.
26  * @start: the first function to call.
27  * @finish: the function to call when it's closed or fails.
28  * @arg: the argument to both @start and @finish.
29  *
30  * This creates a connection which owns @fd.  @start will be called on the
31  * next return to io_loop(), and @finish will be called when an I/O operation
32  * fails, or you call io_close() on the connection.
33  *
34  * The @start function must call one of the io queueing functions
35  * (eg. io_read, io_write) and return the next function to call once
36  * that is done using io_next().  The alternative is to call io_close().
37  *
38  * Returns NULL on error (and sets errno).
39  */
40 #define io_new_conn(fd, start, finish, arg)                             \
41         io_new_conn_((fd),                                              \
42                      typesafe_cb_preargs(struct io_op *, void *,        \
43                                          (start), (arg), struct io_conn *), \
44                      typesafe_cb_preargs(void, void *, (finish), (arg), \
45                                          struct io_conn *),             \
46                      (arg))
47 struct io_conn *io_new_conn_(int fd,
48                              struct io_op *(*start)(struct io_conn *, void *),
49                              void (*finish)(struct io_conn *, void *),
50                              void *arg);
51
52 /**
53  * io_new_listener - create a new accepting listener.
54  * @fd: the file descriptor.
55  * @start: the first function to call on new connections.
56  * @finish: the function to call when the connection is closed or fails.
57  * @arg: the argument to both @start and @finish.
58  *
59  * When @fd becomes readable, we accept() and turn that fd into a new
60  * connection.
61  *
62  * Returns NULL on error (and sets errno).
63  */
64 #define io_new_listener(fd, start, finish, arg)                         \
65         io_new_listener_((fd),                                          \
66                          typesafe_cb_preargs(struct io_op *, void *,    \
67                                              (start), (arg),            \
68                                              struct io_conn *),         \
69                          typesafe_cb_preargs(void, void *, (finish),    \
70                                              (arg), struct io_conn *),  \
71                          (arg))
72 struct io_listener *io_new_listener_(int fd,
73                                      struct io_op *(*start)(struct io_conn *,
74                                                             void *arg),
75                                      void (*finish)(struct io_conn *,
76                                                     void *arg),
77                                      void *arg);
78
79 /**
80  * io_close_listener - delete a listener.
81  * @listener: the listener returned from io_new_listener.
82  *
83  * This closes the fd and frees @listener.
84  */
85 void io_close_listener(struct io_listener *listener);
86
87 /**
88  * io_write - queue data to be written.
89  * @data: the data buffer.
90  * @len: the length to write.
91  * @next: what to call next.
92  *
93  * This will queue the data buffer for writing.  Once it's all written, the
94  * function registered with io_next() will be called: on an error, the finish
95  * function is called instead.
96  *
97  * Note that the I/O may actually be done immediately.
98  */
99 struct io_op *io_write(const void *data, size_t len, struct io_next *next);
100
101 /**
102  * io_read - queue buffer to be read.
103  * @data: the data buffer.
104  * @len: the length to read.
105  * @next: what to call next.
106  *
107  * This will queue the data buffer for reading.  Once it's all read, the
108  * function registered with io_next() will be called: on an error, the finish
109  * function is called instead.
110  *
111  * Note that the I/O may actually be done immediately.
112  */
113 struct io_op *io_read(void *data, size_t len, struct io_next *next);
114
115 /**
116  * io_read_partial - queue buffer to be read (partial OK).
117  * @data: the data buffer.
118  * @len: the maximum length to read, set to the length actually read.
119  * @next: what to call next.
120  *
121  * This will queue the data buffer for reading.  Once any data is
122  * read, @len is updated and the function registered with io_next()
123  * will be called: on an error, the finish function is called instead.
124  *
125  * Note that the I/O may actually be done immediately.
126  */
127 struct io_op *io_read_partial(void *data, size_t *len, struct io_next *next);
128
129 /**
130  * io_write_partial - queue data to be written (partial OK).
131  * @data: the data buffer.
132  * @len: the maximum length to write, set to the length actually written.
133  * @next: what to call next.
134  *
135  * This will queue the data buffer for writing.  Once any data is
136  * written, @len is updated and the function registered with io_next()
137  * will be called: on an error, the finish function is called instead.
138  *
139  * Note that the I/O may actually be done immediately.
140  */
141 struct io_op *io_write_partial(const void *data, size_t *len,
142                                struct io_next *next);
143
144 /**
145  * io_idle - explicitly note that this connection will do nothing.
146  * @conn: the current connection.
147  *
148  * This indicates the connection is idle: some other function will
149  * later call io_read/io_write etc. (or io_close) on it, in which case
150  * it will do that.
151  */
152 struct io_op *io_idle(struct io_conn *conn);
153
154 /**
155  * io_duplex - split an fd into two connections.
156  * @conn: a connection.
157  * @start: the first function to call.
158  * @finish: the function to call when it's closed or fails.
159  * @arg: the argument to both @start and @finish.
160  *
161  * Sometimes you want to be able to simultaneously read and write on a
162  * single fd, but io forces a linear call sequence.  The solition is
163  * to have two connections for the same fd, and use one for read
164  * operations and one for write.
165  *
166  * You must io_close() both of them to close the fd.
167  */
168 #define io_duplex(conn, start, finish, arg)                             \
169         io_duplex_((conn),                                              \
170                    typesafe_cb_preargs(struct io_op *, void *,          \
171                                        (start), (arg), struct io_conn *), \
172                    typesafe_cb_preargs(void, void *, (finish), (arg),   \
173                                        struct io_conn *),               \
174                    (arg))
175
176 struct io_conn *io_duplex_(struct io_conn *conn,
177                            struct io_op *(*start)(struct io_conn *, void *),
178                            void (*finish)(struct io_conn *, void *),
179                            void *arg);
180
181 /**
182  * io_wake - wake up and idle connection.
183  * @conn: an idle connection.
184  * @next: the next function to call once queued IO is complete.
185  * @arg: the argument to @next.
186  *
187  * This makes @conn run its @next function the next time around the
188  * io_loop().
189  */
190 #define io_wake(conn, next, arg)                                        \
191         io_wake_((conn),                                                \
192                  typesafe_cb_preargs(struct io_op *, void *,            \
193                                      (next), (arg), struct io_conn *),  \
194                  (arg))
195 void io_wake_(struct io_conn *conn,
196               struct io_op *(*next)(struct io_conn *, void *), void *arg);
197
198 /**
199  * io_break - return from io_loop()
200  * @arg: non-NULL value to return from io_loop().
201  * @next: what to call next (can be NULL if we expect no return).
202  *
203  * This breaks out of the io_loop.  As soon as the current @next
204  * function returns, any io_closed()'d connections will have their
205  * finish callbacks called, then io_loop() with return with @arg.
206  *
207  * If io_loop() is called again, then @next will be called.
208  */
209 struct io_op *io_break(void *arg, struct io_next *next);
210
211 /**
212  * io_next - indicate what callback to call next.
213  * @conn: this connection.
214  * @next: the next function to call once queued IO is complete.
215  * @arg: the argument to @next.
216  *
217  * Every @next (or @start) function should "return io_next(...);" once
218  * they have indicated what io to perform (eg. io_write, io_idle).
219  * The exception is io_close(), which can be used instead of io_next().
220  *
221  * Note that as an optimization, the next function may be called
222  * immediately, which is why this should be the last statement in your
223  * function.
224  */
225 #define io_next(conn, next, arg)                                        \
226         io_next_((conn),                                                \
227                  typesafe_cb_preargs(struct io_op *, void *,            \
228                                      (next), (arg), struct io_conn *),  \
229                  (arg))
230 struct io_next *io_next_(struct io_conn *conn,
231                          struct io_op *(*next)(struct io_conn *, void *arg),
232                          void *arg);
233
234 /* FIXME: io_recvfrom/io_sendto */
235
236 /**
237  * io_close - terminate a connection.
238  * @conn: any connection.
239  *
240  * The schedules a connection to be closed.  It can be done on any
241  * connection, whether it has I/O queued or not (though that I/O may
242  * be performed first).
243  *
244  * It's common to 'return io_close(...)' from a @next function, but
245  * io_close can also be used as an argument to io_next().
246  */
247 struct io_op *io_close(struct io_conn *, void *unused);
248
249 /**
250  * io_loop - process fds until all closed on io_break.
251  *
252  * This is the core loop; it exits with the io_break() arg, or NULL if
253  * all connections and listeners are closed.
254  */
255 void *io_loop(void);
256 #endif /* CCAN_IO_H */