]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
ccan/io: io_duplex.
[ccan] / ccan / io / backend.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_IO_BACKEND_H
3 #define CCAN_IO_BACKEND_H
4 #include <stdbool.h>
5
6 struct fd {
7         int fd;
8         bool listener;
9         size_t backend_info;
10
11         struct io_op *(*next)(struct io_conn *, void *arg);
12         void *next_arg;
13
14         void (*finish)(struct io_conn *, void *arg);
15         void *finish_arg;
16 };
17
18
19 /* Listeners create connections. */
20 struct io_listener {
21         struct fd fd;
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_ioop(struct io_op *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 /* One connection per client. */
65 struct io_conn {
66         struct fd fd;
67
68         struct io_conn *duplex;
69
70         enum io_state state;
71         union {
72                 struct io_state_read read;
73                 struct io_state_write write;
74                 struct io_state_readpart readpart;
75                 struct io_state_writepart writepart;
76         } u;
77 };
78
79 extern void *io_loop_return;
80
81 bool add_listener(struct io_listener *l);
82 bool add_conn(struct io_conn *c);
83 bool add_duplex(struct io_conn *c);
84 void del_listener(struct io_listener *l);
85 void backend_set_state(struct io_conn *conn, struct io_op *op);
86
87 struct io_op *do_ready(struct io_conn *conn);
88 #endif /* CCAN_IO_BACKEND_H */