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