]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
b6674025b0233cf7e2f3a801dd9b41b026a3e97a
[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 <ccan/timer/timer.h>
6 #include <poll.h>
7
8 /* A setting for actions to always run (eg. zero-length reads). */
9 #define POLLALWAYS (((POLLIN|POLLOUT) + 1) & ~((POLLIN|POLLOUT)))
10
11 struct io_alloc {
12         void *(*alloc)(size_t size);
13         void *(*realloc)(void *ptr, size_t size);
14         void (*free)(void *ptr);
15 };
16 extern struct io_alloc io_alloc;
17
18 struct fd {
19         int fd;
20         bool listener;
21         size_t backend_info;
22 };
23
24 /* Listeners create connections. */
25 struct io_listener {
26         struct fd fd;
27
28         /* These are for connections we create. */
29         void (*init)(int fd, void *arg);
30         void *arg;
31 };
32
33 struct io_timeout {
34         struct timer timer;
35         struct io_conn *conn;
36
37         struct io_plan (*next)(struct io_conn *, void *arg);
38         void *next_arg;
39 };
40
41 /* One connection per client. */
42 struct io_conn {
43         struct fd fd;
44
45         void (*finish)(struct io_conn *, void *arg);
46         void *finish_arg;
47
48         struct io_conn *duplex;
49         struct io_timeout *timeout;
50
51         struct io_plan plan;
52 };
53
54 static inline bool timeout_active(const struct io_conn *conn)
55 {
56         return conn->timeout && conn->timeout->conn;
57 }
58
59 extern void *io_loop_return;
60
61 #ifdef DEBUG
62 extern struct io_conn *current;
63 static inline void set_current(struct io_conn *conn)
64 {
65         current = conn;
66 }
67 static inline bool doing_debug_on(struct io_conn *conn)
68 {
69         return io_debug_conn && io_debug_conn(conn);
70 }
71 static inline bool doing_debug(void)
72 {
73         return io_debug_conn;
74 }
75 #else
76 static inline void set_current(struct io_conn *conn)
77 {
78 }
79 static inline bool doing_debug_on(struct io_conn *conn)
80 {
81         return false;
82 }
83 static inline bool doing_debug(void)
84 {
85         return false;
86 }
87 #endif
88
89 bool add_listener(struct io_listener *l);
90 bool add_conn(struct io_conn *c);
91 bool add_duplex(struct io_conn *c);
92 void del_listener(struct io_listener *l);
93 void backend_plan_changed(struct io_conn *conn);
94 void backend_wait_changed(const void *wait);
95 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
96 void backend_del_timeout(struct io_conn *conn);
97 void backend_del_conn(struct io_conn *conn);
98
99 void io_ready(struct io_conn *conn);
100 void *do_io_loop(struct io_conn **ready);
101 #endif /* CCAN_IO_BACKEND_H */