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