]> git.ozlabs.org Git - ccan/blob - ccan/io/SCENARIOS
ccan/io: test custom io functions.
[ccan] / ccan / io / SCENARIOS
1 Simple:
2         step1(conn): read(conn), then step2
3         step2(conn): write(conn), then close
4
5 Pass-through:
6         step1(conn): read(conn), then step2
7         step2(conn): write(otherconn), then step1
8
9 Pass-through-and-connect:
10         step1(conn): read(conn), then step2
11         step2(conn): connect(otherconn), then step3
12         step3(conn): write(otherconn), then step1
13
14 Chatroom:
15         step1(conn): read(conn), then step2
16         step2(conn): for c in allcons: write(c).  goto step1
17
18 Simple:
19
20 void event(struct io_event *done)
21 {
22         char *buf = done->priv;
23         struct io_event *e;
24
25         e = queue_read(done, done->conn, buf, 100);
26         e = queue_write(e, done->conn, buf, 100);
27         queue_close(e, done->conn);
28 }
29
30 Pass-through:
31 struct passthru {
32         char buf[100];
33         struct conn *rconn, *wconn;
34 };
35
36 void event(struct io_event *done)
37 {
38         struct passthru *p = done->priv;
39         struct io_event *e;
40
41         e = queue_read(done, p->rconn, p->buf, 100);
42         e = queue_write(e, p->wconn, buf, 100);
43         queue_event(e, event);
44 }
45
46 Chatroom:
47 struct list_head clients;
48
49 struct buffer {
50         char buf[100];
51         unsigned int ref;
52 };
53
54 struct client {
55         struct list_node list;
56         struct connection *conn;
57         struct buffer *rbuf, *wbuf;
58 };
59
60 void broadcast(struct io_event *done)
61 {
62         struct client *i, *c = done->conn->priv;
63         struct io_event *e;
64
65         list_for_each(&clients, i, list) {
66                 e = queue_write(done, i->conn, c->buf->buf, 100);
67                 e->priv = c->buf;
68                 c->buf->ref++;
69                 queue_event(e, drop_ref);
70         }
71
72
73
74 void event(struct io_event *done)
75 {
76         struct client *c = done->conn->priv;
77         struct io_event *e;
78
79         assert(c->conn == done->conn);
80         c->buf = malloc(sizeof(*c->buf));
81         c->buf->ref = 0;
82         e = queue_read(done, c->conn, c->buf->buf, 100);
83         e = queue_event(e, broadcast);
84 }
85
86
87         step1(conn): read(conn), then step2
88         step2(conn): for c in allcons: write(c).  goto step1