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