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