2 83f0f95a 2022-09-29 op * Copyright (c) 2002-2004 Niels Provos <provos@citi.umich.edu>
3 83f0f95a 2022-09-29 op * All rights reserved.
5 83f0f95a 2022-09-29 op * Redistribution and use in source and binary forms, with or without
6 83f0f95a 2022-09-29 op * modification, are permitted provided that the following conditions
8 83f0f95a 2022-09-29 op * 1. Redistributions of source code must retain the above copyright
9 83f0f95a 2022-09-29 op * notice, this list of conditions and the following disclaimer.
10 83f0f95a 2022-09-29 op * 2. Redistributions in binary form must reproduce the above copyright
11 83f0f95a 2022-09-29 op * notice, this list of conditions and the following disclaimer in the
12 83f0f95a 2022-09-29 op * documentation and/or other materials provided with the distribution.
13 83f0f95a 2022-09-29 op * 3. The name of the author may not be used to endorse or promote products
14 83f0f95a 2022-09-29 op * derived from this software without specific prior written permission.
16 83f0f95a 2022-09-29 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 83f0f95a 2022-09-29 op * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 83f0f95a 2022-09-29 op * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 83f0f95a 2022-09-29 op * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 83f0f95a 2022-09-29 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 83f0f95a 2022-09-29 op * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 83f0f95a 2022-09-29 op * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 83f0f95a 2022-09-29 op * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 83f0f95a 2022-09-29 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 83f0f95a 2022-09-29 op * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 83f0f95a 2022-09-29 op #include <sys/time.h>
29 83f0f95a 2022-09-29 op #include <event.h>
30 83f0f95a 2022-09-29 op #include "config.h"
32 83f0f95a 2022-09-29 op void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t,
36 83f0f95a 2022-09-29 op bufferevent_add(struct event *ev, int timeout)
38 83f0f95a 2022-09-29 op struct timeval tv, *ptv = NULL;
40 83f0f95a 2022-09-29 op if (timeout) {
41 83f0f95a 2022-09-29 op timerclear(&tv);
42 83f0f95a 2022-09-29 op tv.tv_sec = timeout;
46 83f0f95a 2022-09-29 op return (event_add(ev, ptv));
50 83f0f95a 2022-09-29 op * This callback is executed when the size of the input buffer changes.
51 83f0f95a 2022-09-29 op * We use it to apply back pressure on the reading side.
54 83f0f95a 2022-09-29 op bufferevent_read_pressure_cb(struct evbuffer *buf, size_t old, size_t now,
56 83f0f95a 2022-09-29 op struct bufferevent *bufev = arg;
58 83f0f95a 2022-09-29 op * If we are below the watermark then reschedule reading if it's
59 83f0f95a 2022-09-29 op * still enabled.
61 83f0f95a 2022-09-29 op if (bufev->wm_read.high == 0 || now < bufev->wm_read.high) {
62 83f0f95a 2022-09-29 op evbuffer_setcb(buf, NULL, NULL);
64 83f0f95a 2022-09-29 op if (bufev->enabled & EV_READ)
65 83f0f95a 2022-09-29 op #if HAVE_LIBEVENT2
66 83f0f95a 2022-09-29 op bufferevent_add(&bufev->ev_read,
67 83f0f95a 2022-09-29 op bufev->timeout_read.tv_sec);
69 83f0f95a 2022-09-29 op bufferevent_add(&bufev->ev_read, bufev->timeout_read);