]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
629023f92b6c0073ea2203c78293bf72305e2cb2
[ccan] / ccan / io / io.h
1 /* Licensed under LGPLv2.1+ - 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 <ccan/time/time.h>
6 #include <stdbool.h>
7 #include <unistd.h>
8
9 /**
10  * struct io_plan - pointer to return from a setup function.
11  *
12  * A plan of what IO to do, when.
13  */
14 struct io_plan;
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_plan *, 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_plan *(*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_plan *, 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_plan *(*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_plan *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_plan *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_plan *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_plan *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_plan *io_idle(struct io_conn *conn);
153
154 /**
155  * io_timeout - set timeout function if the callback doesn't fire.
156  * @conn: the current connection.
157  * @ts: how long until the timeout should be called.
158  * @next: function to call.
159  * @arg: argument to @next.
160  *
161  * If the usual next callback is not called for this connection before @ts,
162  * this function will be called.  If next callback is called, the timeout
163  * is automatically removed.
164  *
165  * Returns false on allocation failure.  A connection can only have one
166  * timeout.
167  */
168 #define io_timeout(conn, ts, next, arg) \
169         io_timeout_((conn), (ts),                                       \
170                     typesafe_cb_preargs(struct io_plan *, void *,       \
171                                         (next), (arg),                  \
172                                         struct io_conn *),              \
173                     (arg))
174
175 bool io_timeout_(struct io_conn *conn, struct timespec ts,
176                  struct io_plan *(*next)(struct io_conn *, void *), void *arg);
177
178 /**
179  * io_duplex - split an fd into two connections.
180  * @conn: a connection.
181  * @start: the first function to call.
182  * @finish: the function to call when it's closed or fails.
183  * @arg: the argument to both @start and @finish.
184  *
185  * Sometimes you want to be able to simultaneously read and write on a
186  * single fd, but io forces a linear call sequence.  The solition is
187  * to have two connections for the same fd, and use one for read
188  * operations and one for write.
189  *
190  * You must io_close() both of them to close the fd.
191  */
192 #define io_duplex(conn, start, finish, arg)                             \
193         io_duplex_((conn),                                              \
194                    typesafe_cb_preargs(struct io_plan *, void *,        \
195                                        (start), (arg), struct io_conn *), \
196                    typesafe_cb_preargs(void, void *, (finish), (arg),   \
197                                        struct io_conn *),               \
198                    (arg))
199
200 struct io_conn *io_duplex_(struct io_conn *conn,
201                            struct io_plan *(*start)(struct io_conn *, void *),
202                            void (*finish)(struct io_conn *, void *),
203                            void *arg);
204
205 /**
206  * io_wake - wake up and idle connection.
207  * @conn: an idle connection.
208  * @next: the next function to call once queued IO is complete.
209  * @arg: the argument to @next.
210  *
211  * This makes @conn run its @next function the next time around the
212  * io_loop().
213  */
214 #define io_wake(conn, next, arg)                                        \
215         io_wake_((conn),                                                \
216                  typesafe_cb_preargs(struct io_plan *, void *,          \
217                                      (next), (arg), struct io_conn *),  \
218                  (arg))
219 void io_wake_(struct io_conn *conn,
220               struct io_plan *(*next)(struct io_conn *, void *), void *arg);
221
222 /**
223  * io_break - return from io_loop()
224  * @arg: non-NULL value to return from io_loop().
225  * @next: what to call next (can be NULL if we expect no return).
226  *
227  * This breaks out of the io_loop.  As soon as the current @next
228  * function returns, any io_closed()'d connections will have their
229  * finish callbacks called, then io_loop() with return with @arg.
230  *
231  * If io_loop() is called again, then @next will be called.
232  */
233 struct io_plan *io_break(void *arg, struct io_next *next);
234
235 /**
236  * io_next - indicate what callback to call next.
237  * @conn: this connection.
238  * @next: the next function to call once queued IO is complete.
239  * @arg: the argument to @next.
240  *
241  * Every @next (or @start) function should "return io_next(...);" once
242  * they have indicated what io to perform (eg. io_write, io_idle).
243  * The exception is io_close(), which can be used instead of io_next().
244  *
245  * Note that as an optimization, the next function may be called
246  * immediately, which is why this should be the last statement in your
247  * function.
248  */
249 #define io_next(conn, next, arg)                                        \
250         io_next_((conn),                                                \
251                  typesafe_cb_preargs(struct io_plan *, void *,          \
252                                      (next), (arg), struct io_conn *),  \
253                  (arg))
254 struct io_next *io_next_(struct io_conn *conn,
255                          struct io_plan *(*next)(struct io_conn *, void *arg),
256                          void *arg);
257
258 /* FIXME: io_recvfrom/io_sendto */
259
260 /**
261  * io_close - terminate a connection.
262  * @conn: any connection.
263  *
264  * The schedules a connection to be closed.  It can be done on any
265  * connection, whether it has I/O queued or not (though that I/O may
266  * be performed first).
267  *
268  * It's common to 'return io_close(...)' from a @next function, but
269  * io_close can also be used as an argument to io_next().
270  */
271 struct io_plan *io_close(struct io_conn *, void *unused);
272
273 /**
274  * io_loop - process fds until all closed on io_break.
275  *
276  * This is the core loop; it exits with the io_break() arg, or NULL if
277  * all connections and listeners are closed.
278  */
279 void *io_loop(void);
280 #endif /* CCAN_IO_H */