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