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