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