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 =
70 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
72 if (!bufev_private->read_suspended)
73 bufev->be_ops->disable(bufev, EV_READ);
74 bufev_private->read_suspended |= what;
79 bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
81 struct bufferevent_private *bufev_private =
82 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
84 bufev_private->read_suspended &= ~what;
85 if (!bufev_private->read_suspended && (bufev->enabled & EV_READ))
86 bufev->be_ops->enable(bufev, EV_READ);
91 bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
93 struct bufferevent_private *bufev_private =
94 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
96 if (!bufev_private->write_suspended)
97 bufev->be_ops->disable(bufev, EV_WRITE);
98 bufev_private->write_suspended |= what;
103 bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
105 struct bufferevent_private *bufev_private =
106 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
108 bufev_private->write_suspended &= ~what;
109 if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE))
110 bufev->be_ops->enable(bufev, EV_WRITE);
115 /* Callback to implement watermarks on the input buffer. Only enabled
116 * if the watermark is set. */
118 bufferevent_inbuf_wm_cb(struct evbuffer *buf,
119 const struct evbuffer_cb_info *cbinfo,
122 struct bufferevent *bufev = arg;
125 size = evbuffer_get_length(buf);
127 if (size >= bufev->wm_read.high)
128 bufferevent_wm_suspend_read(bufev);
130 bufferevent_wm_unsuspend_read(bufev);
134 bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg)
136 struct bufferevent_private *bufev_private = arg;
137 struct bufferevent *bufev = &bufev_private->bev;
140 if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
142 /* The "connected" happened before any reads or writes, so
144 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
145 bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg);
147 if (bufev_private->readcb_pending && bufev->readcb) {
148 bufev_private->readcb_pending = 0;
149 bufev->readcb(bufev, bufev->cbarg);
151 if (bufev_private->writecb_pending && bufev->writecb) {
152 bufev_private->writecb_pending = 0;
153 bufev->writecb(bufev, bufev->cbarg);
155 if (bufev_private->eventcb_pending && bufev->errorcb) {
156 short what = bufev_private->eventcb_pending;
157 int err = bufev_private->errno_pending;
158 bufev_private->eventcb_pending = 0;
159 bufev_private->errno_pending = 0;
160 EVUTIL_SET_SOCKET_ERROR(err);
161 bufev->errorcb(bufev, what, bufev->cbarg);
163 bufferevent_decref_and_unlock_(bufev);
167 bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg)
169 struct bufferevent_private *bufev_private = arg;
170 struct bufferevent *bufev = &bufev_private->bev;
173 #define UNLOCKED(stmt) \
174 do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0)
176 if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
178 /* The "connected" happened before any reads or writes, so
180 bufferevent_event_cb errorcb = bufev->errorcb;
181 void *cbarg = bufev->cbarg;
182 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
183 UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg));
185 if (bufev_private->readcb_pending && bufev->readcb) {
186 bufferevent_data_cb readcb = bufev->readcb;
187 void *cbarg = bufev->cbarg;
188 bufev_private->readcb_pending = 0;
189 UNLOCKED(readcb(bufev, cbarg));
191 if (bufev_private->writecb_pending && bufev->writecb) {
192 bufferevent_data_cb writecb = bufev->writecb;
193 void *cbarg = bufev->cbarg;
194 bufev_private->writecb_pending = 0;
195 UNLOCKED(writecb(bufev, cbarg));
197 if (bufev_private->eventcb_pending && bufev->errorcb) {
198 bufferevent_event_cb errorcb = bufev->errorcb;
199 void *cbarg = bufev->cbarg;
200 short what = bufev_private->eventcb_pending;
201 int err = bufev_private->errno_pending;
202 bufev_private->eventcb_pending = 0;
203 bufev_private->errno_pending = 0;
204 EVUTIL_SET_SOCKET_ERROR(err);
205 UNLOCKED(errorcb(bufev,what,cbarg));
207 bufferevent_decref_and_unlock_(bufev);
211 #define SCHEDULE_DEFERRED(bevp) \
213 if (event_deferred_cb_schedule_( \
214 (bevp)->bev.ev_base, \
215 &(bevp)->deferred)) \
216 bufferevent_incref_(&(bevp)->bev); \
221 bufferevent_run_readcb_(struct bufferevent *bufev, int options)
223 /* Requires that we hold the lock and a reference */
224 struct bufferevent_private *p =
225 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
226 if (bufev->readcb == NULL)
228 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
229 p->readcb_pending = 1;
230 SCHEDULE_DEFERRED(p);
232 bufev->readcb(bufev, bufev->cbarg);
237 bufferevent_run_writecb_(struct bufferevent *bufev, int options)
239 /* Requires that we hold the lock and a reference */
240 struct bufferevent_private *p =
241 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
242 if (bufev->writecb == NULL)
244 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
245 p->writecb_pending = 1;
246 SCHEDULE_DEFERRED(p);
248 bufev->writecb(bufev, bufev->cbarg);
252 #define BEV_TRIG_ALL_OPTS ( \
253 BEV_TRIG_IGNORE_WATERMARKS| \
254 BEV_TRIG_DEFER_CALLBACKS \
258 bufferevent_trigger(struct bufferevent *bufev, short iotype, int options)
260 bufferevent_incref_and_lock_(bufev);
261 bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS);
262 bufferevent_decref_and_unlock_(bufev);
266 bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options)
268 /* Requires that we hold the lock and a reference */
269 struct bufferevent_private *p =
270 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
271 if (bufev->errorcb == NULL)
273 if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
274 p->eventcb_pending |= what;
275 p->errno_pending = EVUTIL_SOCKET_ERROR();
276 SCHEDULE_DEFERRED(p);
278 bufev->errorcb(bufev, what, bufev->cbarg);
283 bufferevent_trigger_event(struct bufferevent *bufev, short what, int options)
285 bufferevent_incref_and_lock_(bufev);
286 bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS);
287 bufferevent_decref_and_unlock_(bufev);
291 bufferevent_init_common_(struct bufferevent_private *bufev_private,
292 struct event_base *base,
293 const struct bufferevent_ops *ops,
294 enum bufferevent_options options)
296 struct bufferevent *bufev = &bufev_private->bev;
299 if ((bufev->input = evbuffer_new()) == NULL)
303 if (!bufev->output) {
304 if ((bufev->output = evbuffer_new()) == NULL) {
305 evbuffer_free(bufev->input);
310 bufev_private->refcnt = 1;
311 bufev->ev_base = base;
313 /* Disable timeouts. */
314 evutil_timerclear(&bufev->timeout_read);
315 evutil_timerclear(&bufev->timeout_write);
319 bufferevent_ratelim_init_(bufev_private);
322 * Set to EV_WRITE so that using bufferevent_write is going to
323 * trigger a callback. Reading needs to be explicitly enabled
324 * because otherwise no data will be available.
326 bufev->enabled = EV_WRITE;
328 #ifndef EVENT__DISABLE_THREAD_SUPPORT
329 if (options & BEV_OPT_THREADSAFE) {
330 if (bufferevent_enable_locking_(bufev, NULL) < 0) {
332 evbuffer_free(bufev->input);
333 evbuffer_free(bufev->output);
335 bufev->output = NULL;
340 if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS))
341 == BEV_OPT_UNLOCK_CALLBACKS) {
342 event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
345 if (options & BEV_OPT_UNLOCK_CALLBACKS)
346 event_deferred_cb_init_(
347 &bufev_private->deferred,
348 event_base_get_npriorities(base) / 2,
349 bufferevent_run_deferred_callbacks_unlocked,
352 event_deferred_cb_init_(
353 &bufev_private->deferred,
354 event_base_get_npriorities(base) / 2,
355 bufferevent_run_deferred_callbacks_locked,
358 bufev_private->options = options;
360 evbuffer_set_parent_(bufev->input, bufev);
361 evbuffer_set_parent_(bufev->output, bufev);
367 bufferevent_setcb(struct bufferevent *bufev,
368 bufferevent_data_cb readcb, bufferevent_data_cb writecb,
369 bufferevent_event_cb eventcb, void *cbarg)
373 bufev->readcb = readcb;
374 bufev->writecb = writecb;
375 bufev->errorcb = eventcb;
377 bufev->cbarg = cbarg;
382 bufferevent_getcb(struct bufferevent *bufev,
383 bufferevent_data_cb *readcb_ptr,
384 bufferevent_data_cb *writecb_ptr,
385 bufferevent_event_cb *eventcb_ptr,
390 *readcb_ptr = bufev->readcb;
392 *writecb_ptr = bufev->writecb;
394 *eventcb_ptr = bufev->errorcb;
396 *cbarg_ptr = bufev->cbarg;
402 bufferevent_get_input(struct bufferevent *bufev)
408 bufferevent_get_output(struct bufferevent *bufev)
410 return bufev->output;
414 bufferevent_get_base(struct bufferevent *bufev)
416 return bufev->ev_base;
420 bufferevent_get_priority(const struct bufferevent *bufev)
422 if (event_initialized(&bufev->ev_read)) {
423 return event_get_priority(&bufev->ev_read);
425 return event_base_get_npriorities(bufev->ev_base) / 2;
430 bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
432 if (evbuffer_add(bufev->output, data, size) == -1)
439 bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
441 if (evbuffer_add_buffer(bufev->output, buf) == -1)
448 bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
450 return (evbuffer_remove(bufev->input, data, size));
454 bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
456 return (evbuffer_add_buffer(buf, bufev->input));
460 bufferevent_enable(struct bufferevent *bufev, short event)
462 struct bufferevent_private *bufev_private =
463 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
464 short impl_events = event;
467 bufferevent_incref_and_lock_(bufev);
468 if (bufev_private->read_suspended)
469 impl_events &= ~EV_READ;
470 if (bufev_private->write_suspended)
471 impl_events &= ~EV_WRITE;
473 bufev->enabled |= event;
475 if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
478 bufferevent_decref_and_unlock_(bufev);
483 bufferevent_set_timeouts(struct bufferevent *bufev,
484 const struct timeval *tv_read,
485 const struct timeval *tv_write)
490 bufev->timeout_read = *tv_read;
492 evutil_timerclear(&bufev->timeout_read);
495 bufev->timeout_write = *tv_write;
497 evutil_timerclear(&bufev->timeout_write);
500 if (bufev->be_ops->adj_timeouts)
501 r = bufev->be_ops->adj_timeouts(bufev);
508 /* Obsolete; use bufferevent_set_timeouts */
510 bufferevent_settimeout(struct bufferevent *bufev,
511 int timeout_read, int timeout_write)
513 struct timeval tv_read, tv_write;
514 struct timeval *ptv_read = NULL, *ptv_write = NULL;
516 memset(&tv_read, 0, sizeof(tv_read));
517 memset(&tv_write, 0, sizeof(tv_write));
520 tv_read.tv_sec = timeout_read;
524 tv_write.tv_sec = timeout_write;
525 ptv_write = &tv_write;
528 bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
533 bufferevent_disable_hard_(struct bufferevent *bufev, short event)
536 struct bufferevent_private *bufev_private =
537 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
540 bufev->enabled &= ~event;
542 bufev_private->connecting = 0;
543 if (bufev->be_ops->disable(bufev, event) < 0)
551 bufferevent_disable(struct bufferevent *bufev, short event)
556 bufev->enabled &= ~event;
558 if (bufev->be_ops->disable(bufev, event) < 0)
566 * Sets the water marks
570 bufferevent_setwatermark(struct bufferevent *bufev, short events,
571 size_t lowmark, size_t highmark)
573 struct bufferevent_private *bufev_private =
574 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
577 if (events & EV_WRITE) {
578 bufev->wm_write.low = lowmark;
579 bufev->wm_write.high = highmark;
582 if (events & EV_READ) {
583 bufev->wm_read.low = lowmark;
584 bufev->wm_read.high = highmark;
587 /* There is now a new high-water mark for read.
588 enable the callback if needed, and see if we should
589 suspend/bufferevent_wm_unsuspend. */
591 if (bufev_private->read_watermarks_cb == NULL) {
592 bufev_private->read_watermarks_cb =
593 evbuffer_add_cb(bufev->input,
594 bufferevent_inbuf_wm_cb,
597 evbuffer_cb_set_flags(bufev->input,
598 bufev_private->read_watermarks_cb,
599 EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
601 if (evbuffer_get_length(bufev->input) >= highmark)
602 bufferevent_wm_suspend_read(bufev);
603 else if (evbuffer_get_length(bufev->input) < highmark)
604 bufferevent_wm_unsuspend_read(bufev);
606 /* There is now no high-water mark for read. */
607 if (bufev_private->read_watermarks_cb)
608 evbuffer_cb_clear_flags(bufev->input,
609 bufev_private->read_watermarks_cb,
610 EVBUFFER_CB_ENABLED);
611 bufferevent_wm_unsuspend_read(bufev);
618 bufferevent_getwatermark(struct bufferevent *bufev, short events,
619 size_t *lowmark, size_t *highmark)
621 if (events == EV_WRITE) {
624 *lowmark = bufev->wm_write.low;
626 *highmark = bufev->wm_write.high;
631 if (events == EV_READ) {
634 *lowmark = bufev->wm_read.low;
636 *highmark = bufev->wm_read.high;
644 bufferevent_flush(struct bufferevent *bufev,
646 enum bufferevent_flush_mode mode)
650 if (bufev->be_ops->flush)
651 r = bufev->be_ops->flush(bufev, iotype, mode);
657 bufferevent_incref_and_lock_(struct bufferevent *bufev)
659 struct bufferevent_private *bufev_private =
662 ++bufev_private->refcnt;
667 bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
668 struct bufferevent *recipient)
670 struct bufferevent_private *d = BEV_UPCAST(donor);
671 struct bufferevent_private *r = BEV_UPCAST(recipient);
672 if (d->lock != r->lock)
684 bufferevent_decref_and_unlock_(struct bufferevent *bufev)
686 struct bufferevent_private *bufev_private =
687 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
690 struct event_callback *cbs[MAX_CBS];
692 EVUTIL_ASSERT(bufev_private->refcnt > 0);
694 if (--bufev_private->refcnt) {
699 if (bufev->be_ops->unlink)
700 bufev->be_ops->unlink(bufev);
702 /* Okay, we're out of references. Let's finalize this once all the
703 * callbacks are done running. */
704 cbs[0] = &bufev->ev_read.ev_evcallback;
705 cbs[1] = &bufev->ev_write.ev_evcallback;
706 cbs[2] = &bufev_private->deferred;
708 if (bufev_private->rate_limiting) {
709 struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
710 if (event_initialized(e))
711 cbs[n_cbs++] = &e->ev_evcallback;
713 n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
714 n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
716 event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
717 bufferevent_finalize_cb_);
726 bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
728 struct bufferevent *bufev = arg_;
729 struct bufferevent *underlying;
730 struct bufferevent_private *bufev_private =
731 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
734 underlying = bufferevent_get_underlying(bufev);
736 /* Clean up the shared info */
737 if (bufev->be_ops->destruct)
738 bufev->be_ops->destruct(bufev);
740 /* XXX what happens if refcnt for these buffers is > 1?
741 * The buffers can share a lock with this bufferevent object,
742 * but the lock might be destroyed below. */
743 /* evbuffer will free the callbacks */
744 evbuffer_free(bufev->input);
745 evbuffer_free(bufev->output);
747 if (bufev_private->rate_limiting) {
748 if (bufev_private->rate_limiting->group)
749 bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
750 mm_free(bufev_private->rate_limiting);
751 bufev_private->rate_limiting = NULL;
757 if (bufev_private->own_lock)
758 EVTHREAD_FREE_LOCK(bufev_private->lock,
759 EVTHREAD_LOCKTYPE_RECURSIVE);
761 /* Free the actual allocated memory. */
762 mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
764 /* Release the reference to underlying now that we no longer need the
765 * reference to it. We wait this long mainly in case our lock is
766 * shared with underlying.
768 * The 'destruct' function will also drop a reference to underlying
769 * if BEV_OPT_CLOSE_ON_FREE is set.
771 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
772 * It would probably save us some headaches.
775 bufferevent_decref_(underlying);
779 bufferevent_decref(struct bufferevent *bufev)
782 return bufferevent_decref_and_unlock_(bufev);
786 bufferevent_free(struct bufferevent *bufev)
789 bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
790 bufferevent_cancel_all_(bufev);
791 bufferevent_decref_and_unlock_(bufev);
795 bufferevent_incref(struct bufferevent *bufev)
797 struct bufferevent_private *bufev_private =
798 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
800 /* XXX: now that this function is public, we might want to
801 * - return the count from this function
802 * - create a new function to atomically grab the current refcount
805 ++bufev_private->refcnt;
810 bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
812 #ifdef EVENT__DISABLE_THREAD_SUPPORT
815 struct bufferevent *underlying;
817 if (BEV_UPCAST(bufev)->lock)
819 underlying = bufferevent_get_underlying(bufev);
821 if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
822 lock = BEV_UPCAST(underlying)->lock;
823 BEV_UPCAST(bufev)->lock = lock;
824 BEV_UPCAST(bufev)->own_lock = 0;
826 EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
829 BEV_UPCAST(bufev)->lock = lock;
830 BEV_UPCAST(bufev)->own_lock = 1;
832 BEV_UPCAST(bufev)->lock = lock;
833 BEV_UPCAST(bufev)->own_lock = 0;
835 evbuffer_enable_locking(bufev->input, lock);
836 evbuffer_enable_locking(bufev->output, lock);
838 if (underlying && !BEV_UPCAST(underlying)->lock)
839 bufferevent_enable_locking_(underlying, lock);
846 bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
848 union bufferevent_ctrl_data d;
852 if (bev->be_ops->ctrl)
853 res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
859 bufferevent_getfd(struct bufferevent *bev)
861 union bufferevent_ctrl_data d;
865 if (bev->be_ops->ctrl)
866 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
868 return (res<0) ? -1 : d.fd;
871 enum bufferevent_options
872 bufferevent_get_options_(struct bufferevent *bev)
874 struct bufferevent_private *bev_p =
875 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
876 enum bufferevent_options options;
879 options = bev_p->options;
886 bufferevent_cancel_all_(struct bufferevent *bev)
888 union bufferevent_ctrl_data d;
889 memset(&d, 0, sizeof(d));
891 if (bev->be_ops->ctrl)
892 bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
897 bufferevent_get_enabled(struct bufferevent *bufev)
907 bufferevent_get_underlying(struct bufferevent *bev)
909 union bufferevent_ctrl_data d;
913 if (bev->be_ops->ctrl)
914 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
916 return (res<0) ? NULL : d.ptr;
920 bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
922 struct bufferevent *bev = ctx;
923 bufferevent_incref_and_lock_(bev);
924 bufferevent_disable(bev, EV_READ);
925 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
926 bufferevent_decref_and_unlock_(bev);
929 bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
931 struct bufferevent *bev = ctx;
932 bufferevent_incref_and_lock_(bev);
933 bufferevent_disable(bev, EV_WRITE);
934 bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
935 bufferevent_decref_and_unlock_(bev);
939 bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
941 event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
942 bufferevent_generic_read_timeout_cb, bev);
943 event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
944 bufferevent_generic_write_timeout_cb, bev);
948 bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
950 const short enabled = bev->enabled;
951 struct bufferevent_private *bev_p =
952 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
954 if ((enabled & EV_READ) && !bev_p->read_suspended &&
955 evutil_timerisset(&bev->timeout_read))
956 r1 = event_add(&bev->ev_read, &bev->timeout_read);
958 r1 = event_del(&bev->ev_read);
960 if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
961 evutil_timerisset(&bev->timeout_write) &&
962 evbuffer_get_length(bev->output))
963 r2 = event_add(&bev->ev_write, &bev->timeout_write);
965 r2 = event_del(&bev->ev_write);
966 if (r1 < 0 || r2 < 0)
972 bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
975 if (event_pending(&bev->ev_read, EV_READ, NULL)) {
976 if (evutil_timerisset(&bev->timeout_read)) {
977 if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
980 event_remove_timer(&bev->ev_read);
983 if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
984 if (evutil_timerisset(&bev->timeout_write)) {
985 if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
988 event_remove_timer(&bev->ev_write);
995 bufferevent_add_event_(struct event *ev, const struct timeval *tv)
997 if (!evutil_timerisset(tv))
998 return event_add(ev, NULL);
1000 return event_add(ev, tv);
1003 /* For use by user programs only; internally, we should be calling
1004 either bufferevent_incref_and_lock_(), or BEV_LOCK. */
1006 bufferevent_lock(struct bufferevent *bev)
1008 bufferevent_incref_and_lock_(bev);
1012 bufferevent_unlock(struct bufferevent *bev)
1014 bufferevent_decref_and_unlock_(bev);