1 /* $NetBSD: bufferevent_async.c,v 1.1.1.1 2013/04/11 16:43:28 christos Exp $ */
3 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "event2/event-config.h"
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: bufferevent_async.c,v 1.1.1.1 2013/04/11 16:43:28 christos Exp $");
34 #ifdef _EVENT_HAVE_SYS_TIME_H
42 #ifdef _EVENT_HAVE_STDARG_H
45 #ifdef _EVENT_HAVE_UNISTD_H
54 #include <sys/queue.h>
56 #include "event2/util.h"
57 #include "event2/bufferevent.h"
58 #include "event2/buffer.h"
59 #include "event2/bufferevent_struct.h"
60 #include "event2/event.h"
61 #include "event2/util.h"
62 #include "event-internal.h"
63 #include "log-internal.h"
64 #include "mm-internal.h"
65 #include "bufferevent-internal.h"
66 #include "util-internal.h"
67 #include "iocp-internal.h"
69 #ifndef SO_UPDATE_CONNECT_CONTEXT
70 /* Mingw is sometimes missing this */
71 #define SO_UPDATE_CONNECT_CONTEXT 0x7010
75 static int be_async_enable(struct bufferevent
*, short);
76 static int be_async_disable(struct bufferevent
*, short);
77 static void be_async_destruct(struct bufferevent
*);
78 static int be_async_flush(struct bufferevent
*, short, enum bufferevent_flush_mode
);
79 static int be_async_ctrl(struct bufferevent
*, enum bufferevent_ctrl_op
, union bufferevent_ctrl_data
*);
81 struct bufferevent_async
{
82 struct bufferevent_private bev
;
83 struct event_overlapped connect_overlapped
;
84 struct event_overlapped read_overlapped
;
85 struct event_overlapped write_overlapped
;
86 size_t read_in_progress
;
87 size_t write_in_progress
;
89 unsigned read_added
: 1;
90 unsigned write_added
: 1;
93 const struct bufferevent_ops bufferevent_ops_async
= {
95 evutil_offsetof(struct bufferevent_async
, bev
.bev
),
99 _bufferevent_generic_adj_timeouts
,
104 static inline struct bufferevent_async
*
105 upcast(struct bufferevent
*bev
)
107 struct bufferevent_async
*bev_a
;
108 if (bev
->be_ops
!= &bufferevent_ops_async
)
110 bev_a
= EVUTIL_UPCAST(bev
, struct bufferevent_async
, bev
.bev
);
114 static inline struct bufferevent_async
*
115 upcast_connect(struct event_overlapped
*eo
)
117 struct bufferevent_async
*bev_a
;
118 bev_a
= EVUTIL_UPCAST(eo
, struct bufferevent_async
, connect_overlapped
);
119 EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a
->bev
.bev
));
123 static inline struct bufferevent_async
*
124 upcast_read(struct event_overlapped
*eo
)
126 struct bufferevent_async
*bev_a
;
127 bev_a
= EVUTIL_UPCAST(eo
, struct bufferevent_async
, read_overlapped
);
128 EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a
->bev
.bev
));
132 static inline struct bufferevent_async
*
133 upcast_write(struct event_overlapped
*eo
)
135 struct bufferevent_async
*bev_a
;
136 bev_a
= EVUTIL_UPCAST(eo
, struct bufferevent_async
, write_overlapped
);
137 EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a
->bev
.bev
));
142 bev_async_del_write(struct bufferevent_async
*beva
)
144 struct bufferevent
*bev
= &beva
->bev
.bev
;
146 if (beva
->write_added
) {
147 beva
->write_added
= 0;
148 event_base_del_virtual(bev
->ev_base
);
153 bev_async_del_read(struct bufferevent_async
*beva
)
155 struct bufferevent
*bev
= &beva
->bev
.bev
;
157 if (beva
->read_added
) {
158 beva
->read_added
= 0;
159 event_base_del_virtual(bev
->ev_base
);
164 bev_async_add_write(struct bufferevent_async
*beva
)
166 struct bufferevent
*bev
= &beva
->bev
.bev
;
168 if (!beva
->write_added
) {
169 beva
->write_added
= 1;
170 event_base_add_virtual(bev
->ev_base
);
175 bev_async_add_read(struct bufferevent_async
*beva
)
177 struct bufferevent
*bev
= &beva
->bev
.bev
;
179 if (!beva
->read_added
) {
180 beva
->read_added
= 1;
181 event_base_add_virtual(bev
->ev_base
);
186 bev_async_consider_writing(struct bufferevent_async
*beva
)
190 struct bufferevent
*bev
= &beva
->bev
.bev
;
192 /* Don't write if there's a write in progress, or we do not
193 * want to write, or when there's nothing left to write. */
194 if (beva
->write_in_progress
|| beva
->bev
.connecting
)
196 if (!beva
->ok
|| !(bev
->enabled
&EV_WRITE
) ||
197 !evbuffer_get_length(bev
->output
)) {
198 bev_async_del_write(beva
);
202 at_most
= evbuffer_get_length(bev
->output
);
204 /* This is safe so long as bufferevent_get_write_max never returns
205 * more than INT_MAX. That's true for now. XXXX */
206 limit
= (int)_bufferevent_get_write_max(&beva
->bev
);
207 if (at_most
>= (size_t)limit
&& limit
>= 0)
210 if (beva
->bev
.write_suspended
) {
211 bev_async_del_write(beva
);
215 /* XXXX doesn't respect low-water mark very well. */
216 bufferevent_incref(bev
);
217 if (evbuffer_launch_write(bev
->output
, at_most
,
218 &beva
->write_overlapped
)) {
219 bufferevent_decref(bev
);
221 _bufferevent_run_eventcb(bev
, BEV_EVENT_ERROR
);
223 beva
->write_in_progress
= at_most
;
224 _bufferevent_decrement_write_buckets(&beva
->bev
, at_most
);
225 bev_async_add_write(beva
);
230 bev_async_consider_reading(struct bufferevent_async
*beva
)
236 struct bufferevent
*bev
= &beva
->bev
.bev
;
238 /* Don't read if there is a read in progress, or we do not
240 if (beva
->read_in_progress
|| beva
->bev
.connecting
)
242 if (!beva
->ok
|| !(bev
->enabled
&EV_READ
)) {
243 bev_async_del_read(beva
);
247 /* Don't read if we're full */
248 cur_size
= evbuffer_get_length(bev
->input
);
249 read_high
= bev
->wm_read
.high
;
251 if (cur_size
>= read_high
) {
252 bev_async_del_read(beva
);
255 at_most
= read_high
- cur_size
;
257 at_most
= 16384; /* FIXME totally magic. */
260 /* XXXX This over-commits. */
261 /* XXXX see also not above on cast on _bufferevent_get_write_max() */
262 limit
= (int)_bufferevent_get_read_max(&beva
->bev
);
263 if (at_most
>= (size_t)limit
&& limit
>= 0)
266 if (beva
->bev
.read_suspended
) {
267 bev_async_del_read(beva
);
271 bufferevent_incref(bev
);
272 if (evbuffer_launch_read(bev
->input
, at_most
, &beva
->read_overlapped
)) {
274 _bufferevent_run_eventcb(bev
, BEV_EVENT_ERROR
);
275 bufferevent_decref(bev
);
277 beva
->read_in_progress
= at_most
;
278 _bufferevent_decrement_read_buckets(&beva
->bev
, at_most
);
279 bev_async_add_read(beva
);
286 be_async_outbuf_callback(struct evbuffer
*buf
,
287 const struct evbuffer_cb_info
*cbinfo
,
290 struct bufferevent
*bev
= arg
;
291 struct bufferevent_async
*bev_async
= upcast(bev
);
293 /* If we added data to the outbuf and were not writing before,
294 * we may want to write now. */
296 _bufferevent_incref_and_lock(bev
);
299 bev_async_consider_writing(bev_async
);
301 _bufferevent_decref_and_unlock(bev
);
305 be_async_inbuf_callback(struct evbuffer
*buf
,
306 const struct evbuffer_cb_info
*cbinfo
,
309 struct bufferevent
*bev
= arg
;
310 struct bufferevent_async
*bev_async
= upcast(bev
);
312 /* If we drained data from the inbuf and were not reading before,
313 * we may want to read now */
315 _bufferevent_incref_and_lock(bev
);
317 if (cbinfo
->n_deleted
)
318 bev_async_consider_reading(bev_async
);
320 _bufferevent_decref_and_unlock(bev
);
324 be_async_enable(struct bufferevent
*buf
, short what
)
326 struct bufferevent_async
*bev_async
= upcast(buf
);
331 if (bev_async
->bev
.connecting
) {
332 /* Don't launch anything during connection attempts. */
337 BEV_RESET_GENERIC_READ_TIMEOUT(buf
);
339 BEV_RESET_GENERIC_WRITE_TIMEOUT(buf
);
341 /* If we newly enable reading or writing, and we aren't reading or
342 writing already, consider launching a new read or write. */
345 bev_async_consider_reading(bev_async
);
347 bev_async_consider_writing(bev_async
);
352 be_async_disable(struct bufferevent
*bev
, short what
)
354 struct bufferevent_async
*bev_async
= upcast(bev
);
355 /* XXXX If we disable reading or writing, we may want to consider
356 * canceling any in-progress read or write operation, though it might
359 if (what
& EV_READ
) {
360 BEV_DEL_GENERIC_READ_TIMEOUT(bev
);
361 bev_async_del_read(bev_async
);
363 if (what
& EV_WRITE
) {
364 BEV_DEL_GENERIC_WRITE_TIMEOUT(bev
);
365 bev_async_del_write(bev_async
);
372 be_async_destruct(struct bufferevent
*bev
)
374 struct bufferevent_async
*bev_async
= upcast(bev
);
375 struct bufferevent_private
*bev_p
= BEV_UPCAST(bev
);
378 EVUTIL_ASSERT(!upcast(bev
)->write_in_progress
&&
379 !upcast(bev
)->read_in_progress
);
381 bev_async_del_read(bev_async
);
382 bev_async_del_write(bev_async
);
384 fd
= _evbuffer_overlapped_get_fd(bev
->input
);
385 if (bev_p
->options
& BEV_OPT_CLOSE_ON_FREE
) {
386 /* XXXX possible double-close */
387 evutil_closesocket(fd
);
389 /* delete this in case non-blocking connect was used */
390 if (event_initialized(&bev
->ev_write
)) {
391 event_del(&bev
->ev_write
);
392 _bufferevent_del_generic_timeout_cbs(bev
);
396 /* GetQueuedCompletionStatus doesn't reliably yield WSA error codes, so
397 * we use WSAGetOverlappedResult to translate. */
399 bev_async_set_wsa_error(struct bufferevent
*bev
, struct event_overlapped
*eo
)
404 fd
= _evbuffer_overlapped_get_fd(bev
->input
);
405 WSAGetOverlappedResult(fd
, &eo
->overlapped
, &bytes
, FALSE
, &flags
);
409 be_async_flush(struct bufferevent
*bev
, short what
,
410 enum bufferevent_flush_mode mode
)
416 connect_complete(struct event_overlapped
*eo
, ev_uintptr_t key
,
417 ev_ssize_t nbytes
, int ok
)
419 struct bufferevent_async
*bev_a
= upcast_connect(eo
);
420 struct bufferevent
*bev
= &bev_a
->bev
.bev
;
421 evutil_socket_t sock
;
425 EVUTIL_ASSERT(bev_a
->bev
.connecting
);
426 bev_a
->bev
.connecting
= 0;
427 sock
= _evbuffer_overlapped_get_fd(bev_a
->bev
.bev
.input
);
428 /* XXXX Handle error? */
429 setsockopt(sock
, SOL_SOCKET
, SO_UPDATE_CONNECT_CONTEXT
, NULL
, 0);
432 bufferevent_async_set_connected(bev
);
434 bev_async_set_wsa_error(bev
, eo
);
436 _bufferevent_run_eventcb(bev
,
437 ok
? BEV_EVENT_CONNECTED
: BEV_EVENT_ERROR
);
439 event_base_del_virtual(bev
->ev_base
);
441 _bufferevent_decref_and_unlock(bev
);
445 read_complete(struct event_overlapped
*eo
, ev_uintptr_t key
,
446 ev_ssize_t nbytes
, int ok
)
448 struct bufferevent_async
*bev_a
= upcast_read(eo
);
449 struct bufferevent
*bev
= &bev_a
->bev
.bev
;
450 short what
= BEV_EVENT_READING
;
451 ev_ssize_t amount_unread
;
453 EVUTIL_ASSERT(bev_a
->read_in_progress
);
455 amount_unread
= bev_a
->read_in_progress
- nbytes
;
456 evbuffer_commit_read(bev
->input
, nbytes
);
457 bev_a
->read_in_progress
= 0;
459 _bufferevent_decrement_read_buckets(&bev_a
->bev
, -amount_unread
);
462 bev_async_set_wsa_error(bev
, eo
);
466 BEV_RESET_GENERIC_READ_TIMEOUT(bev
);
467 if (evbuffer_get_length(bev
->input
) >= bev
->wm_read
.low
)
468 _bufferevent_run_readcb(bev
);
469 bev_async_consider_reading(bev_a
);
471 what
|= BEV_EVENT_ERROR
;
473 _bufferevent_run_eventcb(bev
, what
);
474 } else if (!nbytes
) {
475 what
|= BEV_EVENT_EOF
;
477 _bufferevent_run_eventcb(bev
, what
);
481 _bufferevent_decref_and_unlock(bev
);
485 write_complete(struct event_overlapped
*eo
, ev_uintptr_t key
,
486 ev_ssize_t nbytes
, int ok
)
488 struct bufferevent_async
*bev_a
= upcast_write(eo
);
489 struct bufferevent
*bev
= &bev_a
->bev
.bev
;
490 short what
= BEV_EVENT_WRITING
;
491 ev_ssize_t amount_unwritten
;
494 EVUTIL_ASSERT(bev_a
->write_in_progress
);
496 amount_unwritten
= bev_a
->write_in_progress
- nbytes
;
497 evbuffer_commit_write(bev
->output
, nbytes
);
498 bev_a
->write_in_progress
= 0;
500 if (amount_unwritten
)
501 _bufferevent_decrement_write_buckets(&bev_a
->bev
,
506 bev_async_set_wsa_error(bev
, eo
);
510 BEV_RESET_GENERIC_WRITE_TIMEOUT(bev
);
511 if (evbuffer_get_length(bev
->output
) <=
513 _bufferevent_run_writecb(bev
);
514 bev_async_consider_writing(bev_a
);
516 what
|= BEV_EVENT_ERROR
;
518 _bufferevent_run_eventcb(bev
, what
);
519 } else if (!nbytes
) {
520 what
|= BEV_EVENT_EOF
;
522 _bufferevent_run_eventcb(bev
, what
);
526 _bufferevent_decref_and_unlock(bev
);
530 bufferevent_async_new(struct event_base
*base
,
531 evutil_socket_t fd
, int options
)
533 struct bufferevent_async
*bev_a
;
534 struct bufferevent
*bev
;
535 struct event_iocp_port
*iocp
;
537 options
|= BEV_OPT_THREADSAFE
;
539 if (!(iocp
= event_base_get_iocp(base
)))
542 if (fd
>= 0 && event_iocp_port_associate(iocp
, fd
, 1)<0) {
543 int err
= GetLastError();
544 /* We may have alrady associated this fd with a port.
545 * Let's hope it's this port, and that the error code
546 * for doing this neer changes. */
547 if (err
!= ERROR_INVALID_PARAMETER
)
551 if (!(bev_a
= mm_calloc(1, sizeof(struct bufferevent_async
))))
554 bev
= &bev_a
->bev
.bev
;
555 if (!(bev
->input
= evbuffer_overlapped_new(fd
))) {
559 if (!(bev
->output
= evbuffer_overlapped_new(fd
))) {
560 evbuffer_free(bev
->input
);
565 if (bufferevent_init_common(&bev_a
->bev
, base
, &bufferevent_ops_async
,
569 evbuffer_add_cb(bev
->input
, be_async_inbuf_callback
, bev
);
570 evbuffer_add_cb(bev
->output
, be_async_outbuf_callback
, bev
);
572 event_overlapped_init(&bev_a
->connect_overlapped
, connect_complete
);
573 event_overlapped_init(&bev_a
->read_overlapped
, read_complete
);
574 event_overlapped_init(&bev_a
->write_overlapped
, write_complete
);
578 _bufferevent_init_generic_timeout_cbs(bev
);
582 bufferevent_free(&bev_a
->bev
.bev
);
587 bufferevent_async_set_connected(struct bufferevent
*bev
)
589 struct bufferevent_async
*bev_async
= upcast(bev
);
591 _bufferevent_init_generic_timeout_cbs(bev
);
592 /* Now's a good time to consider reading/writing */
593 be_async_enable(bev
, bev
->enabled
);
597 bufferevent_async_can_connect(struct bufferevent
*bev
)
599 const struct win32_extension_fns
*ext
=
600 event_get_win32_extension_fns();
602 if (BEV_IS_ASYNC(bev
) &&
603 event_base_get_iocp(bev
->ev_base
) &&
604 ext
&& ext
->ConnectEx
)
611 bufferevent_async_connect(struct bufferevent
*bev
, evutil_socket_t fd
,
612 const struct sockaddr
*sa
, int socklen
)
615 struct bufferevent_async
*bev_async
= upcast(bev
);
616 struct sockaddr_storage ss
;
617 const struct win32_extension_fns
*ext
=
618 event_get_win32_extension_fns();
620 EVUTIL_ASSERT(ext
&& ext
->ConnectEx
&& fd
>= 0 && sa
!= NULL
);
622 /* ConnectEx() requires that the socket be bound to an address
623 * with bind() before using, otherwise it will fail. We attempt
624 * to issue a bind() here, taking into account that the error
625 * code is set to WSAEINVAL when the socket is already bound. */
626 memset(&ss
, 0, sizeof(ss
));
627 if (sa
->sa_family
== AF_INET
) {
628 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ss
;
629 sin
->sin_family
= AF_INET
;
630 sin
->sin_addr
.s_addr
= INADDR_ANY
;
631 } else if (sa
->sa_family
== AF_INET6
) {
632 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&ss
;
633 sin6
->sin6_family
= AF_INET6
;
634 sin6
->sin6_addr
= in6addr_any
;
636 /* Well, the user will have to bind() */
639 if (bind(fd
, (struct sockaddr
*)&ss
, sizeof(ss
)) < 0 &&
640 WSAGetLastError() != WSAEINVAL
)
643 event_base_add_virtual(bev
->ev_base
);
644 bufferevent_incref(bev
);
645 rc
= ext
->ConnectEx(fd
, sa
, socklen
, NULL
, 0, NULL
,
646 &bev_async
->connect_overlapped
.overlapped
);
647 if (rc
|| WSAGetLastError() == ERROR_IO_PENDING
)
650 event_base_del_virtual(bev
->ev_base
);
651 bufferevent_decref(bev
);
657 be_async_ctrl(struct bufferevent
*bev
, enum bufferevent_ctrl_op op
,
658 union bufferevent_ctrl_data
*data
)
661 case BEV_CTRL_GET_FD
:
662 data
->fd
= _evbuffer_overlapped_get_fd(bev
->input
);
664 case BEV_CTRL_SET_FD
: {
665 struct event_iocp_port
*iocp
;
667 if (data
->fd
== _evbuffer_overlapped_get_fd(bev
->input
))
669 if (!(iocp
= event_base_get_iocp(bev
->ev_base
)))
671 if (event_iocp_port_associate(iocp
, data
->fd
, 1) < 0)
673 _evbuffer_overlapped_set_fd(bev
->input
, data
->fd
);
674 _evbuffer_overlapped_set_fd(bev
->output
, data
->fd
);
677 case BEV_CTRL_CANCEL_ALL
: {
678 struct bufferevent_async
*bev_a
= upcast(bev
);
679 evutil_socket_t fd
= _evbuffer_overlapped_get_fd(bev
->input
);
680 if (fd
!= (evutil_socket_t
)INVALID_SOCKET
&&
681 (bev_a
->bev
.options
& BEV_OPT_CLOSE_ON_FREE
)) {
687 case BEV_CTRL_GET_UNDERLYING
: