]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
ccan/io: generic init function for listening connections.
[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 struct io_conn;
10
11 struct io_state_read {
12         char *buf;
13         size_t len;
14 };
15
16 struct io_state_write {
17         const char *buf;
18         size_t len;
19 };
20
21 struct io_state_readpart {
22         char *buf;
23         size_t *lenp;
24 };
25
26 struct io_state_writepart {
27         const char *buf;
28         size_t *lenp;
29 };
30
31 enum io_result {
32         RESULT_AGAIN,
33         RESULT_FINISHED,
34         RESULT_CLOSE
35 };
36
37 enum io_state {
38         IO_IO,
39         IO_NEXT, /* eg starting, woken from idle, return from io_break. */
40         IO_IDLE,
41         IO_FINISHED
42 };
43
44 /**
45  * struct io_plan - returned from a setup function.
46  *
47  * A plan of what IO to do, when.
48  */
49 struct io_plan {
50         int pollflag;
51         enum io_state state;
52         enum io_result (*io)(struct io_conn *conn);
53         struct io_plan (*next)(struct io_conn *, void *arg);
54         void *next_arg;
55
56         union {
57                 struct io_state_read read;
58                 struct io_state_write write;
59                 struct io_state_readpart readpart;
60                 struct io_state_writepart writepart;
61         } u;
62 };
63
64 /**
65  * io_new_conn - create a new connection.
66  * @fd: the file descriptor.
67  * @start: the first function to call.
68  * @finish: the function to call when it's closed or fails.
69  * @arg: the argument to both @start and @finish.
70  *
71  * This creates a connection which owns @fd.  @start will be called on the
72  * next return to io_loop(), and @finish will be called when an I/O operation
73  * fails, or you call io_close() on the connection.
74  *
75  * The @start function must call one of the io queueing functions
76  * (eg. io_read, io_write) and return the next function to call once
77  * that is done using io_next().  The alternative is to call io_close().
78  *
79  * Returns NULL on error (and sets errno).
80  */
81 #define io_new_conn(fd, start, finish, arg)                             \
82         io_new_conn_((fd),                                              \
83                      typesafe_cb_preargs(struct io_plan, void *,        \
84                                          (start), (arg), struct io_conn *), \
85                      typesafe_cb_preargs(void, void *, (finish), (arg), \
86                                          struct io_conn *),             \
87                      (arg))
88 struct io_conn *io_new_conn_(int fd,
89                              struct io_plan (*start)(struct io_conn *, void *),
90                              void (*finish)(struct io_conn *, void *),
91                              void *arg);
92
93 /**
94  * io_new_listener - create a new accepting listener.
95  * @fd: the file descriptor.
96  * @init: the function to call for a new connection
97  * @arg: the argument to @init.
98  *
99  * When @fd becomes readable, we accept() and pass that fd to init().
100  *
101  * Returns NULL on error (and sets errno).
102  */
103 #define io_new_listener(fd, init, arg)                                  \
104         io_new_listener_((fd),                                          \
105                          typesafe_cb_preargs(void, void *,              \
106                                              (init), (arg),             \
107                                              int fd),                   \
108                          (arg))
109 struct io_listener *io_new_listener_(int fd,
110                                      void (*init)(int fd, void *arg),
111                                      void *arg);
112
113 /**
114  * io_close_listener - delete a listener.
115  * @listener: the listener returned from io_new_listener.
116  *
117  * This closes the fd and frees @listener.
118  */
119 void io_close_listener(struct io_listener *listener);
120
121 /**
122  * io_write - queue data to be written.
123  * @data: the data buffer.
124  * @len: the length to write.
125  * @cb: function to call once it's done.
126  * @arg: @cb argument
127  *
128  * This will queue the data buffer for writing.  Once it's all
129  * written, the @cb function will be called: on an error, the finish
130  * function is called instead.
131  *
132  * Note that the I/O may actually be done immediately.
133  */
134 #define io_write(data, len, cb, arg)                                    \
135         io_write_((data), (len),                                        \
136                   typesafe_cb_preargs(struct io_plan, void *,           \
137                                       (cb), (arg), struct io_conn *),   \
138                   (arg))
139 struct io_plan io_write_(const void *data, size_t len,
140                          struct io_plan (*cb)(struct io_conn *, void *),
141                          void *arg);
142
143 /**
144  * io_read - queue buffer to be read.
145  * @data: the data buffer.
146  * @len: the length to read.
147  * @cb: function to call once it's done.
148  * @arg: @cb argument
149  *
150  * This will queue the data buffer for reading.  Once it's all read,
151  * the @cb function will be called: on an error, the finish function
152  * is called instead.
153  *
154  * Note that the I/O may actually be done immediately.
155  */
156 #define io_read(data, len, cb, arg)                                     \
157         io_read_((data), (len),                                         \
158                  typesafe_cb_preargs(struct io_plan, void *,            \
159                                      (cb), (arg), struct io_conn *),    \
160                  (arg))
161 struct io_plan io_read_(void *data, size_t len,
162                         struct io_plan (*cb)(struct io_conn *, void *),
163                         void *arg);
164
165
166 /**
167  * io_read_partial - queue buffer to be read (partial OK).
168  * @data: the data buffer.
169  * @len: the maximum length to read, set to the length actually read.
170  * @cb: function to call once it's done.
171  * @arg: @cb argument
172  *
173  * This will queue the data buffer for reading.  Once any data is
174  * read, @len is updated and the @cb function will be called: on an
175  * error, the finish function is called instead.
176  *
177  * Note that the I/O may actually be done immediately.
178  */
179 #define io_read_partial(data, len, cb, arg)                             \
180         io_read_partial_((data), (len),                                 \
181                          typesafe_cb_preargs(struct io_plan, void *,    \
182                                              (cb), (arg), struct io_conn *), \
183                          (arg))
184 struct io_plan io_read_partial_(void *data, size_t *len,
185                                 struct io_plan (*cb)(struct io_conn *, void *),
186                                 void *arg);
187
188 /**
189  * io_write_partial - queue data to be written (partial OK).
190  * @data: the data buffer.
191  * @len: the maximum length to write, set to the length actually written.
192  * @cb: function to call once it's done.
193  * @arg: @cb argument
194  *
195  * This will queue the data buffer for writing.  Once any data is
196  * written, @len is updated and the @cb function will be called: on an
197  * error, the finish function is called instead.
198  *
199  * Note that the I/O may actually be done immediately.
200  */
201 #define io_write_partial(data, len, cb, arg)                            \
202         io_write_partial_((data), (len),                                \
203                           typesafe_cb_preargs(struct io_plan, void *,   \
204                                               (cb), (arg), struct io_conn *), \
205                           (arg))
206 struct io_plan io_write_partial_(const void *data, size_t *len,
207                                  struct io_plan (*cb)(struct io_conn *, void*),
208                                  void *arg);
209
210
211 /**
212  * io_idle - explicitly note that this connection will do nothing.
213  *
214  * This indicates the connection is idle: some other function will
215  * later call io_read/io_write etc. (or io_close) on it, in which case
216  * it will do that.
217  */
218 struct io_plan io_idle(void);
219
220 /**
221  * io_timeout - set timeout function if the callback doesn't fire.
222  * @conn: the current connection.
223  * @ts: how long until the timeout should be called.
224  * @cb to call.
225  * @arg: argument to @cb.
226  *
227  * If the usual next callback is not called for this connection before @ts,
228  * this function will be called.  If next callback is called, the timeout
229  * is automatically removed.
230  *
231  * Returns false on allocation failure.  A connection can only have one
232  * timeout.
233  */
234 #define io_timeout(conn, ts, fn, arg)                                   \
235         io_timeout_((conn), (ts),                                       \
236                     typesafe_cb_preargs(struct io_plan, void *,         \
237                                         (fn), (arg),                    \
238                                         struct io_conn *),              \
239                     (arg))
240 bool io_timeout_(struct io_conn *conn, struct timespec ts,
241                  struct io_plan (*fn)(struct io_conn *, void *), void *arg);
242
243 /**
244  * io_duplex - split an fd into two connections.
245  * @conn: a connection.
246  * @start: the first function to call.
247  * @finish: the function to call when it's closed or fails.
248  * @arg: the argument to both @start and @finish.
249  *
250  * Sometimes you want to be able to simultaneously read and write on a
251  * single fd, but io forces a linear call sequence.  The solition is
252  * to have two connections for the same fd, and use one for read
253  * operations and one for write.
254  *
255  * You must io_close() both of them to close the fd.
256  */
257 #define io_duplex(conn, start, finish, arg)                             \
258         io_duplex_((conn),                                              \
259                    typesafe_cb_preargs(struct io_plan, void *,          \
260                                        (start), (arg), struct io_conn *), \
261                    typesafe_cb_preargs(void, void *, (finish), (arg),   \
262                                        struct io_conn *),               \
263                    (arg))
264
265 struct io_conn *io_duplex_(struct io_conn *conn,
266                            struct io_plan (*start)(struct io_conn *, void *),
267                            void (*finish)(struct io_conn *, void *),
268                            void *arg);
269
270 /**
271  * io_wake - wake up and idle connection.
272  * @conn: an idle connection.
273  * @fn: the next function to call once queued IO is complete.
274  * @arg: the argument to @next.
275  *
276  * This makes @conn run its @next function the next time around the
277  * io_loop().
278  */
279 #define io_wake(conn, fn, arg)                                          \
280         io_wake_((conn),                                                \
281                  typesafe_cb_preargs(struct io_plan, void *,            \
282                                      (fn), (arg), struct io_conn *),    \
283                  (arg))
284 void io_wake_(struct io_conn *conn,
285               struct io_plan (*fn)(struct io_conn *, void *), void *arg);
286
287 /**
288  * io_break - return from io_loop()
289  * @ret: non-NULL value to return from io_loop().
290  * @cb: function to call once on return
291  * @arg: @cb argument
292  *
293  * This breaks out of the io_loop.  As soon as the current @next
294  * function returns, any io_closed()'d connections will have their
295  * finish callbacks called, then io_loop() with return with @ret.
296  *
297  * If io_loop() is called again, then @cb will be called.
298  */
299 #define io_break(ret, fn, arg)                                          \
300         io_break_((ret),                                                \
301                   typesafe_cb_preargs(struct io_plan, void *,           \
302                                       (fn), (arg), struct io_conn *),   \
303                   (arg))
304 struct io_plan io_break_(void *ret,
305                          struct io_plan (*fn)(struct io_conn *, void *),
306                          void *arg);
307
308 /* FIXME: io_recvfrom/io_sendto */
309
310 /**
311  * io_close - terminate a connection.
312  * @conn: any connection.
313  *
314  * The schedules a connection to be closed.  It can be done on any
315  * connection, whether it has I/O queued or not (though that I/O may
316  * be performed first).
317  *
318  * It's common to 'return io_close(...)' from a @next function, but
319  * io_close can also be used as an argument to io_next().
320  */
321 struct io_plan io_close(struct io_conn *, void *unused);
322
323 /**
324  * io_loop - process fds until all closed on io_break.
325  *
326  * This is the core loop; it exits with the io_break() arg, or NULL if
327  * all connections and listeners are closed.
328  */
329 void *io_loop(void);
330 #endif /* CCAN_IO_H */