]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
io: allow overriding poll function.
[ccan] / ccan / io / backend.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_IO_BACKEND_H
3 #define CCAN_IO_BACKEND_H
4 #include <stdbool.h>
5 #include "io_plan.h"
6 #include <ccan/list/list.h>
7
8 struct fd {
9         int fd;
10         bool listener;
11         size_t backend_info;
12 };
13
14 /* Listeners create connections. */
15 struct io_listener {
16         struct fd fd;
17
18         const tal_t *ctx;
19
20         /* These are for connections we create. */
21         struct io_plan *(*init)(struct io_conn *conn, void *arg);
22         void *arg;
23 };
24
25 enum io_plan_status {
26         /* As before calling next function. */
27         IO_UNSET,
28         /* Normal. */
29         IO_POLLING,
30         /* Waiting for io_wake */
31         IO_WAITING,
32         /* Always do this. */
33         IO_ALWAYS
34 };
35
36 /**
37  * struct io_plan - one half of I/O to do
38  * @status: the status of this plan.
39  * @io: function to call when fd becomes read/writable, returns 0 to be
40  *      called again, 1 if it's finished, and -1 on error (fd will be closed)
41  * @next: the next function which is called if io returns 1.
42  * @next_arg: the argument to @next
43  * @u1, @u2: scratch space for @io.
44  */
45 struct io_plan {
46         enum io_plan_status status;
47
48         int (*io)(int fd, struct io_plan_arg *arg);
49
50         struct io_plan *(*next)(struct io_conn *, void *next_arg);
51         void *next_arg;
52
53         struct io_plan_arg arg;
54 };
55
56 /* One connection per client. */
57 struct io_conn {
58         struct fd fd;
59
60         /* always list. */
61         struct list_node always;
62
63         void (*finish)(struct io_conn *, void *arg);
64         void *finish_arg;
65
66         struct io_plan plan[2];
67 };
68
69 extern void *io_loop_return;
70
71 bool add_listener(struct io_listener *l);
72 bool add_conn(struct io_conn *c);
73 bool add_duplex(struct io_conn *c);
74 void del_listener(struct io_listener *l);
75 void cleanup_conn_without_close(struct io_conn *c);
76 void backend_new_always(struct io_conn *conn);
77 void backend_new_plan(struct io_conn *conn);
78 void remove_from_always(struct io_conn *conn);
79 void backend_plan_done(struct io_conn *conn);
80
81 void backend_wake(const void *wait);
82
83 void io_ready(struct io_conn *conn, int pollflags);
84 void io_do_always(struct io_conn *conn);
85 void io_do_wakeup(struct io_conn *conn, enum io_direction dir);
86 void *do_io_loop(struct io_conn **ready);
87 #endif /* CCAN_IO_BACKEND_H */