bufferevent_async.c revision 1.1.1.1.8.2 1 1.1.1.1.8.2 tls /* $NetBSD: bufferevent_async.c,v 1.1.1.1.8.2 2014/08/19 23:51:45 tls Exp $ */
2 1.1.1.1.8.2 tls
3 1.1.1.1.8.2 tls /*
4 1.1.1.1.8.2 tls * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5 1.1.1.1.8.2 tls *
6 1.1.1.1.8.2 tls * All rights reserved.
7 1.1.1.1.8.2 tls *
8 1.1.1.1.8.2 tls * Redistribution and use in source and binary forms, with or without
9 1.1.1.1.8.2 tls * modification, are permitted provided that the following conditions
10 1.1.1.1.8.2 tls * are met:
11 1.1.1.1.8.2 tls * 1. Redistributions of source code must retain the above copyright
12 1.1.1.1.8.2 tls * notice, this list of conditions and the following disclaimer.
13 1.1.1.1.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.1.1.8.2 tls * notice, this list of conditions and the following disclaimer in the
15 1.1.1.1.8.2 tls * documentation and/or other materials provided with the distribution.
16 1.1.1.1.8.2 tls * 3. The name of the author may not be used to endorse or promote products
17 1.1.1.1.8.2 tls * derived from this software without specific prior written permission.
18 1.1.1.1.8.2 tls *
19 1.1.1.1.8.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1.1.1.8.2 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1.1.1.8.2 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1.1.1.8.2 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1.1.1.8.2 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1.1.1.8.2 tls * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1.1.1.8.2 tls * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1.1.1.8.2 tls * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1.1.1.8.2 tls * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1.1.1.8.2 tls * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1.1.1.8.2 tls */
30 1.1.1.1.8.2 tls
31 1.1.1.1.8.2 tls #include "event2/event-config.h"
32 1.1.1.1.8.2 tls #include "evconfig-private.h"
33 1.1.1.1.8.2 tls
34 1.1.1.1.8.2 tls #ifdef EVENT__HAVE_SYS_TIME_H
35 1.1.1.1.8.2 tls #include <sys/time.h>
36 1.1.1.1.8.2 tls #endif
37 1.1.1.1.8.2 tls
38 1.1.1.1.8.2 tls #include <errno.h>
39 1.1.1.1.8.2 tls #include <stdio.h>
40 1.1.1.1.8.2 tls #include <stdlib.h>
41 1.1.1.1.8.2 tls #include <string.h>
42 1.1.1.1.8.2 tls #ifdef EVENT__HAVE_STDARG_H
43 1.1.1.1.8.2 tls #include <stdarg.h>
44 1.1.1.1.8.2 tls #endif
45 1.1.1.1.8.2 tls #ifdef EVENT__HAVE_UNISTD_H
46 1.1.1.1.8.2 tls #include <unistd.h>
47 1.1.1.1.8.2 tls #endif
48 1.1.1.1.8.2 tls
49 1.1.1.1.8.2 tls #ifdef _WIN32
50 1.1.1.1.8.2 tls #include <winsock2.h>
51 1.1.1.1.8.2 tls #include <ws2tcpip.h>
52 1.1.1.1.8.2 tls #endif
53 1.1.1.1.8.2 tls
54 1.1.1.1.8.2 tls #include <sys/queue.h>
55 1.1.1.1.8.2 tls
56 1.1.1.1.8.2 tls #include "event2/util.h"
57 1.1.1.1.8.2 tls #include "event2/bufferevent.h"
58 1.1.1.1.8.2 tls #include "event2/buffer.h"
59 1.1.1.1.8.2 tls #include "event2/bufferevent_struct.h"
60 1.1.1.1.8.2 tls #include "event2/event.h"
61 1.1.1.1.8.2 tls #include "event2/util.h"
62 1.1.1.1.8.2 tls #include "event-internal.h"
63 1.1.1.1.8.2 tls #include "log-internal.h"
64 1.1.1.1.8.2 tls #include "mm-internal.h"
65 1.1.1.1.8.2 tls #include "bufferevent-internal.h"
66 1.1.1.1.8.2 tls #include "util-internal.h"
67 1.1.1.1.8.2 tls #include "iocp-internal.h"
68 1.1.1.1.8.2 tls
69 1.1.1.1.8.2 tls #ifndef SO_UPDATE_CONNECT_CONTEXT
70 1.1.1.1.8.2 tls /* Mingw is sometimes missing this */
71 1.1.1.1.8.2 tls #define SO_UPDATE_CONNECT_CONTEXT 0x7010
72 1.1.1.1.8.2 tls #endif
73 1.1.1.1.8.2 tls
74 1.1.1.1.8.2 tls /* prototypes */
75 1.1.1.1.8.2 tls static int be_async_enable(struct bufferevent *, short);
76 1.1.1.1.8.2 tls static int be_async_disable(struct bufferevent *, short);
77 1.1.1.1.8.2 tls static void be_async_destruct(struct bufferevent *);
78 1.1.1.1.8.2 tls static int be_async_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
79 1.1.1.1.8.2 tls static int be_async_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
80 1.1.1.1.8.2 tls
81 1.1.1.1.8.2 tls struct bufferevent_async {
82 1.1.1.1.8.2 tls struct bufferevent_private bev;
83 1.1.1.1.8.2 tls struct event_overlapped connect_overlapped;
84 1.1.1.1.8.2 tls struct event_overlapped read_overlapped;
85 1.1.1.1.8.2 tls struct event_overlapped write_overlapped;
86 1.1.1.1.8.2 tls size_t read_in_progress;
87 1.1.1.1.8.2 tls size_t write_in_progress;
88 1.1.1.1.8.2 tls unsigned ok : 1;
89 1.1.1.1.8.2 tls unsigned read_added : 1;
90 1.1.1.1.8.2 tls unsigned write_added : 1;
91 1.1.1.1.8.2 tls };
92 1.1.1.1.8.2 tls
93 1.1.1.1.8.2 tls const struct bufferevent_ops bufferevent_ops_async = {
94 1.1.1.1.8.2 tls "socket_async",
95 1.1.1.1.8.2 tls evutil_offsetof(struct bufferevent_async, bev.bev),
96 1.1.1.1.8.2 tls be_async_enable,
97 1.1.1.1.8.2 tls be_async_disable,
98 1.1.1.1.8.2 tls be_async_destruct,
99 1.1.1.1.8.2 tls bufferevent_generic_adj_timeouts_,
100 1.1.1.1.8.2 tls be_async_flush,
101 1.1.1.1.8.2 tls be_async_ctrl,
102 1.1.1.1.8.2 tls };
103 1.1.1.1.8.2 tls
104 1.1.1.1.8.2 tls static inline struct bufferevent_async *
105 1.1.1.1.8.2 tls upcast(struct bufferevent *bev)
106 1.1.1.1.8.2 tls {
107 1.1.1.1.8.2 tls struct bufferevent_async *bev_a;
108 1.1.1.1.8.2 tls if (bev->be_ops != &bufferevent_ops_async)
109 1.1.1.1.8.2 tls return NULL;
110 1.1.1.1.8.2 tls bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev);
111 1.1.1.1.8.2 tls return bev_a;
112 1.1.1.1.8.2 tls }
113 1.1.1.1.8.2 tls
114 1.1.1.1.8.2 tls static inline struct bufferevent_async *
115 1.1.1.1.8.2 tls upcast_connect(struct event_overlapped *eo)
116 1.1.1.1.8.2 tls {
117 1.1.1.1.8.2 tls struct bufferevent_async *bev_a;
118 1.1.1.1.8.2 tls bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, connect_overlapped);
119 1.1.1.1.8.2 tls EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
120 1.1.1.1.8.2 tls return bev_a;
121 1.1.1.1.8.2 tls }
122 1.1.1.1.8.2 tls
123 1.1.1.1.8.2 tls static inline struct bufferevent_async *
124 1.1.1.1.8.2 tls upcast_read(struct event_overlapped *eo)
125 1.1.1.1.8.2 tls {
126 1.1.1.1.8.2 tls struct bufferevent_async *bev_a;
127 1.1.1.1.8.2 tls bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, read_overlapped);
128 1.1.1.1.8.2 tls EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
129 1.1.1.1.8.2 tls return bev_a;
130 1.1.1.1.8.2 tls }
131 1.1.1.1.8.2 tls
132 1.1.1.1.8.2 tls static inline struct bufferevent_async *
133 1.1.1.1.8.2 tls upcast_write(struct event_overlapped *eo)
134 1.1.1.1.8.2 tls {
135 1.1.1.1.8.2 tls struct bufferevent_async *bev_a;
136 1.1.1.1.8.2 tls bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, write_overlapped);
137 1.1.1.1.8.2 tls EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
138 1.1.1.1.8.2 tls return bev_a;
139 1.1.1.1.8.2 tls }
140 1.1.1.1.8.2 tls
141 1.1.1.1.8.2 tls static void
142 1.1.1.1.8.2 tls bev_async_del_write(struct bufferevent_async *beva)
143 1.1.1.1.8.2 tls {
144 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
145 1.1.1.1.8.2 tls
146 1.1.1.1.8.2 tls if (beva->write_added) {
147 1.1.1.1.8.2 tls beva->write_added = 0;
148 1.1.1.1.8.2 tls event_base_del_virtual_(bev->ev_base);
149 1.1.1.1.8.2 tls }
150 1.1.1.1.8.2 tls }
151 1.1.1.1.8.2 tls
152 1.1.1.1.8.2 tls static void
153 1.1.1.1.8.2 tls bev_async_del_read(struct bufferevent_async *beva)
154 1.1.1.1.8.2 tls {
155 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
156 1.1.1.1.8.2 tls
157 1.1.1.1.8.2 tls if (beva->read_added) {
158 1.1.1.1.8.2 tls beva->read_added = 0;
159 1.1.1.1.8.2 tls event_base_del_virtual_(bev->ev_base);
160 1.1.1.1.8.2 tls }
161 1.1.1.1.8.2 tls }
162 1.1.1.1.8.2 tls
163 1.1.1.1.8.2 tls static void
164 1.1.1.1.8.2 tls bev_async_add_write(struct bufferevent_async *beva)
165 1.1.1.1.8.2 tls {
166 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
167 1.1.1.1.8.2 tls
168 1.1.1.1.8.2 tls if (!beva->write_added) {
169 1.1.1.1.8.2 tls beva->write_added = 1;
170 1.1.1.1.8.2 tls event_base_add_virtual_(bev->ev_base);
171 1.1.1.1.8.2 tls }
172 1.1.1.1.8.2 tls }
173 1.1.1.1.8.2 tls
174 1.1.1.1.8.2 tls static void
175 1.1.1.1.8.2 tls bev_async_add_read(struct bufferevent_async *beva)
176 1.1.1.1.8.2 tls {
177 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
178 1.1.1.1.8.2 tls
179 1.1.1.1.8.2 tls if (!beva->read_added) {
180 1.1.1.1.8.2 tls beva->read_added = 1;
181 1.1.1.1.8.2 tls event_base_add_virtual_(bev->ev_base);
182 1.1.1.1.8.2 tls }
183 1.1.1.1.8.2 tls }
184 1.1.1.1.8.2 tls
185 1.1.1.1.8.2 tls static void
186 1.1.1.1.8.2 tls bev_async_consider_writing(struct bufferevent_async *beva)
187 1.1.1.1.8.2 tls {
188 1.1.1.1.8.2 tls size_t at_most;
189 1.1.1.1.8.2 tls int limit;
190 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
191 1.1.1.1.8.2 tls
192 1.1.1.1.8.2 tls /* Don't write if there's a write in progress, or we do not
193 1.1.1.1.8.2 tls * want to write, or when there's nothing left to write. */
194 1.1.1.1.8.2 tls if (beva->write_in_progress || beva->bev.connecting)
195 1.1.1.1.8.2 tls return;
196 1.1.1.1.8.2 tls if (!beva->ok || !(bev->enabled&EV_WRITE) ||
197 1.1.1.1.8.2 tls !evbuffer_get_length(bev->output)) {
198 1.1.1.1.8.2 tls bev_async_del_write(beva);
199 1.1.1.1.8.2 tls return;
200 1.1.1.1.8.2 tls }
201 1.1.1.1.8.2 tls
202 1.1.1.1.8.2 tls at_most = evbuffer_get_length(bev->output);
203 1.1.1.1.8.2 tls
204 1.1.1.1.8.2 tls /* This is safe so long as bufferevent_get_write_max never returns
205 1.1.1.1.8.2 tls * more than INT_MAX. That's true for now. XXXX */
206 1.1.1.1.8.2 tls limit = (int)bufferevent_get_write_max_(&beva->bev);
207 1.1.1.1.8.2 tls if (at_most >= (size_t)limit && limit >= 0)
208 1.1.1.1.8.2 tls at_most = limit;
209 1.1.1.1.8.2 tls
210 1.1.1.1.8.2 tls if (beva->bev.write_suspended) {
211 1.1.1.1.8.2 tls bev_async_del_write(beva);
212 1.1.1.1.8.2 tls return;
213 1.1.1.1.8.2 tls }
214 1.1.1.1.8.2 tls
215 1.1.1.1.8.2 tls /* XXXX doesn't respect low-water mark very well. */
216 1.1.1.1.8.2 tls bufferevent_incref_(bev);
217 1.1.1.1.8.2 tls if (evbuffer_launch_write_(bev->output, at_most,
218 1.1.1.1.8.2 tls &beva->write_overlapped)) {
219 1.1.1.1.8.2 tls bufferevent_decref_(bev);
220 1.1.1.1.8.2 tls beva->ok = 0;
221 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR);
222 1.1.1.1.8.2 tls } else {
223 1.1.1.1.8.2 tls beva->write_in_progress = at_most;
224 1.1.1.1.8.2 tls bufferevent_decrement_write_buckets_(&beva->bev, at_most);
225 1.1.1.1.8.2 tls bev_async_add_write(beva);
226 1.1.1.1.8.2 tls }
227 1.1.1.1.8.2 tls }
228 1.1.1.1.8.2 tls
229 1.1.1.1.8.2 tls static void
230 1.1.1.1.8.2 tls bev_async_consider_reading(struct bufferevent_async *beva)
231 1.1.1.1.8.2 tls {
232 1.1.1.1.8.2 tls size_t cur_size;
233 1.1.1.1.8.2 tls size_t read_high;
234 1.1.1.1.8.2 tls size_t at_most;
235 1.1.1.1.8.2 tls int limit;
236 1.1.1.1.8.2 tls struct bufferevent *bev = &beva->bev.bev;
237 1.1.1.1.8.2 tls
238 1.1.1.1.8.2 tls /* Don't read if there is a read in progress, or we do not
239 1.1.1.1.8.2 tls * want to read. */
240 1.1.1.1.8.2 tls if (beva->read_in_progress || beva->bev.connecting)
241 1.1.1.1.8.2 tls return;
242 1.1.1.1.8.2 tls if (!beva->ok || !(bev->enabled&EV_READ)) {
243 1.1.1.1.8.2 tls bev_async_del_read(beva);
244 1.1.1.1.8.2 tls return;
245 1.1.1.1.8.2 tls }
246 1.1.1.1.8.2 tls
247 1.1.1.1.8.2 tls /* Don't read if we're full */
248 1.1.1.1.8.2 tls cur_size = evbuffer_get_length(bev->input);
249 1.1.1.1.8.2 tls read_high = bev->wm_read.high;
250 1.1.1.1.8.2 tls if (read_high) {
251 1.1.1.1.8.2 tls if (cur_size >= read_high) {
252 1.1.1.1.8.2 tls bev_async_del_read(beva);
253 1.1.1.1.8.2 tls return;
254 1.1.1.1.8.2 tls }
255 1.1.1.1.8.2 tls at_most = read_high - cur_size;
256 1.1.1.1.8.2 tls } else {
257 1.1.1.1.8.2 tls at_most = 16384; /* FIXME totally magic. */
258 1.1.1.1.8.2 tls }
259 1.1.1.1.8.2 tls
260 1.1.1.1.8.2 tls /* XXXX This over-commits. */
261 1.1.1.1.8.2 tls /* XXXX see also not above on cast on bufferevent_get_write_max_() */
262 1.1.1.1.8.2 tls limit = (int)bufferevent_get_read_max_(&beva->bev);
263 1.1.1.1.8.2 tls if (at_most >= (size_t)limit && limit >= 0)
264 1.1.1.1.8.2 tls at_most = limit;
265 1.1.1.1.8.2 tls
266 1.1.1.1.8.2 tls if (beva->bev.read_suspended) {
267 1.1.1.1.8.2 tls bev_async_del_read(beva);
268 1.1.1.1.8.2 tls return;
269 1.1.1.1.8.2 tls }
270 1.1.1.1.8.2 tls
271 1.1.1.1.8.2 tls bufferevent_incref_(bev);
272 1.1.1.1.8.2 tls if (evbuffer_launch_read_(bev->input, at_most, &beva->read_overlapped)) {
273 1.1.1.1.8.2 tls beva->ok = 0;
274 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR);
275 1.1.1.1.8.2 tls bufferevent_decref_(bev);
276 1.1.1.1.8.2 tls } else {
277 1.1.1.1.8.2 tls beva->read_in_progress = at_most;
278 1.1.1.1.8.2 tls bufferevent_decrement_read_buckets_(&beva->bev, at_most);
279 1.1.1.1.8.2 tls bev_async_add_read(beva);
280 1.1.1.1.8.2 tls }
281 1.1.1.1.8.2 tls
282 1.1.1.1.8.2 tls return;
283 1.1.1.1.8.2 tls }
284 1.1.1.1.8.2 tls
285 1.1.1.1.8.2 tls static void
286 1.1.1.1.8.2 tls be_async_outbuf_callback(struct evbuffer *buf,
287 1.1.1.1.8.2 tls const struct evbuffer_cb_info *cbinfo,
288 1.1.1.1.8.2 tls void *arg)
289 1.1.1.1.8.2 tls {
290 1.1.1.1.8.2 tls struct bufferevent *bev = arg;
291 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
292 1.1.1.1.8.2 tls
293 1.1.1.1.8.2 tls /* If we added data to the outbuf and were not writing before,
294 1.1.1.1.8.2 tls * we may want to write now. */
295 1.1.1.1.8.2 tls
296 1.1.1.1.8.2 tls bufferevent_incref_and_lock_(bev);
297 1.1.1.1.8.2 tls
298 1.1.1.1.8.2 tls if (cbinfo->n_added)
299 1.1.1.1.8.2 tls bev_async_consider_writing(bev_async);
300 1.1.1.1.8.2 tls
301 1.1.1.1.8.2 tls bufferevent_decref_and_unlock_(bev);
302 1.1.1.1.8.2 tls }
303 1.1.1.1.8.2 tls
304 1.1.1.1.8.2 tls static void
305 1.1.1.1.8.2 tls be_async_inbuf_callback(struct evbuffer *buf,
306 1.1.1.1.8.2 tls const struct evbuffer_cb_info *cbinfo,
307 1.1.1.1.8.2 tls void *arg)
308 1.1.1.1.8.2 tls {
309 1.1.1.1.8.2 tls struct bufferevent *bev = arg;
310 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
311 1.1.1.1.8.2 tls
312 1.1.1.1.8.2 tls /* If we drained data from the inbuf and were not reading before,
313 1.1.1.1.8.2 tls * we may want to read now */
314 1.1.1.1.8.2 tls
315 1.1.1.1.8.2 tls bufferevent_incref_and_lock_(bev);
316 1.1.1.1.8.2 tls
317 1.1.1.1.8.2 tls if (cbinfo->n_deleted)
318 1.1.1.1.8.2 tls bev_async_consider_reading(bev_async);
319 1.1.1.1.8.2 tls
320 1.1.1.1.8.2 tls bufferevent_decref_and_unlock_(bev);
321 1.1.1.1.8.2 tls }
322 1.1.1.1.8.2 tls
323 1.1.1.1.8.2 tls static int
324 1.1.1.1.8.2 tls be_async_enable(struct bufferevent *buf, short what)
325 1.1.1.1.8.2 tls {
326 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(buf);
327 1.1.1.1.8.2 tls
328 1.1.1.1.8.2 tls if (!bev_async->ok)
329 1.1.1.1.8.2 tls return -1;
330 1.1.1.1.8.2 tls
331 1.1.1.1.8.2 tls if (bev_async->bev.connecting) {
332 1.1.1.1.8.2 tls /* Don't launch anything during connection attempts. */
333 1.1.1.1.8.2 tls return 0;
334 1.1.1.1.8.2 tls }
335 1.1.1.1.8.2 tls
336 1.1.1.1.8.2 tls if (what & EV_READ)
337 1.1.1.1.8.2 tls BEV_RESET_GENERIC_READ_TIMEOUT(buf);
338 1.1.1.1.8.2 tls if (what & EV_WRITE)
339 1.1.1.1.8.2 tls BEV_RESET_GENERIC_WRITE_TIMEOUT(buf);
340 1.1.1.1.8.2 tls
341 1.1.1.1.8.2 tls /* If we newly enable reading or writing, and we aren't reading or
342 1.1.1.1.8.2 tls writing already, consider launching a new read or write. */
343 1.1.1.1.8.2 tls
344 1.1.1.1.8.2 tls if (what & EV_READ)
345 1.1.1.1.8.2 tls bev_async_consider_reading(bev_async);
346 1.1.1.1.8.2 tls if (what & EV_WRITE)
347 1.1.1.1.8.2 tls bev_async_consider_writing(bev_async);
348 1.1.1.1.8.2 tls return 0;
349 1.1.1.1.8.2 tls }
350 1.1.1.1.8.2 tls
351 1.1.1.1.8.2 tls static int
352 1.1.1.1.8.2 tls be_async_disable(struct bufferevent *bev, short what)
353 1.1.1.1.8.2 tls {
354 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
355 1.1.1.1.8.2 tls /* XXXX If we disable reading or writing, we may want to consider
356 1.1.1.1.8.2 tls * canceling any in-progress read or write operation, though it might
357 1.1.1.1.8.2 tls * not work. */
358 1.1.1.1.8.2 tls
359 1.1.1.1.8.2 tls if (what & EV_READ) {
360 1.1.1.1.8.2 tls BEV_DEL_GENERIC_READ_TIMEOUT(bev);
361 1.1.1.1.8.2 tls bev_async_del_read(bev_async);
362 1.1.1.1.8.2 tls }
363 1.1.1.1.8.2 tls if (what & EV_WRITE) {
364 1.1.1.1.8.2 tls BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
365 1.1.1.1.8.2 tls bev_async_del_write(bev_async);
366 1.1.1.1.8.2 tls }
367 1.1.1.1.8.2 tls
368 1.1.1.1.8.2 tls return 0;
369 1.1.1.1.8.2 tls }
370 1.1.1.1.8.2 tls
371 1.1.1.1.8.2 tls static void
372 1.1.1.1.8.2 tls be_async_destruct(struct bufferevent *bev)
373 1.1.1.1.8.2 tls {
374 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
375 1.1.1.1.8.2 tls struct bufferevent_private *bev_p = BEV_UPCAST(bev);
376 1.1.1.1.8.2 tls evutil_socket_t fd;
377 1.1.1.1.8.2 tls
378 1.1.1.1.8.2 tls EVUTIL_ASSERT(!upcast(bev)->write_in_progress &&
379 1.1.1.1.8.2 tls !upcast(bev)->read_in_progress);
380 1.1.1.1.8.2 tls
381 1.1.1.1.8.2 tls bev_async_del_read(bev_async);
382 1.1.1.1.8.2 tls bev_async_del_write(bev_async);
383 1.1.1.1.8.2 tls
384 1.1.1.1.8.2 tls fd = evbuffer_overlapped_get_fd_(bev->input);
385 1.1.1.1.8.2 tls if (bev_p->options & BEV_OPT_CLOSE_ON_FREE) {
386 1.1.1.1.8.2 tls /* XXXX possible double-close */
387 1.1.1.1.8.2 tls evutil_closesocket(fd);
388 1.1.1.1.8.2 tls }
389 1.1.1.1.8.2 tls /* delete this in case non-blocking connect was used */
390 1.1.1.1.8.2 tls if (event_initialized(&bev->ev_write)) {
391 1.1.1.1.8.2 tls event_del(&bev->ev_write);
392 1.1.1.1.8.2 tls bufferevent_del_generic_timeout_cbs_(bev);
393 1.1.1.1.8.2 tls }
394 1.1.1.1.8.2 tls }
395 1.1.1.1.8.2 tls
396 1.1.1.1.8.2 tls /* GetQueuedCompletionStatus doesn't reliably yield WSA error codes, so
397 1.1.1.1.8.2 tls * we use WSAGetOverlappedResult to translate. */
398 1.1.1.1.8.2 tls static void
399 1.1.1.1.8.2 tls bev_async_set_wsa_error(struct bufferevent *bev, struct event_overlapped *eo)
400 1.1.1.1.8.2 tls {
401 1.1.1.1.8.2 tls DWORD bytes, flags;
402 1.1.1.1.8.2 tls evutil_socket_t fd;
403 1.1.1.1.8.2 tls
404 1.1.1.1.8.2 tls fd = evbuffer_overlapped_get_fd_(bev->input);
405 1.1.1.1.8.2 tls WSAGetOverlappedResult(fd, &eo->overlapped, &bytes, FALSE, &flags);
406 1.1.1.1.8.2 tls }
407 1.1.1.1.8.2 tls
408 1.1.1.1.8.2 tls static int
409 1.1.1.1.8.2 tls be_async_flush(struct bufferevent *bev, short what,
410 1.1.1.1.8.2 tls enum bufferevent_flush_mode mode)
411 1.1.1.1.8.2 tls {
412 1.1.1.1.8.2 tls return 0;
413 1.1.1.1.8.2 tls }
414 1.1.1.1.8.2 tls
415 1.1.1.1.8.2 tls static void
416 1.1.1.1.8.2 tls connect_complete(struct event_overlapped *eo, ev_uintptr_t key,
417 1.1.1.1.8.2 tls ev_ssize_t nbytes, int ok)
418 1.1.1.1.8.2 tls {
419 1.1.1.1.8.2 tls struct bufferevent_async *bev_a = upcast_connect(eo);
420 1.1.1.1.8.2 tls struct bufferevent *bev = &bev_a->bev.bev;
421 1.1.1.1.8.2 tls evutil_socket_t sock;
422 1.1.1.1.8.2 tls
423 1.1.1.1.8.2 tls BEV_LOCK(bev);
424 1.1.1.1.8.2 tls
425 1.1.1.1.8.2 tls EVUTIL_ASSERT(bev_a->bev.connecting);
426 1.1.1.1.8.2 tls bev_a->bev.connecting = 0;
427 1.1.1.1.8.2 tls sock = evbuffer_overlapped_get_fd_(bev_a->bev.bev.input);
428 1.1.1.1.8.2 tls /* XXXX Handle error? */
429 1.1.1.1.8.2 tls setsockopt(sock, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
430 1.1.1.1.8.2 tls
431 1.1.1.1.8.2 tls if (ok)
432 1.1.1.1.8.2 tls bufferevent_async_set_connected_(bev);
433 1.1.1.1.8.2 tls else
434 1.1.1.1.8.2 tls bev_async_set_wsa_error(bev, eo);
435 1.1.1.1.8.2 tls
436 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev,
437 1.1.1.1.8.2 tls ok? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR);
438 1.1.1.1.8.2 tls
439 1.1.1.1.8.2 tls event_base_del_virtual_(bev->ev_base);
440 1.1.1.1.8.2 tls
441 1.1.1.1.8.2 tls bufferevent_decref_and_unlock_(bev);
442 1.1.1.1.8.2 tls }
443 1.1.1.1.8.2 tls
444 1.1.1.1.8.2 tls static void
445 1.1.1.1.8.2 tls read_complete(struct event_overlapped *eo, ev_uintptr_t key,
446 1.1.1.1.8.2 tls ev_ssize_t nbytes, int ok)
447 1.1.1.1.8.2 tls {
448 1.1.1.1.8.2 tls struct bufferevent_async *bev_a = upcast_read(eo);
449 1.1.1.1.8.2 tls struct bufferevent *bev = &bev_a->bev.bev;
450 1.1.1.1.8.2 tls short what = BEV_EVENT_READING;
451 1.1.1.1.8.2 tls ev_ssize_t amount_unread;
452 1.1.1.1.8.2 tls BEV_LOCK(bev);
453 1.1.1.1.8.2 tls EVUTIL_ASSERT(bev_a->read_in_progress);
454 1.1.1.1.8.2 tls
455 1.1.1.1.8.2 tls amount_unread = bev_a->read_in_progress - nbytes;
456 1.1.1.1.8.2 tls evbuffer_commit_read_(bev->input, nbytes);
457 1.1.1.1.8.2 tls bev_a->read_in_progress = 0;
458 1.1.1.1.8.2 tls if (amount_unread)
459 1.1.1.1.8.2 tls bufferevent_decrement_read_buckets_(&bev_a->bev, -amount_unread);
460 1.1.1.1.8.2 tls
461 1.1.1.1.8.2 tls if (!ok)
462 1.1.1.1.8.2 tls bev_async_set_wsa_error(bev, eo);
463 1.1.1.1.8.2 tls
464 1.1.1.1.8.2 tls if (bev_a->ok) {
465 1.1.1.1.8.2 tls if (ok && nbytes) {
466 1.1.1.1.8.2 tls BEV_RESET_GENERIC_READ_TIMEOUT(bev);
467 1.1.1.1.8.2 tls if (evbuffer_get_length(bev->input) >= bev->wm_read.low)
468 1.1.1.1.8.2 tls bufferevent_run_readcb_(bev);
469 1.1.1.1.8.2 tls bev_async_consider_reading(bev_a);
470 1.1.1.1.8.2 tls } else if (!ok) {
471 1.1.1.1.8.2 tls what |= BEV_EVENT_ERROR;
472 1.1.1.1.8.2 tls bev_a->ok = 0;
473 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, what);
474 1.1.1.1.8.2 tls } else if (!nbytes) {
475 1.1.1.1.8.2 tls what |= BEV_EVENT_EOF;
476 1.1.1.1.8.2 tls bev_a->ok = 0;
477 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, what);
478 1.1.1.1.8.2 tls }
479 1.1.1.1.8.2 tls }
480 1.1.1.1.8.2 tls
481 1.1.1.1.8.2 tls bufferevent_decref_and_unlock_(bev);
482 1.1.1.1.8.2 tls }
483 1.1.1.1.8.2 tls
484 1.1.1.1.8.2 tls static void
485 1.1.1.1.8.2 tls write_complete(struct event_overlapped *eo, ev_uintptr_t key,
486 1.1.1.1.8.2 tls ev_ssize_t nbytes, int ok)
487 1.1.1.1.8.2 tls {
488 1.1.1.1.8.2 tls struct bufferevent_async *bev_a = upcast_write(eo);
489 1.1.1.1.8.2 tls struct bufferevent *bev = &bev_a->bev.bev;
490 1.1.1.1.8.2 tls short what = BEV_EVENT_WRITING;
491 1.1.1.1.8.2 tls ev_ssize_t amount_unwritten;
492 1.1.1.1.8.2 tls
493 1.1.1.1.8.2 tls BEV_LOCK(bev);
494 1.1.1.1.8.2 tls EVUTIL_ASSERT(bev_a->write_in_progress);
495 1.1.1.1.8.2 tls
496 1.1.1.1.8.2 tls amount_unwritten = bev_a->write_in_progress - nbytes;
497 1.1.1.1.8.2 tls evbuffer_commit_write_(bev->output, nbytes);
498 1.1.1.1.8.2 tls bev_a->write_in_progress = 0;
499 1.1.1.1.8.2 tls
500 1.1.1.1.8.2 tls if (amount_unwritten)
501 1.1.1.1.8.2 tls bufferevent_decrement_write_buckets_(&bev_a->bev,
502 1.1.1.1.8.2 tls -amount_unwritten);
503 1.1.1.1.8.2 tls
504 1.1.1.1.8.2 tls
505 1.1.1.1.8.2 tls if (!ok)
506 1.1.1.1.8.2 tls bev_async_set_wsa_error(bev, eo);
507 1.1.1.1.8.2 tls
508 1.1.1.1.8.2 tls if (bev_a->ok) {
509 1.1.1.1.8.2 tls if (ok && nbytes) {
510 1.1.1.1.8.2 tls BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
511 1.1.1.1.8.2 tls if (evbuffer_get_length(bev->output) <=
512 1.1.1.1.8.2 tls bev->wm_write.low)
513 1.1.1.1.8.2 tls bufferevent_run_writecb_(bev);
514 1.1.1.1.8.2 tls bev_async_consider_writing(bev_a);
515 1.1.1.1.8.2 tls } else if (!ok) {
516 1.1.1.1.8.2 tls what |= BEV_EVENT_ERROR;
517 1.1.1.1.8.2 tls bev_a->ok = 0;
518 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, what);
519 1.1.1.1.8.2 tls } else if (!nbytes) {
520 1.1.1.1.8.2 tls what |= BEV_EVENT_EOF;
521 1.1.1.1.8.2 tls bev_a->ok = 0;
522 1.1.1.1.8.2 tls bufferevent_run_eventcb_(bev, what);
523 1.1.1.1.8.2 tls }
524 1.1.1.1.8.2 tls }
525 1.1.1.1.8.2 tls
526 1.1.1.1.8.2 tls bufferevent_decref_and_unlock_(bev);
527 1.1.1.1.8.2 tls }
528 1.1.1.1.8.2 tls
529 1.1.1.1.8.2 tls struct bufferevent *
530 1.1.1.1.8.2 tls bufferevent_async_new_(struct event_base *base,
531 1.1.1.1.8.2 tls evutil_socket_t fd, int options)
532 1.1.1.1.8.2 tls {
533 1.1.1.1.8.2 tls struct bufferevent_async *bev_a;
534 1.1.1.1.8.2 tls struct bufferevent *bev;
535 1.1.1.1.8.2 tls struct event_iocp_port *iocp;
536 1.1.1.1.8.2 tls
537 1.1.1.1.8.2 tls options |= BEV_OPT_THREADSAFE;
538 1.1.1.1.8.2 tls
539 1.1.1.1.8.2 tls if (!(iocp = event_base_get_iocp_(base)))
540 1.1.1.1.8.2 tls return NULL;
541 1.1.1.1.8.2 tls
542 1.1.1.1.8.2 tls if (fd >= 0 && event_iocp_port_associate_(iocp, fd, 1)<0) {
543 1.1.1.1.8.2 tls int err = GetLastError();
544 1.1.1.1.8.2 tls /* We may have alrady associated this fd with a port.
545 1.1.1.1.8.2 tls * Let's hope it's this port, and that the error code
546 1.1.1.1.8.2 tls * for doing this neer changes. */
547 1.1.1.1.8.2 tls if (err != ERROR_INVALID_PARAMETER)
548 1.1.1.1.8.2 tls return NULL;
549 1.1.1.1.8.2 tls }
550 1.1.1.1.8.2 tls
551 1.1.1.1.8.2 tls if (!(bev_a = mm_calloc(1, sizeof(struct bufferevent_async))))
552 1.1.1.1.8.2 tls return NULL;
553 1.1.1.1.8.2 tls
554 1.1.1.1.8.2 tls bev = &bev_a->bev.bev;
555 1.1.1.1.8.2 tls if (!(bev->input = evbuffer_overlapped_new_(fd))) {
556 1.1.1.1.8.2 tls mm_free(bev_a);
557 1.1.1.1.8.2 tls return NULL;
558 1.1.1.1.8.2 tls }
559 1.1.1.1.8.2 tls if (!(bev->output = evbuffer_overlapped_new_(fd))) {
560 1.1.1.1.8.2 tls evbuffer_free(bev->input);
561 1.1.1.1.8.2 tls mm_free(bev_a);
562 1.1.1.1.8.2 tls return NULL;
563 1.1.1.1.8.2 tls }
564 1.1.1.1.8.2 tls
565 1.1.1.1.8.2 tls if (bufferevent_init_common_(&bev_a->bev, base, &bufferevent_ops_async,
566 1.1.1.1.8.2 tls options)<0)
567 1.1.1.1.8.2 tls goto err;
568 1.1.1.1.8.2 tls
569 1.1.1.1.8.2 tls evbuffer_add_cb(bev->input, be_async_inbuf_callback, bev);
570 1.1.1.1.8.2 tls evbuffer_add_cb(bev->output, be_async_outbuf_callback, bev);
571 1.1.1.1.8.2 tls
572 1.1.1.1.8.2 tls event_overlapped_init_(&bev_a->connect_overlapped, connect_complete);
573 1.1.1.1.8.2 tls event_overlapped_init_(&bev_a->read_overlapped, read_complete);
574 1.1.1.1.8.2 tls event_overlapped_init_(&bev_a->write_overlapped, write_complete);
575 1.1.1.1.8.2 tls
576 1.1.1.1.8.2 tls bev_a->ok = fd >= 0;
577 1.1.1.1.8.2 tls if (bev_a->ok)
578 1.1.1.1.8.2 tls bufferevent_init_generic_timeout_cbs_(bev);
579 1.1.1.1.8.2 tls
580 1.1.1.1.8.2 tls return bev;
581 1.1.1.1.8.2 tls err:
582 1.1.1.1.8.2 tls bufferevent_free(&bev_a->bev.bev);
583 1.1.1.1.8.2 tls return NULL;
584 1.1.1.1.8.2 tls }
585 1.1.1.1.8.2 tls
586 1.1.1.1.8.2 tls void
587 1.1.1.1.8.2 tls bufferevent_async_set_connected_(struct bufferevent *bev)
588 1.1.1.1.8.2 tls {
589 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
590 1.1.1.1.8.2 tls bev_async->ok = 1;
591 1.1.1.1.8.2 tls bufferevent_init_generic_timeout_cbs_(bev);
592 1.1.1.1.8.2 tls /* Now's a good time to consider reading/writing */
593 1.1.1.1.8.2 tls be_async_enable(bev, bev->enabled);
594 1.1.1.1.8.2 tls }
595 1.1.1.1.8.2 tls
596 1.1.1.1.8.2 tls int
597 1.1.1.1.8.2 tls bufferevent_async_can_connect_(struct bufferevent *bev)
598 1.1.1.1.8.2 tls {
599 1.1.1.1.8.2 tls const struct win32_extension_fns *ext =
600 1.1.1.1.8.2 tls event_get_win32_extension_fns_();
601 1.1.1.1.8.2 tls
602 1.1.1.1.8.2 tls if (BEV_IS_ASYNC(bev) &&
603 1.1.1.1.8.2 tls event_base_get_iocp_(bev->ev_base) &&
604 1.1.1.1.8.2 tls ext && ext->ConnectEx)
605 1.1.1.1.8.2 tls return 1;
606 1.1.1.1.8.2 tls
607 1.1.1.1.8.2 tls return 0;
608 1.1.1.1.8.2 tls }
609 1.1.1.1.8.2 tls
610 1.1.1.1.8.2 tls int
611 1.1.1.1.8.2 tls bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
612 1.1.1.1.8.2 tls const struct sockaddr *sa, int socklen)
613 1.1.1.1.8.2 tls {
614 1.1.1.1.8.2 tls BOOL rc;
615 1.1.1.1.8.2 tls struct bufferevent_async *bev_async = upcast(bev);
616 1.1.1.1.8.2 tls struct sockaddr_storage ss;
617 1.1.1.1.8.2 tls const struct win32_extension_fns *ext =
618 1.1.1.1.8.2 tls event_get_win32_extension_fns_();
619 1.1.1.1.8.2 tls
620 1.1.1.1.8.2 tls EVUTIL_ASSERT(ext && ext->ConnectEx && fd >= 0 && sa != NULL);
621 1.1.1.1.8.2 tls
622 1.1.1.1.8.2 tls /* ConnectEx() requires that the socket be bound to an address
623 1.1.1.1.8.2 tls * with bind() before using, otherwise it will fail. We attempt
624 1.1.1.1.8.2 tls * to issue a bind() here, taking into account that the error
625 1.1.1.1.8.2 tls * code is set to WSAEINVAL when the socket is already bound. */
626 1.1.1.1.8.2 tls memset(&ss, 0, sizeof(ss));
627 1.1.1.1.8.2 tls if (sa->sa_family == AF_INET) {
628 1.1.1.1.8.2 tls struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
629 1.1.1.1.8.2 tls sin->sin_family = AF_INET;
630 1.1.1.1.8.2 tls sin->sin_addr.s_addr = INADDR_ANY;
631 1.1.1.1.8.2 tls } else if (sa->sa_family == AF_INET6) {
632 1.1.1.1.8.2 tls struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
633 1.1.1.1.8.2 tls sin6->sin6_family = AF_INET6;
634 1.1.1.1.8.2 tls sin6->sin6_addr = in6addr_any;
635 1.1.1.1.8.2 tls } else {
636 1.1.1.1.8.2 tls /* Well, the user will have to bind() */
637 1.1.1.1.8.2 tls return -1;
638 1.1.1.1.8.2 tls }
639 1.1.1.1.8.2 tls if (bind(fd, (struct sockaddr *)&ss, sizeof(ss)) < 0 &&
640 1.1.1.1.8.2 tls WSAGetLastError() != WSAEINVAL)
641 1.1.1.1.8.2 tls return -1;
642 1.1.1.1.8.2 tls
643 1.1.1.1.8.2 tls event_base_add_virtual_(bev->ev_base);
644 1.1.1.1.8.2 tls bufferevent_incref_(bev);
645 1.1.1.1.8.2 tls rc = ext->ConnectEx(fd, sa, socklen, NULL, 0, NULL,
646 1.1.1.1.8.2 tls &bev_async->connect_overlapped.overlapped);
647 1.1.1.1.8.2 tls if (rc || WSAGetLastError() == ERROR_IO_PENDING)
648 1.1.1.1.8.2 tls return 0;
649 1.1.1.1.8.2 tls
650 1.1.1.1.8.2 tls event_base_del_virtual_(bev->ev_base);
651 1.1.1.1.8.2 tls bufferevent_decref_(bev);
652 1.1.1.1.8.2 tls
653 1.1.1.1.8.2 tls return -1;
654 1.1.1.1.8.2 tls }
655 1.1.1.1.8.2 tls
656 1.1.1.1.8.2 tls static int
657 1.1.1.1.8.2 tls be_async_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
658 1.1.1.1.8.2 tls union bufferevent_ctrl_data *data)
659 1.1.1.1.8.2 tls {
660 1.1.1.1.8.2 tls switch (op) {
661 1.1.1.1.8.2 tls case BEV_CTRL_GET_FD:
662 1.1.1.1.8.2 tls data->fd = evbuffer_overlapped_get_fd_(bev->input);
663 1.1.1.1.8.2 tls return 0;
664 1.1.1.1.8.2 tls case BEV_CTRL_SET_FD: {
665 1.1.1.1.8.2 tls struct event_iocp_port *iocp;
666 1.1.1.1.8.2 tls
667 1.1.1.1.8.2 tls if (data->fd == evbuffer_overlapped_get_fd_(bev->input))
668 1.1.1.1.8.2 tls return 0;
669 1.1.1.1.8.2 tls if (!(iocp = event_base_get_iocp_(bev->ev_base)))
670 1.1.1.1.8.2 tls return -1;
671 1.1.1.1.8.2 tls if (event_iocp_port_associate_(iocp, data->fd, 1) < 0)
672 1.1.1.1.8.2 tls return -1;
673 1.1.1.1.8.2 tls evbuffer_overlapped_set_fd_(bev->input, data->fd);
674 1.1.1.1.8.2 tls evbuffer_overlapped_set_fd_(bev->output, data->fd);
675 1.1.1.1.8.2 tls return 0;
676 1.1.1.1.8.2 tls }
677 1.1.1.1.8.2 tls case BEV_CTRL_CANCEL_ALL: {
678 1.1.1.1.8.2 tls struct bufferevent_async *bev_a = upcast(bev);
679 1.1.1.1.8.2 tls evutil_socket_t fd = evbuffer_overlapped_get_fd_(bev->input);
680 1.1.1.1.8.2 tls if (fd != (evutil_socket_t)INVALID_SOCKET &&
681 1.1.1.1.8.2 tls (bev_a->bev.options & BEV_OPT_CLOSE_ON_FREE)) {
682 1.1.1.1.8.2 tls closesocket(fd);
683 1.1.1.1.8.2 tls }
684 1.1.1.1.8.2 tls bev_a->ok = 0;
685 1.1.1.1.8.2 tls return 0;
686 1.1.1.1.8.2 tls }
687 1.1.1.1.8.2 tls case BEV_CTRL_GET_UNDERLYING:
688 1.1.1.1.8.2 tls default:
689 1.1.1.1.8.2 tls return -1;
690 1.1.1.1.8.2 tls }
691 1.1.1.1.8.2 tls }
692 1.1.1.1.8.2 tls
693 1.1.1.1.8.2 tls
694