- Save statistics
[dragonfly.git] / contrib / libevent / evbuffer.c
blob52712bce58567cf2f871b892a3ee0c4c9699c774
1 /*
2 * Copyright (c) 2002-2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 <sys/types.h>
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #ifdef HAVE_STDARG_H
43 #include <stdarg.h>
44 #endif
46 #include "event.h"
48 /* prototypes */
50 void bufferevent_setwatermark(struct bufferevent *, short, size_t, size_t);
51 void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *);
53 static int
54 bufferevent_add(struct event *ev, int timeout)
56 struct timeval tv, *ptv = NULL;
58 if (timeout) {
59 timerclear(&tv);
60 tv.tv_sec = timeout;
61 ptv = &tv;
64 return (event_add(ev, ptv));
67 /*
68 * This callback is executed when the size of the input buffer changes.
69 * We use it to apply back pressure on the reading side.
72 void
73 bufferevent_read_pressure_cb(struct evbuffer *buf, size_t old, size_t now,
74 void *arg) {
75 struct bufferevent *bufev = arg;
76 /*
77 * If we are below the watermark then reschedule reading if it's
78 * still enabled.
80 if (bufev->wm_read.high == 0 || now < bufev->wm_read.high) {
81 evbuffer_setcb(buf, NULL, NULL);
83 if (bufev->enabled & EV_READ)
84 bufferevent_add(&bufev->ev_read, bufev->timeout_read);
88 static void
89 bufferevent_readcb(int fd, short event, void *arg)
91 struct bufferevent *bufev = arg;
92 int res = 0;
93 short what = EVBUFFER_READ;
94 size_t len;
95 int howmuch = -1;
97 if (event == EV_TIMEOUT) {
98 what |= EVBUFFER_TIMEOUT;
99 goto error;
103 * If we have a high watermark configured then we don't want to
104 * read more data than would make us reach the watermark.
106 if (bufev->wm_read.high != 0)
107 howmuch = bufev->wm_read.high;
109 res = evbuffer_read(bufev->input, fd, howmuch);
110 if (res == -1) {
111 if (errno == EAGAIN || errno == EINTR)
112 goto reschedule;
113 /* error case */
114 what |= EVBUFFER_ERROR;
115 } else if (res == 0) {
116 /* eof case */
117 what |= EVBUFFER_EOF;
120 if (res <= 0)
121 goto error;
123 bufferevent_add(&bufev->ev_read, bufev->timeout_read);
125 /* See if this callbacks meets the water marks */
126 len = EVBUFFER_LENGTH(bufev->input);
127 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
128 return;
129 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
130 struct evbuffer *buf = bufev->input;
131 event_del(&bufev->ev_read);
133 /* Now schedule a callback for us */
134 evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
135 return;
138 /* Invoke the user callback - must always be called last */
139 if (bufev->readcb != NULL)
140 (*bufev->readcb)(bufev, bufev->cbarg);
141 return;
143 reschedule:
144 bufferevent_add(&bufev->ev_read, bufev->timeout_read);
145 return;
147 error:
148 (*bufev->errorcb)(bufev, what, bufev->cbarg);
151 static void
152 bufferevent_writecb(int fd, short event, void *arg)
154 struct bufferevent *bufev = arg;
155 int res = 0;
156 short what = EVBUFFER_WRITE;
158 if (event == EV_TIMEOUT) {
159 what |= EVBUFFER_TIMEOUT;
160 goto error;
163 if (EVBUFFER_LENGTH(bufev->output)) {
164 res = evbuffer_write(bufev->output, fd);
165 if (res == -1) {
166 #ifndef WIN32
167 /*todo. evbuffer uses WriteFile when WIN32 is set. WIN32 system calls do not
168 *set errno. thus this error checking is not portable*/
169 if (errno == EAGAIN ||
170 errno == EINTR ||
171 errno == EINPROGRESS)
172 goto reschedule;
173 /* error case */
174 what |= EVBUFFER_ERROR;
176 #else
177 goto reschedule;
178 #endif
180 } else if (res == 0) {
181 /* eof case */
182 what |= EVBUFFER_EOF;
184 if (res <= 0)
185 goto error;
188 if (EVBUFFER_LENGTH(bufev->output) != 0)
189 bufferevent_add(&bufev->ev_write, bufev->timeout_write);
192 * Invoke the user callback if our buffer is drained or below the
193 * low watermark.
195 if (bufev->writecb != NULL &&
196 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
197 (*bufev->writecb)(bufev, bufev->cbarg);
199 return;
201 reschedule:
202 if (EVBUFFER_LENGTH(bufev->output) != 0)
203 bufferevent_add(&bufev->ev_write, bufev->timeout_write);
204 return;
206 error:
207 (*bufev->errorcb)(bufev, what, bufev->cbarg);
211 * Create a new buffered event object.
213 * The read callback is invoked whenever we read new data.
214 * The write callback is invoked whenever the output buffer is drained.
215 * The error callback is invoked on a write/read error or on EOF.
217 * Both read and write callbacks maybe NULL. The error callback is not
218 * allowed to be NULL and have to be provided always.
221 struct bufferevent *
222 bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb,
223 everrorcb errorcb, void *cbarg)
225 struct bufferevent *bufev;
227 if ((bufev = calloc(1, sizeof(struct bufferevent))) == NULL)
228 return (NULL);
230 if ((bufev->input = evbuffer_new()) == NULL) {
231 free(bufev);
232 return (NULL);
235 if ((bufev->output = evbuffer_new()) == NULL) {
236 evbuffer_free(bufev->input);
237 free(bufev);
238 return (NULL);
241 event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev);
242 event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev);
244 bufev->readcb = readcb;
245 bufev->writecb = writecb;
246 bufev->errorcb = errorcb;
248 bufev->cbarg = cbarg;
251 * Set to EV_WRITE so that using bufferevent_write is going to
252 * trigger a callback. Reading needs to be explicitly enabled
253 * because otherwise no data will be available.
255 bufev->enabled = EV_WRITE;
257 return (bufev);
261 bufferevent_priority_set(struct bufferevent *bufev, int priority)
263 if (event_priority_set(&bufev->ev_read, priority) == -1)
264 return (-1);
265 if (event_priority_set(&bufev->ev_write, priority) == -1)
266 return (-1);
268 return (0);
271 /* Closing the file descriptor is the responsibility of the caller */
273 void
274 bufferevent_free(struct bufferevent *bufev)
276 event_del(&bufev->ev_read);
277 event_del(&bufev->ev_write);
279 evbuffer_free(bufev->input);
280 evbuffer_free(bufev->output);
282 free(bufev);
286 * Returns 0 on success;
287 * -1 on failure.
291 bufferevent_write(struct bufferevent *bufev, void *data, size_t size)
293 int res;
295 res = evbuffer_add(bufev->output, data, size);
297 if (res == -1)
298 return (res);
300 /* If everything is okay, we need to schedule a write */
301 if (size > 0 && (bufev->enabled & EV_WRITE))
302 bufferevent_add(&bufev->ev_write, bufev->timeout_write);
304 return (res);
308 bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
310 int res;
312 res = bufferevent_write(bufev, buf->buffer, buf->off);
313 if (res != -1)
314 evbuffer_drain(buf, buf->off);
316 return (res);
319 size_t
320 bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
322 struct evbuffer *buf = bufev->input;
324 if (buf->off < size)
325 size = buf->off;
327 /* Copy the available data to the user buffer */
328 memcpy(data, buf->buffer, size);
330 if (size)
331 evbuffer_drain(buf, size);
333 return (size);
337 bufferevent_enable(struct bufferevent *bufev, short event)
339 if (event & EV_READ) {
340 if (bufferevent_add(&bufev->ev_read, bufev->timeout_read) == -1)
341 return (-1);
343 if (event & EV_WRITE) {
344 if (bufferevent_add(&bufev->ev_write, bufev->timeout_write) == -1)
345 return (-1);
348 bufev->enabled |= event;
349 return (0);
353 bufferevent_disable(struct bufferevent *bufev, short event)
355 if (event & EV_READ) {
356 if (event_del(&bufev->ev_read) == -1)
357 return (-1);
359 if (event & EV_WRITE) {
360 if (event_del(&bufev->ev_write) == -1)
361 return (-1);
364 bufev->enabled &= ~event;
365 return (0);
369 * Sets the read and write timeout for a buffered event.
372 void
373 bufferevent_settimeout(struct bufferevent *bufev,
374 int timeout_read, int timeout_write) {
375 bufev->timeout_read = timeout_read;
376 bufev->timeout_write = timeout_write;
380 * Sets the water marks
383 void
384 bufferevent_setwatermark(struct bufferevent *bufev, short events,
385 size_t lowmark, size_t highmark)
387 if (events & EV_READ) {
388 bufev->wm_read.low = lowmark;
389 bufev->wm_read.high = highmark;
392 if (events & EV_WRITE) {
393 bufev->wm_write.low = lowmark;
394 bufev->wm_write.high = highmark;
397 /* If the watermarks changed then see if we should call read again */
398 bufferevent_read_pressure_cb(bufev->input,
399 0, EVBUFFER_LENGTH(bufev->input), bufev);
403 bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
405 int res;
407 res = event_base_set(base, &bufev->ev_read);
408 if (res == -1)
409 return (res);
411 res = event_base_set(base, &bufev->ev_write);
412 return (res);