-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchaining.c
180 lines (142 loc) · 4.16 KB
/
chaining.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "chaining.h"
#include <stdlib.h>
#include <sys/queue.h>
#include <assert.h>
#include <event2/event.h>
/*
* Implementation considerations (aka features not advertised to chain users):
* - do not cede control between an error being raised and before it will be
* handled by current+chain-wide except callback. If we did, we risk being
* called again with the chain being in a half torn-down state.
*/
struct chain_elem {
chain_process process;
chain_except event;
void *ctx;
STAILQ_ENTRY(chain_elem) next;
};
struct chain {
STAILQ_HEAD(chain_q, chain_elem) elems;
struct chain_elem *current;
chain_except event;
void *ctx;
};
static void chain_run(struct bufferevent *, void *);
static void chain_event(struct bufferevent *, short, void *);
static int chain_event_int(struct bufferevent *, int, void *);
int
chain_add(struct chain *chain, chain_process process, chain_except event, void *ctx)
{
struct chain_elem *elem;
assert(chain);
elem = calloc(1, sizeof(struct chain_elem));
if (!elem)
return CHAIN_ERROR;
elem->process = process;
elem->event = event;
elem->ctx = ctx;
STAILQ_INSERT_TAIL(&chain->elems, elem, next);
return CHAIN_DONE;
}
struct chain *
chain_new(chain_except event, void *ctx)
{
struct chain *chain;
assert(event);
chain = calloc(1, sizeof(struct chain));
if (!chain)
return NULL;
STAILQ_INIT(&chain->elems);
chain->event = event;
chain->ctx = ctx;
return chain;
}
int
chain_activate(struct chain *chain, struct bufferevent *bev, short iotype)
{
bufferevent_data_cb read_cb;
bufferevent_data_cb write_cb;
assert(chain && bev);
read_cb = (iotype & EV_READ) ? chain_run : NULL;
write_cb = (iotype & EV_WRITE) ? chain_run : NULL;
chain->current = STAILQ_FIRST(&chain->elems);
bufferevent_setcb(bev, read_cb, write_cb, chain_event, chain);
/* we'd be stuck indefinitely until there was a new event on bev, so run
* now */
bufferevent_trigger(bev, iotype, BEV_TRIG_DEFER_CALLBACKS);
return CHAIN_DONE;
}
static void
chain_run(struct bufferevent *bev, void *ctx)
{
struct chain *chain = ctx;
struct chain_elem *cur;
int rc = CHAIN_DONE;
assert(chain);
while (rc == CHAIN_DONE) {
for (cur = chain->current;
rc == CHAIN_DONE && cur;
cur = STAILQ_NEXT(cur, next)) {
chain->current = cur;
if (cur->process)
rc = cur->process(chain, bev, cur->ctx);
};
if (rc == CHAIN_AGAIN) {
/* We need more data */
return;
}
if (rc == CHAIN_DONE) {
chain_destroy(chain, bev, CHAIN_DONE);
return;
}
/* Error */
rc = chain_event_int(bev, rc, ctx);
/* If we recovered with CHAIN_DONE now, continue */
}
}
static void
chain_event(struct bufferevent *bev, short events, void *ctx)
{
(void)chain_event_int(bev, events, ctx);
}
static int
chain_event_int(struct bufferevent *bev, int events, void *ctx)
{
struct chain *chain = ctx;
int rc;
assert(chain && chain->current);
if (chain->current->event) {
rc = chain->current->event(chain, bev, events, chain->current->ctx);
if (rc == CHAIN_AGAIN || rc == CHAIN_DONE) {
if (rc == CHAIN_DONE)
chain->current = STAILQ_NEXT(chain->current, next);
/* Recovered */
return rc;
}
}
if (chain->event) {
rc = chain->event(chain, bev, events, chain->ctx);
if (rc == CHAIN_AGAIN) {
/* Recovered */
return rc;
}
}
chain_destroy(chain, bev, CHAIN_ABORT);
return CHAIN_ABORT;
}
void
chain_destroy(struct chain *chain, struct bufferevent *bev, int events)
{
struct chain_elem *elem;
assert(chain);
while (!STAILQ_EMPTY(&chain->elems)) {
elem = STAILQ_FIRST(&chain->elems);
if (elem->event)
elem->event(chain, bev, events, elem->ctx);
STAILQ_REMOVE_HEAD(&chain->elems, next);
free(elem);
}
if (chain->event)
chain->event(chain, bev, events, chain->ctx);
free(chain);
}