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 	/* We could put these in io_plan, but they pack nicely here */
12 	bool exclusive[2];
13 	size_t backend_info;
14 };
15 
16 /* Listeners create connections. */
17 struct io_listener {
18 	struct fd fd;
19 
20 	const tal_t *ctx;
21 
22 	/* These are for connections we create. */
23 	struct io_plan *(*init)(struct io_conn *conn, void *arg);
24 	void *arg;
25 };
26 
27 enum io_plan_status {
28 	/* As before calling next function. */
29 	IO_UNSET,
30 	/* Normal, but haven't started yet. */
31 	IO_POLLING_NOTSTARTED,
32 	IO_POLLING_STARTED,
33 	/* Waiting for io_wake */
34 	IO_WAITING,
35 	/* Always do this. */
36 	IO_ALWAYS
37 };
38 
39 /**
40  * struct io_plan - one half of I/O to do
41  * @status: the status of this plan.
42  * @dir: are we plan[0] or plan[1] inside io_conn?
43  * @io: function to call when fd becomes read/writable, returns 0 to be
44  *      called again, 1 if it's finished, and -1 on error (fd will be closed)
45  * @next: the next function which is called if io returns 1.
46  * @next_arg: the argument to @next
47  * @u1, @u2: scratch space for @io.
48  */
49 struct io_plan {
50 	enum io_plan_status status;
51 	enum io_direction dir;
52 
53 	int (*io)(int fd, struct io_plan_arg *arg);
54 
55 	struct io_plan *(*next)(struct io_conn *, void *next_arg);
56 	void *next_arg;
57 
58 	struct io_plan_arg arg;
59 };
60 
61 /* One connection per client. */
62 struct io_conn {
63 	struct fd fd;
64 
65 	void (*finish)(struct io_conn *, void *arg);
66 	void *finish_arg;
67 
68 	struct io_plan plan[2];
69 };
70 
71 extern void *io_loop_return;
72 
73 bool add_listener(struct io_listener *l);
74 bool add_conn(struct io_conn *c);
75 bool add_duplex(struct io_conn *c);
76 void del_listener(struct io_listener *l);
77 void cleanup_conn_without_close(struct io_conn *c);
78 bool backend_new_always(struct io_plan *plan);
79 void backend_new_plan(struct io_conn *conn);
80 void backend_plan_done(struct io_conn *conn);
81 bool backend_set_exclusive(struct io_plan *plan, bool exclusive);
82 
83 void backend_wake(const void *wait);
84 
85 void io_ready(struct io_conn *conn, int pollflags);
86 void io_do_always(struct io_plan *conn);
87 void io_do_wakeup(struct io_conn *conn, enum io_direction dir);
88 void *do_io_loop(struct io_conn **ready);
89 #endif /* CCAN_IO_BACKEND_H */
90