]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
ccan/io: use explicit IO callback functions, instead of io_state values.
[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
14 /* Listeners create connections. */
15 struct io_listener {
16         struct fd fd;
17
18         /* These are for connections we create. */
19         struct io_plan *(*next)(struct io_conn *, void *arg);
20         void (*finish)(struct io_conn *, void *arg);
21         void *conn_arg;
22 };
23
24 enum io_result {
25         RESULT_AGAIN,
26         RESULT_FINISHED,
27         RESULT_CLOSE
28 };
29
30 enum io_state {
31         IO,
32         NEXT, /* eg starting, woken from idle, return from io_break. */
33         IDLE,
34         FINISHED,
35         PROCESSING /* We expect them to change this now. */
36 };
37
38 static inline enum io_state from_ioplan(struct io_plan *op)
39 {
40         return (enum io_state)(long)op;
41 }
42
43 struct io_state_read {
44         char *buf;
45         size_t len;
46 };
47
48 struct io_state_write {
49         const char *buf;
50         size_t len;
51 };
52
53 struct io_state_readpart {
54         char *buf;
55         size_t *lenp;
56 };
57
58 struct io_state_writepart {
59         const char *buf;
60         size_t *lenp;
61 };
62
63 struct io_timeout {
64         struct timer timer;
65         struct io_conn *conn;
66
67         struct io_plan *(*next)(struct io_conn *, void *arg);
68         void *next_arg;
69 };
70
71 /* One connection per client. */
72 struct io_conn {
73         struct fd fd;
74
75         struct io_plan *(*next)(struct io_conn *, void *arg);
76         void *next_arg;
77
78         void (*finish)(struct io_conn *, void *arg);
79         void *finish_arg;
80
81         struct io_conn *duplex;
82         struct io_timeout *timeout;
83
84         enum io_result (*io)(struct io_conn *conn);
85
86         int pollflag; /* 0, POLLIN or POLLOUT */
87         enum io_state state;
88         union {
89                 struct io_state_read read;
90                 struct io_state_write write;
91                 struct io_state_readpart readpart;
92                 struct io_state_writepart writepart;
93         } u;
94 };
95
96 static inline bool timeout_active(const struct io_conn *conn)
97 {
98         return conn->timeout && conn->timeout->conn;
99 }
100
101 extern void *io_loop_return;
102
103 bool add_listener(struct io_listener *l);
104 bool add_conn(struct io_conn *c);
105 bool add_duplex(struct io_conn *c);
106 void del_listener(struct io_listener *l);
107 void backend_set_state(struct io_conn *conn, struct io_plan *op);
108 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
109 void backend_del_timeout(struct io_conn *conn);
110
111 struct io_plan *do_ready(struct io_conn *conn);
112 #endif /* CCAN_IO_BACKEND_H */