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