]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
39605ec48662114944f184f4739848e1d2953c0d
[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
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         /* Closing (both plans will be the same). */
35         IO_CLOSING
36 };
37
38 /**
39  * struct io_plan - one half of I/O to do
40  * @status: the status of this plan.
41  * @io: function to call when fd becomes read/writable, returns 0 to be
42  *      called again, 1 if it's finished, and -1 on error (fd will be closed)
43  * @next: the next function which is called if io returns 1.
44  * @next_arg: the argument to @next
45  * @u1, @u2: scratch space for @io.
46  */
47 struct io_plan {
48         enum io_plan_status status;
49
50         int (*io)(int fd, struct io_plan_arg *arg);
51
52         struct io_plan *(*next)(struct io_conn *, void *next_arg);
53         void *next_arg;
54
55         struct io_plan_arg arg;
56 };
57
58 /* One connection per client. */
59 struct io_conn {
60         struct fd fd;
61         bool debug;
62         /* For duplex to save. */
63         bool debug_saved;
64
65         /* always or closing list. */
66         struct io_conn *list;
67
68         void (*finish)(struct io_conn *, void *arg);
69         void *finish_arg;
70
71         struct io_plan plan[2];
72 };
73
74 extern void *io_loop_return;
75
76 bool add_listener(struct io_listener *l);
77 bool add_conn(struct io_conn *c);
78 bool add_duplex(struct io_conn *c);
79 void del_listener(struct io_listener *l);
80 void backend_new_closing(struct io_conn *conn);
81 void backend_new_always(struct io_conn *conn);
82 void backend_new_plan(struct io_conn *conn);
83 void remove_from_always(struct io_conn *conn);
84 void backend_plan_done(struct io_conn *conn);
85
86 void backend_wake(const void *wait);
87 void backend_del_conn(struct io_conn *conn);
88
89 void io_ready(struct io_conn *conn, int pollflags);
90 void io_do_always(struct io_conn *conn);
91 void io_do_wakeup(struct io_conn *conn, enum io_direction dir);
92 void *do_io_loop(struct io_conn **ready);
93 #endif /* CCAN_IO_BACKEND_H */