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