]> git.ozlabs.org Git - ccan/blob - ccan/io/backend.h
06427eb6fa37ea53aabfb47725211e8a448756b8
[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         NEXT, /* eg starting, woken from idle, return from io_break. */
26         READ,
27         WRITE,
28         READPART,
29         WRITEPART,
30         IDLE,
31         FINISHED,
32         PROCESSING /* We expect them to change this now. */
33 };
34
35 static inline enum io_state from_ioop(struct io_op *op)
36 {
37         return (enum io_state)(long)op;
38 }
39
40 struct io_state_read {
41         char *buf;
42         size_t len;
43 };
44
45 struct io_state_write {
46         const char *buf;
47         size_t len;
48 };
49
50 struct io_state_readpart {
51         char *buf;
52         size_t *lenp;
53 };
54
55 struct io_state_writepart {
56         const char *buf;
57         size_t *lenp;
58 };
59
60 /* One connection per client. */
61 struct io_conn {
62         struct fd fd;
63
64         enum io_state state;
65         union {
66                 struct io_state_read read;
67                 struct io_state_write write;
68                 struct io_state_readpart readpart;
69                 struct io_state_writepart writepart;
70         } u;
71 };
72
73 extern void *io_loop_return;
74
75 bool add_listener(struct io_listener *l);
76 bool add_conn(struct io_conn *c);
77 void del_listener(struct io_listener *l);
78 void backend_set_state(struct io_conn *conn, struct io_op *op);
79
80 struct io_op *do_writeable(struct io_conn *conn);
81 struct io_op *do_readable(struct io_conn *conn);
82 #endif /* CCAN_IO_BACKEND_H */