2 * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos, Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
31 #include <sys/types.h>
33 #ifdef EVENT__HAVE_SYS_TIME_H
41 #ifdef EVENT__HAVE_STDARG_H
49 #include "event2/util.h"
50 #include "event2/buffer.h"
51 #include "event2/buffer_compat.h"
52 #include "event2/bufferevent.h"
53 #include "event2/bufferevent_struct.h"
54 #include "event2/bufferevent_compat.h"
55 #include "event2/event.h"
56 #include "event-internal.h"
57 #include "log-internal.h"
58 #include "mm-internal.h"
59 #include "bufferevent-internal.h"
60 #include "evbuffer-internal.h"
61 #include "util-internal.h"
63 static void bufferevent_cancel_all_(struct bufferevent *bev);
64 static void bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_);
67 bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
69 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
71 if (!bufev_private->read_suspended)
72 bufev->be_ops->disable(bufev, EV_READ);
73 bufev_private->read_suspended |= what;
78 bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
80 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
82 bufev_private->read_suspended &= ~what;
83 if (!bufev_private->read_suspended && (bufev->enabled & EV_READ))
84 bufev->be_ops->enable(bufev, EV_READ);
89 bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
91 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
93 if (!bufev_private->write_suspended)
94 bufev->be_ops->disable(bufev, EV_WRITE);
95 bufev_private->write_suspended |= what;
100 bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
102 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
104 bufev_private->write_suspended &= ~what;
105 if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE))
106 bufev->be_ops->enable(bufev, EV_WRITE);
111 * Sometimes bufferevent's implementation can overrun high watermarks
112 * (one of examples is openssl) and in this case if the read callback
113 * will not handle enough data do over condition above the read
114 * callback will never be called again (due to suspend above).
116 * To avoid this we are scheduling read callback again here, but only
117 * from the user callback to avoid multiple scheduling:
118 * - when the data had been added to it
119 * - when the data had been drained from it (user specified read callback)
121 static void bufferevent_inbuf_wm_check(struct bufferevent *bev)
123 if (!bev->wm_read.high)
125 if (!(bev->enabled & EV_READ))
127 if (evbuffer_get_length(bev->input) < bev->wm_read.high)
130 bufferevent_trigger(bev, EV_READ, BEV_OPT_DEFER_CALLBACKS);
133 /* Callback to implement watermarks on the input buffer. Only enabled
134 * if the watermark is set. */
136 bufferevent_inbuf_wm_cb(struct evbuffer *buf,
137 const struct evbuffer_cb_info *cbinfo,
140 struct bufferevent *bufev = arg;
143 size = evbuffer_get_length(buf);
145 if (size >= bufev->wm_read.high)
146 bufferevent_wm_suspend_read(bufev);
148 bufferevent_wm_unsuspend_read(bufev);
152 bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg)
154 struct bufferevent_private *bufev_private = arg;
155 struct bufferevent *bufev = &bufev_private->bev;
158 if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
160 /* The "connected" happened before any reads or writes, so
162 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
163 bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg);
165 if (bufev_private->readcb_pending && bufev->readcb) {
166 bufev_private->readcb_pending = 0;
167 bufev->readcb(bufev, bufev->cbarg);
168 bufferevent_inbuf_wm_check(bufev);
170 if (bufev_private->writecb_pending && bufev->writecb) {
171 bufev_private->writecb_pending = 0;
172 bufev->writecb(bufev, bufev->cbarg);
174 if (bufev_private->eventcb_pending && bufev->errorcb) {
175 short what = bufev_private->eventcb_pending;
176 int err = bufev_private->errno_pending;
177 bufev_private->eventcb_pending = 0;
178 bufev_private->errno_pending = 0;
179 EVUTIL_SET_SOCKET_ERROR(err);
180 bufev->errorcb(bufev, what, bufev->cbarg);
182 bufferevent_decref_and_unlock_(bufev);
186 bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg)
188 struct bufferevent_private *bufev_private = arg;
189 struct bufferevent *bufev = &bufev_private->bev;
192 #define UNLOCKED(stmt) \
193 do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0)
195 if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
197 /* The "connected" happened before any reads or writes, so
199 bufferevent_event_cb errorcb = bufev->errorcb;
200 void *cbarg = bufev->cbarg;
201 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
202 UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg));
204 if (bufev_private->readcb_pending && bufev->readcb) {
205 bufferevent_data_cb readcb = bufev->readcb;
206 void *cbarg = bufev->cbarg;
207 bufev_private->readcb_pending = 0;
208 UNLOCKED(readcb(bufev, cbarg));
209 bufferevent_inbuf_wm_check(bufev);
211 if (bufev_private->writecb_pending && bufev->writecb) {
212 bufferevent_data_cb writecb = bufev->writecb;
213 void *cbarg = bufev->cbarg;
214 bufev_private->writecb_pending = 0;
215 UNLOCKED(writecb(bufev, cbarg));
217 if (bufev_private->eventcb_pending && bufev->errorcb) {
218 bufferevent_event_cb errorcb = bufev->errorcb;
219 void *cbarg = bufev->cbarg;
220 short what = bufev_private->eventcb_pending;
221 int err = bufev_private->errno_pending;
222 bufev_private->eventcb_pending = 0;
223 bufev_private->errno_pending = 0;
224 EVUTIL_SET_SOCKET_ERROR(err);
225 UNLOCKED(errorcb(bufev,what,cbarg));
227 bufferevent_decref_and_unlock_(bufev);
231 #define SCHEDULE_DEFERRED(bevp) \
233 if (event_deferred_cb_schedule_( \
234 (bevp)->bev.ev_base, \
235 &(bevp)->deferred)) \
236 bufferevent_incref_(&(bevp)->bev); \
241 bufferevent_run_readcb_(struct bufferevent *bufev, int options)
243 /* Requires that we hold the lock and a reference */
244 struct bufferevent_private *p = BEV_UPCAST(bufev);
245 if (bufev->readcb == NULL)
247 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
248 p->readcb_pending = 1;
249 SCHEDULE_DEFERRED(p);
251 bufev->readcb(bufev, bufev->cbarg);
252 bufferevent_inbuf_wm_check(bufev);
257 bufferevent_run_writecb_(struct bufferevent *bufev, int options)
259 /* Requires that we hold the lock and a reference */
260 struct bufferevent_private *p = BEV_UPCAST(bufev);
261 if (bufev->writecb == NULL)
263 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
264 p->writecb_pending = 1;
265 SCHEDULE_DEFERRED(p);
267 bufev->writecb(bufev, bufev->cbarg);
271 #define BEV_TRIG_ALL_OPTS ( \
272 BEV_TRIG_IGNORE_WATERMARKS| \
273 BEV_TRIG_DEFER_CALLBACKS \
277 bufferevent_trigger(struct bufferevent *bufev, short iotype, int options)
279 bufferevent_incref_and_lock_(bufev);
280 bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS);
281 bufferevent_decref_and_unlock_(bufev);
285 bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options)
287 /* Requires that we hold the lock and a reference */
288 struct bufferevent_private *p = BEV_UPCAST(bufev);
289 if (bufev->errorcb == NULL)
291 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
292 p->eventcb_pending |= what;
293 p->errno_pending = EVUTIL_SOCKET_ERROR();
294 SCHEDULE_DEFERRED(p);
296 bufev->errorcb(bufev, what, bufev->cbarg);
301 bufferevent_trigger_event(struct bufferevent *bufev, short what, int options)
303 bufferevent_incref_and_lock_(bufev);
304 bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS);
305 bufferevent_decref_and_unlock_(bufev);
309 bufferevent_init_common_(struct bufferevent_private *bufev_private,
310 struct event_base *base,
311 const struct bufferevent_ops *ops,
312 enum bufferevent_options options)
314 struct bufferevent *bufev = &bufev_private->bev;
317 if ((bufev->input = evbuffer_new()) == NULL)
321 if (!bufev->output) {
322 if ((bufev->output = evbuffer_new()) == NULL)
326 bufev_private->refcnt = 1;
327 bufev->ev_base = base;
329 /* Disable timeouts. */
330 evutil_timerclear(&bufev->timeout_read);
331 evutil_timerclear(&bufev->timeout_write);
335 if (bufferevent_ratelim_init_(bufev_private))
339 * Set to EV_WRITE so that using bufferevent_write is going to
340 * trigger a callback. Reading needs to be explicitly enabled
341 * because otherwise no data will be available.
343 bufev->enabled = EV_WRITE;
345 #ifndef EVENT__DISABLE_THREAD_SUPPORT
346 if (options & BEV_OPT_THREADSAFE) {
347 if (bufferevent_enable_locking_(bufev, NULL) < 0)
351 if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS))
352 == BEV_OPT_UNLOCK_CALLBACKS) {
353 event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
356 if (options & BEV_OPT_UNLOCK_CALLBACKS)
357 event_deferred_cb_init_(
358 &bufev_private->deferred,
359 event_base_get_npriorities(base) / 2,
360 bufferevent_run_deferred_callbacks_unlocked,
363 event_deferred_cb_init_(
364 &bufev_private->deferred,
365 event_base_get_npriorities(base) / 2,
366 bufferevent_run_deferred_callbacks_locked,
369 bufev_private->options = options;
371 evbuffer_set_parent_(bufev->input, bufev);
372 evbuffer_set_parent_(bufev->output, bufev);
378 evbuffer_free(bufev->input);
382 evbuffer_free(bufev->output);
383 bufev->output = NULL;
389 bufferevent_setcb(struct bufferevent *bufev,
390 bufferevent_data_cb readcb, bufferevent_data_cb writecb,
391 bufferevent_event_cb eventcb, void *cbarg)
395 bufev->readcb = readcb;
396 bufev->writecb = writecb;
397 bufev->errorcb = eventcb;
399 bufev->cbarg = cbarg;
404 bufferevent_getcb(struct bufferevent *bufev,
405 bufferevent_data_cb *readcb_ptr,
406 bufferevent_data_cb *writecb_ptr,
407 bufferevent_event_cb *eventcb_ptr,
412 *readcb_ptr = bufev->readcb;
414 *writecb_ptr = bufev->writecb;
416 *eventcb_ptr = bufev->errorcb;
418 *cbarg_ptr = bufev->cbarg;
424 bufferevent_get_input(struct bufferevent *bufev)
430 bufferevent_get_output(struct bufferevent *bufev)
432 return bufev->output;
436 bufferevent_get_base(struct bufferevent *bufev)
438 return bufev->ev_base;
442 bufferevent_get_priority(const struct bufferevent *bufev)
444 if (event_initialized(&bufev->ev_read)) {
445 return event_get_priority(&bufev->ev_read);
447 return event_base_get_npriorities(bufev->ev_base) / 2;
452 bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
454 if (evbuffer_add(bufev->output, data, size) == -1)
461 bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
463 if (evbuffer_add_buffer(bufev->output, buf) == -1)
470 bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
472 int r = evbuffer_remove(bufev->input, data, size);
481 bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
483 return (evbuffer_add_buffer(buf, bufev->input));
487 bufferevent_enable(struct bufferevent *bufev, short event)
489 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
490 short impl_events = event;
493 bufferevent_incref_and_lock_(bufev);
494 if (bufev_private->read_suspended)
495 impl_events &= ~EV_READ;
496 if (bufev_private->write_suspended)
497 impl_events &= ~EV_WRITE;
499 bufev->enabled |= event;
501 if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
504 event_debug(("%s: cannot enable 0x%hx on %p", __func__, event, (void *)bufev));
506 bufferevent_decref_and_unlock_(bufev);
511 bufferevent_set_timeouts(struct bufferevent *bufev,
512 const struct timeval *tv_read,
513 const struct timeval *tv_write)
518 bufev->timeout_read = *tv_read;
520 evutil_timerclear(&bufev->timeout_read);
523 bufev->timeout_write = *tv_write;
525 evutil_timerclear(&bufev->timeout_write);
528 if (bufev->be_ops->adj_timeouts)
529 r = bufev->be_ops->adj_timeouts(bufev);
536 /* Obsolete; use bufferevent_set_timeouts */
538 bufferevent_settimeout(struct bufferevent *bufev,
539 int timeout_read, int timeout_write)
541 struct timeval tv_read, tv_write;
542 struct timeval *ptv_read = NULL, *ptv_write = NULL;
544 memset(&tv_read, 0, sizeof(tv_read));
545 memset(&tv_write, 0, sizeof(tv_write));
548 tv_read.tv_sec = timeout_read;
552 tv_write.tv_sec = timeout_write;
553 ptv_write = &tv_write;
556 bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
561 bufferevent_disable_hard_(struct bufferevent *bufev, short event)
564 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
567 bufev->enabled &= ~event;
569 bufev_private->connecting = 0;
570 if (bufev->be_ops->disable(bufev, event) < 0)
578 bufferevent_disable(struct bufferevent *bufev, short event)
583 bufev->enabled &= ~event;
585 if (bufev->be_ops->disable(bufev, event) < 0)
588 event_debug(("%s: cannot disable 0x%hx on %p", __func__, event, (void *)bufev));
595 * Sets the water marks
599 bufferevent_setwatermark(struct bufferevent *bufev, short events,
600 size_t lowmark, size_t highmark)
602 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
605 if (events & EV_WRITE) {
606 bufev->wm_write.low = lowmark;
607 bufev->wm_write.high = highmark;
610 if (events & EV_READ) {
611 bufev->wm_read.low = lowmark;
612 bufev->wm_read.high = highmark;
615 /* There is now a new high-water mark for read.
616 enable the callback if needed, and see if we should
617 suspend/bufferevent_wm_unsuspend. */
619 if (bufev_private->read_watermarks_cb == NULL) {
620 bufev_private->read_watermarks_cb =
621 evbuffer_add_cb(bufev->input,
622 bufferevent_inbuf_wm_cb,
625 evbuffer_cb_set_flags(bufev->input,
626 bufev_private->read_watermarks_cb,
627 EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
629 if (evbuffer_get_length(bufev->input) >= highmark)
630 bufferevent_wm_suspend_read(bufev);
631 else if (evbuffer_get_length(bufev->input) < highmark)
632 bufferevent_wm_unsuspend_read(bufev);
634 /* There is now no high-water mark for read. */
635 if (bufev_private->read_watermarks_cb)
636 evbuffer_cb_clear_flags(bufev->input,
637 bufev_private->read_watermarks_cb,
638 EVBUFFER_CB_ENABLED);
639 bufferevent_wm_unsuspend_read(bufev);
646 bufferevent_getwatermark(struct bufferevent *bufev, short events,
647 size_t *lowmark, size_t *highmark)
649 if (events == EV_WRITE) {
652 *lowmark = bufev->wm_write.low;
654 *highmark = bufev->wm_write.high;
659 if (events == EV_READ) {
662 *lowmark = bufev->wm_read.low;
664 *highmark = bufev->wm_read.high;
672 bufferevent_flush(struct bufferevent *bufev,
674 enum bufferevent_flush_mode mode)
678 if (bufev->be_ops->flush)
679 r = bufev->be_ops->flush(bufev, iotype, mode);
685 bufferevent_incref_and_lock_(struct bufferevent *bufev)
687 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
689 ++bufev_private->refcnt;
694 bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
695 struct bufferevent *recipient)
697 struct bufferevent_private *d = BEV_UPCAST(donor);
698 struct bufferevent_private *r = BEV_UPCAST(recipient);
699 if (d->lock != r->lock)
711 bufferevent_decref_and_unlock_(struct bufferevent *bufev)
713 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
716 struct event_callback *cbs[MAX_CBS];
718 EVUTIL_ASSERT(bufev_private->refcnt > 0);
720 if (--bufev_private->refcnt) {
725 if (bufev->be_ops->unlink)
726 bufev->be_ops->unlink(bufev);
728 /* Okay, we're out of references. Let's finalize this once all the
729 * callbacks are done running. */
730 cbs[0] = &bufev->ev_read.ev_evcallback;
731 cbs[1] = &bufev->ev_write.ev_evcallback;
732 cbs[2] = &bufev_private->deferred;
734 if (bufev_private->rate_limiting) {
735 struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
736 if (event_initialized(e))
737 cbs[n_cbs++] = &e->ev_evcallback;
739 n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
740 n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
742 event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
743 bufferevent_finalize_cb_);
752 bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
754 struct bufferevent *bufev = arg_;
755 struct bufferevent *underlying;
756 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
759 underlying = bufferevent_get_underlying(bufev);
761 /* Clean up the shared info */
762 if (bufev->be_ops->destruct)
763 bufev->be_ops->destruct(bufev);
765 /* XXX what happens if refcnt for these buffers is > 1?
766 * The buffers can share a lock with this bufferevent object,
767 * but the lock might be destroyed below. */
768 /* evbuffer will free the callbacks */
769 evbuffer_free(bufev->input);
770 evbuffer_free(bufev->output);
772 if (bufev_private->rate_limiting) {
773 if (bufev_private->rate_limiting->group)
774 bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
775 mm_free(bufev_private->rate_limiting);
776 bufev_private->rate_limiting = NULL;
782 if (bufev_private->own_lock)
783 EVTHREAD_FREE_LOCK(bufev_private->lock,
784 EVTHREAD_LOCKTYPE_RECURSIVE);
786 /* Free the actual allocated memory. */
787 mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
789 /* Release the reference to underlying now that we no longer need the
790 * reference to it. We wait this long mainly in case our lock is
791 * shared with underlying.
793 * The 'destruct' function will also drop a reference to underlying
794 * if BEV_OPT_CLOSE_ON_FREE is set.
796 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
797 * It would probably save us some headaches.
800 bufferevent_decref_(underlying);
804 bufferevent_decref(struct bufferevent *bufev)
807 return bufferevent_decref_and_unlock_(bufev);
811 bufferevent_free(struct bufferevent *bufev)
814 bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
815 bufferevent_cancel_all_(bufev);
816 bufferevent_decref_and_unlock_(bufev);
820 bufferevent_incref(struct bufferevent *bufev)
822 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
824 /* XXX: now that this function is public, we might want to
825 * - return the count from this function
826 * - create a new function to atomically grab the current refcount
829 ++bufev_private->refcnt;
834 bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
836 #ifdef EVENT__DISABLE_THREAD_SUPPORT
839 struct bufferevent *underlying;
841 if (BEV_UPCAST(bufev)->lock)
843 underlying = bufferevent_get_underlying(bufev);
845 if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
846 lock = BEV_UPCAST(underlying)->lock;
847 BEV_UPCAST(bufev)->lock = lock;
848 BEV_UPCAST(bufev)->own_lock = 0;
850 EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
853 BEV_UPCAST(bufev)->lock = lock;
854 BEV_UPCAST(bufev)->own_lock = 1;
856 BEV_UPCAST(bufev)->lock = lock;
857 BEV_UPCAST(bufev)->own_lock = 0;
859 evbuffer_enable_locking(bufev->input, lock);
860 evbuffer_enable_locking(bufev->output, lock);
862 if (underlying && !BEV_UPCAST(underlying)->lock)
863 bufferevent_enable_locking_(underlying, lock);
870 bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
872 union bufferevent_ctrl_data d;
876 if (bev->be_ops->ctrl)
877 res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
879 event_debug(("%s: cannot set fd for %p to "EV_SOCK_FMT, __func__, (void *)bev, fd));
885 bufferevent_replacefd(struct bufferevent *bev, evutil_socket_t fd)
887 union bufferevent_ctrl_data d;
889 evutil_socket_t old_fd = EVUTIL_INVALID_SOCKET;
892 if (bev->be_ops->ctrl) {
893 err = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
896 if (old_fd != EVUTIL_INVALID_SOCKET) {
897 err = evutil_closesocket(old_fd);
902 err = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
906 event_debug(("%s: cannot replace fd for %p from "EV_SOCK_FMT" to "EV_SOCK_FMT, __func__, (void *)bev, old_fd, fd));
913 bufferevent_getfd(struct bufferevent *bev)
915 union bufferevent_ctrl_data d;
919 if (bev->be_ops->ctrl)
920 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
922 event_debug(("%s: cannot get fd for %p", __func__, (void *)bev));
924 return (res<0) ? -1 : d.fd;
927 enum bufferevent_options
928 bufferevent_get_options_(struct bufferevent *bev)
930 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
931 enum bufferevent_options options;
934 options = bev_p->options;
941 bufferevent_cancel_all_(struct bufferevent *bev)
943 union bufferevent_ctrl_data d;
944 memset(&d, 0, sizeof(d));
946 if (bev->be_ops->ctrl)
947 bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
952 bufferevent_get_enabled(struct bufferevent *bufev)
962 bufferevent_get_underlying(struct bufferevent *bev)
964 union bufferevent_ctrl_data d;
968 if (bev->be_ops->ctrl)
969 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
971 return (res<0) ? NULL : d.ptr;
975 bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
977 struct bufferevent *bev = ctx;
978 bufferevent_incref_and_lock_(bev);
979 bufferevent_disable(bev, EV_READ);
980 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
981 bufferevent_decref_and_unlock_(bev);
984 bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
986 struct bufferevent *bev = ctx;
987 bufferevent_incref_and_lock_(bev);
988 bufferevent_disable(bev, EV_WRITE);
989 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
990 bufferevent_decref_and_unlock_(bev);
994 bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
996 event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
997 bufferevent_generic_read_timeout_cb, bev);
998 event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
999 bufferevent_generic_write_timeout_cb, bev);
1003 bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
1005 const short enabled = bev->enabled;
1006 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
1008 if ((enabled & EV_READ) && !bev_p->read_suspended &&
1009 evutil_timerisset(&bev->timeout_read))
1010 r1 = event_add(&bev->ev_read, &bev->timeout_read);
1012 r1 = event_del(&bev->ev_read);
1014 if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
1015 evutil_timerisset(&bev->timeout_write) &&
1016 evbuffer_get_length(bev->output))
1017 r2 = event_add(&bev->ev_write, &bev->timeout_write);
1019 r2 = event_del(&bev->ev_write);
1020 if (r1 < 0 || r2 < 0)
1026 bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
1029 if (event_pending(&bev->ev_read, EV_READ, NULL)) {
1030 if (evutil_timerisset(&bev->timeout_read)) {
1031 if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
1034 event_remove_timer(&bev->ev_read);
1037 if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
1038 if (evutil_timerisset(&bev->timeout_write)) {
1039 if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
1042 event_remove_timer(&bev->ev_write);
1049 bufferevent_add_event_(struct event *ev, const struct timeval *tv)
1051 if (!evutil_timerisset(tv))
1052 return event_add(ev, NULL);
1054 return event_add(ev, tv);
1057 /* For use by user programs only; internally, we should be calling
1058 either bufferevent_incref_and_lock_(), or BEV_LOCK. */
1060 bufferevent_lock(struct bufferevent *bev)
1062 bufferevent_incref_and_lock_(bev);
1066 bufferevent_unlock(struct bufferevent *bev)
1068 bufferevent_decref_and_unlock_(bev);