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 return (evbuffer_remove(bufev->input, data, size));
476 bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
478 return (evbuffer_add_buffer(buf, bufev->input));
482 bufferevent_enable(struct bufferevent *bufev, short event)
484 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
485 short impl_events = event;
488 bufferevent_incref_and_lock_(bufev);
489 if (bufev_private->read_suspended)
490 impl_events &= ~EV_READ;
491 if (bufev_private->write_suspended)
492 impl_events &= ~EV_WRITE;
494 bufev->enabled |= event;
496 if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
499 event_debug(("%s: cannot enable 0x%hx on %p", __func__, event, bufev));
501 bufferevent_decref_and_unlock_(bufev);
506 bufferevent_set_timeouts(struct bufferevent *bufev,
507 const struct timeval *tv_read,
508 const struct timeval *tv_write)
513 bufev->timeout_read = *tv_read;
515 evutil_timerclear(&bufev->timeout_read);
518 bufev->timeout_write = *tv_write;
520 evutil_timerclear(&bufev->timeout_write);
523 if (bufev->be_ops->adj_timeouts)
524 r = bufev->be_ops->adj_timeouts(bufev);
531 /* Obsolete; use bufferevent_set_timeouts */
533 bufferevent_settimeout(struct bufferevent *bufev,
534 int timeout_read, int timeout_write)
536 struct timeval tv_read, tv_write;
537 struct timeval *ptv_read = NULL, *ptv_write = NULL;
539 memset(&tv_read, 0, sizeof(tv_read));
540 memset(&tv_write, 0, sizeof(tv_write));
543 tv_read.tv_sec = timeout_read;
547 tv_write.tv_sec = timeout_write;
548 ptv_write = &tv_write;
551 bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
556 bufferevent_disable_hard_(struct bufferevent *bufev, short event)
559 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
562 bufev->enabled &= ~event;
564 bufev_private->connecting = 0;
565 if (bufev->be_ops->disable(bufev, event) < 0)
573 bufferevent_disable(struct bufferevent *bufev, short event)
578 bufev->enabled &= ~event;
580 if (bufev->be_ops->disable(bufev, event) < 0)
583 event_debug(("%s: cannot disable 0x%hx on %p", __func__, event, bufev));
590 * Sets the water marks
594 bufferevent_setwatermark(struct bufferevent *bufev, short events,
595 size_t lowmark, size_t highmark)
597 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
600 if (events & EV_WRITE) {
601 bufev->wm_write.low = lowmark;
602 bufev->wm_write.high = highmark;
605 if (events & EV_READ) {
606 bufev->wm_read.low = lowmark;
607 bufev->wm_read.high = highmark;
610 /* There is now a new high-water mark for read.
611 enable the callback if needed, and see if we should
612 suspend/bufferevent_wm_unsuspend. */
614 if (bufev_private->read_watermarks_cb == NULL) {
615 bufev_private->read_watermarks_cb =
616 evbuffer_add_cb(bufev->input,
617 bufferevent_inbuf_wm_cb,
620 evbuffer_cb_set_flags(bufev->input,
621 bufev_private->read_watermarks_cb,
622 EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
624 if (evbuffer_get_length(bufev->input) >= highmark)
625 bufferevent_wm_suspend_read(bufev);
626 else if (evbuffer_get_length(bufev->input) < highmark)
627 bufferevent_wm_unsuspend_read(bufev);
629 /* There is now no high-water mark for read. */
630 if (bufev_private->read_watermarks_cb)
631 evbuffer_cb_clear_flags(bufev->input,
632 bufev_private->read_watermarks_cb,
633 EVBUFFER_CB_ENABLED);
634 bufferevent_wm_unsuspend_read(bufev);
641 bufferevent_getwatermark(struct bufferevent *bufev, short events,
642 size_t *lowmark, size_t *highmark)
644 if (events == EV_WRITE) {
647 *lowmark = bufev->wm_write.low;
649 *highmark = bufev->wm_write.high;
654 if (events == EV_READ) {
657 *lowmark = bufev->wm_read.low;
659 *highmark = bufev->wm_read.high;
667 bufferevent_flush(struct bufferevent *bufev,
669 enum bufferevent_flush_mode mode)
673 if (bufev->be_ops->flush)
674 r = bufev->be_ops->flush(bufev, iotype, mode);
680 bufferevent_incref_and_lock_(struct bufferevent *bufev)
682 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
684 ++bufev_private->refcnt;
689 bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
690 struct bufferevent *recipient)
692 struct bufferevent_private *d = BEV_UPCAST(donor);
693 struct bufferevent_private *r = BEV_UPCAST(recipient);
694 if (d->lock != r->lock)
706 bufferevent_decref_and_unlock_(struct bufferevent *bufev)
708 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
711 struct event_callback *cbs[MAX_CBS];
713 EVUTIL_ASSERT(bufev_private->refcnt > 0);
715 if (--bufev_private->refcnt) {
720 if (bufev->be_ops->unlink)
721 bufev->be_ops->unlink(bufev);
723 /* Okay, we're out of references. Let's finalize this once all the
724 * callbacks are done running. */
725 cbs[0] = &bufev->ev_read.ev_evcallback;
726 cbs[1] = &bufev->ev_write.ev_evcallback;
727 cbs[2] = &bufev_private->deferred;
729 if (bufev_private->rate_limiting) {
730 struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
731 if (event_initialized(e))
732 cbs[n_cbs++] = &e->ev_evcallback;
734 n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
735 n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
737 event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
738 bufferevent_finalize_cb_);
747 bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
749 struct bufferevent *bufev = arg_;
750 struct bufferevent *underlying;
751 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
754 underlying = bufferevent_get_underlying(bufev);
756 /* Clean up the shared info */
757 if (bufev->be_ops->destruct)
758 bufev->be_ops->destruct(bufev);
760 /* XXX what happens if refcnt for these buffers is > 1?
761 * The buffers can share a lock with this bufferevent object,
762 * but the lock might be destroyed below. */
763 /* evbuffer will free the callbacks */
764 evbuffer_free(bufev->input);
765 evbuffer_free(bufev->output);
767 if (bufev_private->rate_limiting) {
768 if (bufev_private->rate_limiting->group)
769 bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
770 mm_free(bufev_private->rate_limiting);
771 bufev_private->rate_limiting = NULL;
777 if (bufev_private->own_lock)
778 EVTHREAD_FREE_LOCK(bufev_private->lock,
779 EVTHREAD_LOCKTYPE_RECURSIVE);
781 /* Free the actual allocated memory. */
782 mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
784 /* Release the reference to underlying now that we no longer need the
785 * reference to it. We wait this long mainly in case our lock is
786 * shared with underlying.
788 * The 'destruct' function will also drop a reference to underlying
789 * if BEV_OPT_CLOSE_ON_FREE is set.
791 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
792 * It would probably save us some headaches.
795 bufferevent_decref_(underlying);
799 bufferevent_decref(struct bufferevent *bufev)
802 return bufferevent_decref_and_unlock_(bufev);
806 bufferevent_free(struct bufferevent *bufev)
809 bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
810 bufferevent_cancel_all_(bufev);
811 bufferevent_decref_and_unlock_(bufev);
815 bufferevent_incref(struct bufferevent *bufev)
817 struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
819 /* XXX: now that this function is public, we might want to
820 * - return the count from this function
821 * - create a new function to atomically grab the current refcount
824 ++bufev_private->refcnt;
829 bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
831 #ifdef EVENT__DISABLE_THREAD_SUPPORT
834 struct bufferevent *underlying;
836 if (BEV_UPCAST(bufev)->lock)
838 underlying = bufferevent_get_underlying(bufev);
840 if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
841 lock = BEV_UPCAST(underlying)->lock;
842 BEV_UPCAST(bufev)->lock = lock;
843 BEV_UPCAST(bufev)->own_lock = 0;
845 EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
848 BEV_UPCAST(bufev)->lock = lock;
849 BEV_UPCAST(bufev)->own_lock = 1;
851 BEV_UPCAST(bufev)->lock = lock;
852 BEV_UPCAST(bufev)->own_lock = 0;
854 evbuffer_enable_locking(bufev->input, lock);
855 evbuffer_enable_locking(bufev->output, lock);
857 if (underlying && !BEV_UPCAST(underlying)->lock)
858 bufferevent_enable_locking_(underlying, lock);
865 bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
867 union bufferevent_ctrl_data d;
871 if (bev->be_ops->ctrl)
872 res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
874 event_debug(("%s: cannot set fd for %p to "EV_SOCK_FMT, __func__, bev, fd));
880 bufferevent_getfd(struct bufferevent *bev)
882 union bufferevent_ctrl_data d;
886 if (bev->be_ops->ctrl)
887 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
889 event_debug(("%s: cannot get fd for %p", __func__, bev));
891 return (res<0) ? -1 : d.fd;
894 enum bufferevent_options
895 bufferevent_get_options_(struct bufferevent *bev)
897 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
898 enum bufferevent_options options;
901 options = bev_p->options;
908 bufferevent_cancel_all_(struct bufferevent *bev)
910 union bufferevent_ctrl_data d;
911 memset(&d, 0, sizeof(d));
913 if (bev->be_ops->ctrl)
914 bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
919 bufferevent_get_enabled(struct bufferevent *bufev)
929 bufferevent_get_underlying(struct bufferevent *bev)
931 union bufferevent_ctrl_data d;
935 if (bev->be_ops->ctrl)
936 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
938 return (res<0) ? NULL : d.ptr;
942 bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
944 struct bufferevent *bev = ctx;
945 bufferevent_incref_and_lock_(bev);
946 bufferevent_disable(bev, EV_READ);
947 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
948 bufferevent_decref_and_unlock_(bev);
951 bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
953 struct bufferevent *bev = ctx;
954 bufferevent_incref_and_lock_(bev);
955 bufferevent_disable(bev, EV_WRITE);
956 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
957 bufferevent_decref_and_unlock_(bev);
961 bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
963 event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
964 bufferevent_generic_read_timeout_cb, bev);
965 event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
966 bufferevent_generic_write_timeout_cb, bev);
970 bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
972 const short enabled = bev->enabled;
973 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
975 if ((enabled & EV_READ) && !bev_p->read_suspended &&
976 evutil_timerisset(&bev->timeout_read))
977 r1 = event_add(&bev->ev_read, &bev->timeout_read);
979 r1 = event_del(&bev->ev_read);
981 if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
982 evutil_timerisset(&bev->timeout_write) &&
983 evbuffer_get_length(bev->output))
984 r2 = event_add(&bev->ev_write, &bev->timeout_write);
986 r2 = event_del(&bev->ev_write);
987 if (r1 < 0 || r2 < 0)
993 bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
996 if (event_pending(&bev->ev_read, EV_READ, NULL)) {
997 if (evutil_timerisset(&bev->timeout_read)) {
998 if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
1001 event_remove_timer(&bev->ev_read);
1004 if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
1005 if (evutil_timerisset(&bev->timeout_write)) {
1006 if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
1009 event_remove_timer(&bev->ev_write);
1016 bufferevent_add_event_(struct event *ev, const struct timeval *tv)
1018 if (!evutil_timerisset(tv))
1019 return event_add(ev, NULL);
1021 return event_add(ev, tv);
1024 /* For use by user programs only; internally, we should be calling
1025 either bufferevent_incref_and_lock_(), or BEV_LOCK. */
1027 bufferevent_lock(struct bufferevent *bev)
1029 bufferevent_incref_and_lock_(bev);
1033 bufferevent_unlock(struct bufferevent *bev)
1035 bufferevent_decref_and_unlock_(bev);