]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
ccan/io: make enum io_state namespace-safe.
[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_IO,
32         IO_NEXT, /* eg starting, woken from idle, return from io_break. */
33         IO_IDLE,
34         IO_FINISHED
35 };
36
37 static inline enum io_state from_ioplan(struct io_plan *op)
38 {
39         return (enum io_state)(long)op;
40 }
41
42 struct io_state_read {
43         char *buf;
44         size_t len;
45 };
46
47 struct io_state_write {
48         const char *buf;
49         size_t len;
50 };
51
52 struct io_state_readpart {
53         char *buf;
54         size_t *lenp;
55 };
56
57 struct io_state_writepart {
58         const char *buf;
59         size_t *lenp;
60 };
61
62 struct io_timeout {
63         struct timer timer;
64         struct io_conn *conn;
65
66         struct io_plan *(*next)(struct io_conn *, void *arg);
67         void *next_arg;
68 };
69
70 /* One connection per client. */
71 struct io_conn {
72         struct fd fd;
73
74         struct io_plan *(*next)(struct io_conn *, void *arg);
75         void *next_arg;
76
77         void (*finish)(struct io_conn *, void *arg);
78         void *finish_arg;
79
80         struct io_conn *duplex;
81         struct io_timeout *timeout;
82
83         enum io_result (*io)(struct io_conn *conn);
84
85         int pollflag; /* 0, POLLIN or POLLOUT */
86         enum io_state state;
87         union {
88                 struct io_state_read read;
89                 struct io_state_write write;
90                 struct io_state_readpart readpart;
91                 struct io_state_writepart writepart;
92         } u;
93 };
94
95 static inline bool timeout_active(const struct io_conn *conn)
96 {
97         return conn->timeout && conn->timeout->conn;
98 }
99
100 extern void *io_loop_return;
101
102 bool add_listener(struct io_listener *l);
103 bool add_conn(struct io_conn *c);
104 bool add_duplex(struct io_conn *c);
105 void del_listener(struct io_listener *l);
106 void backend_set_state(struct io_conn *conn, struct io_plan *op);
107 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
108 void backend_del_timeout(struct io_conn *conn);
109
110 struct io_plan *do_ready(struct io_conn *conn);
111 #endif /* CCAN_IO_BACKEND_H */