]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
ccan/io: remove next & finish from union.
[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_state {
25         /* These wait for something to input */
26         READ,
27         READPART,
28
29         /* These wait for room to output */
30         WRITE,
31         WRITEPART,
32
33         NEXT, /* eg starting, woken from idle, return from io_break. */
34         IDLE,
35         FINISHED,
36         PROCESSING /* We expect them to change this now. */
37 };
38
39 static inline enum io_state from_ioplan(struct io_plan *op)
40 {
41         return (enum io_state)(long)op;
42 }
43
44 struct io_state_read {
45         char *buf;
46         size_t len;
47 };
48
49 struct io_state_write {
50         const char *buf;
51         size_t len;
52 };
53
54 struct io_state_readpart {
55         char *buf;
56         size_t *lenp;
57 };
58
59 struct io_state_writepart {
60         const char *buf;
61         size_t *lenp;
62 };
63
64 struct io_timeout {
65         struct timer timer;
66         struct io_conn *conn;
67
68         struct io_plan *(*next)(struct io_conn *, void *arg);
69         void *next_arg;
70 };
71
72 /* One connection per client. */
73 struct io_conn {
74         struct fd fd;
75
76         struct io_plan *(*next)(struct io_conn *, void *arg);
77         void *next_arg;
78
79         void (*finish)(struct io_conn *, void *arg);
80         void *finish_arg;
81
82         struct io_conn *duplex;
83         struct io_timeout *timeout;
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 */