Blame


1 83f0f95a 2022-09-29 op /*
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.
4 83f0f95a 2022-09-29 op *
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
7 83f0f95a 2022-09-29 op * are met:
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.
15 83f0f95a 2022-09-29 op *
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.
26 83f0f95a 2022-09-29 op */
27 83f0f95a 2022-09-29 op
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"
31 83f0f95a 2022-09-29 op
32 83f0f95a 2022-09-29 op void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t,
33 83f0f95a 2022-09-29 op void *);
34 83f0f95a 2022-09-29 op
35 83f0f95a 2022-09-29 op static int
36 83f0f95a 2022-09-29 op bufferevent_add(struct event *ev, int timeout)
37 83f0f95a 2022-09-29 op {
38 83f0f95a 2022-09-29 op struct timeval tv, *ptv = NULL;
39 83f0f95a 2022-09-29 op
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;
43 83f0f95a 2022-09-29 op ptv = &tv;
44 83f0f95a 2022-09-29 op }
45 83f0f95a 2022-09-29 op
46 83f0f95a 2022-09-29 op return (event_add(ev, ptv));
47 83f0f95a 2022-09-29 op }
48 83f0f95a 2022-09-29 op
49 83f0f95a 2022-09-29 op /*
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.
52 83f0f95a 2022-09-29 op */
53 83f0f95a 2022-09-29 op void
54 83f0f95a 2022-09-29 op bufferevent_read_pressure_cb(struct evbuffer *buf, size_t old, size_t now,
55 83f0f95a 2022-09-29 op void *arg) {
56 83f0f95a 2022-09-29 op struct bufferevent *bufev = arg;
57 83f0f95a 2022-09-29 op /*
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.
60 83f0f95a 2022-09-29 op */
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);
63 83f0f95a 2022-09-29 op
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);
68 83f0f95a 2022-09-29 op #else
69 83f0f95a 2022-09-29 op bufferevent_add(&bufev->ev_read, bufev->timeout_read);
70 83f0f95a 2022-09-29 op #endif
71 83f0f95a 2022-09-29 op }
72 83f0f95a 2022-09-29 op }