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