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