| 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. |
| 15 | * |
| 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. |
| 26 | */ |
| 27 |
| 28 | #include <sys/types.h> |
| 29 |
| 30 | #include "config.h" |
| 31 |
| 32 | #ifdef HAVE_SYS_TIME_H |
| 33 | #include <sys/time.h> |
| 34 | #endif |
| 35 |
| 36 | #include <errno.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #ifdef HAVE_STDARG_H |
| 41 | #include <stdarg.h> |
| 42 | #endif |
| 43 |
| 44 | #ifdef WIN32 |
| 45 | #include <winsock2.h> |
| 46 | #endif |
| 47 |
| 48 | #include "evutil.h" |
| 49 | #include "event.h" |
| 50 |
| 51 | /* prototypes */ |
| 52 |
| 53 | void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *); |
| 54 |
| 55 | static int |
| 56 | bufferevent_add(struct event *ev, int timeout) |
| 57 | { |
| 58 | struct timeval tv, *ptv = NULL; |
| 59 |
| 60 | if (timeout) { |
| 61 | evutil_timerclear(&tv); |
| 62 | tv.tv_sec = timeout; |
| 63 | ptv = &tv; |
| 64 | } |
| 65 |
| 66 | return (event_add(ev, ptv)); |
| 67 | } |
| 68 |
| 69 | /* |
| 70 | * This callback is executed when the size of the input buffer changes. |
| 71 | * We use it to apply back pressure on the reading side. |
| 72 | */ |
| 73 |
| 74 | void |
| 75 | bufferevent_read_pressure_cb(struct evbuffer *buf, size_t old, size_t now, |
| 76 | void *arg) { |
| 77 | struct bufferevent *bufev = arg; |
| 78 | /* |
| 79 | * If we are below the watermark then reschedule reading if it's |
| 80 | * still enabled. |
| 81 | */ |
| 82 | if (bufev->wm_read.high == 0 || now < bufev->wm_read.high) { |
| 83 | evbuffer_setcb(buf, NULL, NULL); |
| 84 |
| 85 | if (bufev->enabled & EV_READ) |
| 86 | bufferevent_add(&bufev->ev_read, bufev->timeout_read); |
| 87 | } |
| 88 | } |
| 89 |
| 90 | static void |
| 91 | bufferevent_readcb(int fd, short event, void *arg) |
| 92 | { |
| 93 | struct bufferevent *bufev = arg; |
| 94 | int res = 0; |
| 95 | short what = EVBUFFER_READ; |
| 96 | size_t len; |
| 97 | int howmuch = -1; |
| 98 |
| 99 | if (event == EV_TIMEOUT) { |
| 100 | what |= EVBUFFER_TIMEOUT; |
| 101 | goto error; |
| 102 | } |
| 103 |
| 104 | /* |
| 105 | * If we have a high watermark configured then we don't want to |
| 106 | * read more data than would make us reach the watermark. |
| 107 | */ |
| 108 | if (bufev->wm_read.high != 0) { |
| 109 | howmuch = bufev->wm_read.high - EVBUFFER_LENGTH(bufev->input); |
| 110 | /* we might have lowered the watermark, stop reading */ |
| 111 | if (howmuch <= 0) { |
| 112 | struct evbuffer *buf = bufev->input; |
| 113 | event_del(&bufev->ev_read); |
| 114 | evbuffer_setcb(buf, |
| 115 | bufferevent_read_pressure_cb, bufev); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 |
| 120 | res = evbuffer_read(bufev->input, fd, howmuch); |
| 121 | if (res == -1) { |
| 122 | if (errno == EAGAIN || errno == EINTR) |
| 123 | goto reschedule; |
| 124 | /* error case */ |
| 125 | what |= EVBUFFER_ERROR; |
| 126 | } else if (res == 0) { |
| 127 | /* eof case */ |
| 128 | what |= EVBUFFER_EOF; |
| 129 | } |
| 130 |
| 131 | if (res <= 0) |
| 132 | goto error; |
| 133 |
| 134 | bufferevent_add(&bufev->ev_read, bufev->timeout_read); |
| 135 |
| 136 | /* See if this callbacks meets the water marks */ |
| 137 | len = EVBUFFER_LENGTH(bufev->input); |
| 138 | if (bufev->wm_read.low != 0 && len < bufev->wm_read.low) |
| 139 | return; |
| 140 | if (bufev->wm_read.high != 0 && len >= bufev->wm_read.high) { |
| 141 | struct evbuffer *buf = bufev->input; |
| 142 | event_del(&bufev->ev_read); |
| 143 |
| 144 | /* Now schedule a callback for us when the buffer changes */ |
| 145 | evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev); |
| 146 | } |
| 147 |
| 148 | /* Invoke the user callback - must always be called last */ |
| 149 | if (bufev->readcb != NULL) |
| 150 | (*bufev->readcb)(bufev, bufev->cbarg); |
| 151 | return; |
| 152 |
| 153 | reschedule: |
| 154 | bufferevent_add(&bufev->ev_read, bufev->timeout_read); |
| 155 | return; |
| 156 |
| 157 | error: |
| 158 | (*bufev->errorcb)(bufev, what, bufev->cbarg); |
| 159 | } |
| 160 |
| 161 | static void |
| 162 | bufferevent_writecb(int fd, short event, void *arg) |
| 163 | { |
| 164 | struct bufferevent *bufev = arg; |
| 165 | int res = 0; |
| 166 | short what = EVBUFFER_WRITE; |
| 167 |
| 168 | if (event == EV_TIMEOUT) { |
| 169 | what |= EVBUFFER_TIMEOUT; |
| 170 | goto error; |
| 171 | } |
| 172 |
| 173 | if (EVBUFFER_LENGTH(bufev->output)) { |
| 174 | res = evbuffer_write(bufev->output, fd); |
| 175 | if (res == -1) { |
| 176 | #ifndef WIN32 |
| 177 | /*todo. evbuffer uses WriteFile when WIN32 is set. WIN32 system calls do not |
| 178 | *set errno. thus this error checking is not portable*/ |
| 179 | if (errno == EAGAIN || |
| 180 | errno == EINTR || |
| 181 | errno == EINPROGRESS) |
| 182 | goto reschedule; |
| 183 | /* error case */ |
| 184 | what |= EVBUFFER_ERROR; |
| 185 |
| 186 | #else |
| 187 | goto reschedule; |
| 188 | #endif |
| 189 |
| 190 | } else if (res == 0) { |
| 191 | /* eof case */ |
| 192 | what |= EVBUFFER_EOF; |
| 193 | } |
| 194 | if (res <= 0) |
| 195 | goto error; |
| 196 | } |
| 197 |
| 198 | if (EVBUFFER_LENGTH(bufev->output) != 0) |
| 199 | bufferevent_add(&bufev->ev_write, bufev->timeout_write); |
| 200 |
| 201 | /* |
| 202 | * Invoke the user callback if our buffer is drained or below the |
| 203 | * low watermark. |
| 204 | */ |
| 205 | if (bufev->writecb != NULL && |
| 206 | EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low) |
| 207 | (*bufev->writecb)(bufev, bufev->cbarg); |
| 208 |
| 209 | return; |
| 210 |
| 211 | reschedule: |
| 212 | if (EVBUFFER_LENGTH(bufev->output) != 0) |
| 213 | bufferevent_add(&bufev->ev_write, bufev->timeout_write); |
| 214 | return; |
| 215 |
| 216 | error: |
| 217 | (*bufev->errorcb)(bufev, what, bufev->cbarg); |
| 218 | } |
| 219 |
| 220 | /* |
| 221 | * Create a new buffered event object. |
| 222 | * |
| 223 | * The read callback is invoked whenever we read new data. |
| 224 | * The write callback is invoked whenever the output buffer is drained. |
| 225 | * The error callback is invoked on a write/read error or on EOF. |
| 226 | * |
| 227 | * Both read and write callbacks maybe NULL. The error callback is not |
| 228 | * allowed to be NULL and have to be provided always. |
| 229 | */ |
| 230 |
| 231 | struct bufferevent * |
| 232 | bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb, |
| 233 | everrorcb errorcb, void *cbarg) |
| 234 | { |
| 235 | struct bufferevent *bufev; |
| 236 |
| 237 | if ((bufev = calloc(1, sizeof(struct bufferevent))) == NULL) |
| 238 | return (NULL); |
| 239 |
| 240 | if ((bufev->input = evbuffer_new()) == NULL) { |
| 241 | free(bufev); |
| 242 | return (NULL); |
| 243 | } |
| 244 |
| 245 | if ((bufev->output = evbuffer_new()) == NULL) { |
| 246 | evbuffer_free(bufev->input); |
| 247 | free(bufev); |
| 248 | return (NULL); |
| 249 | } |
| 250 |
| 251 | event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev); |
| 252 | event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev); |
| 253 |
| 254 | bufferevent_setcb(bufev, readcb, writecb, errorcb, cbarg); |
| 255 |
| 256 | /* |
| 257 | * Set to EV_WRITE so that using bufferevent_write is going to |
| 258 | * trigger a callback. Reading needs to be explicitly enabled |
| 259 | * because otherwise no data will be available. |
| 260 | */ |
| 261 | bufev->enabled = EV_WRITE; |
| 262 |
| 263 | return (bufev); |
| 264 | } |
| 265 |
| 266 | void |
| 267 | bufferevent_setcb(struct bufferevent *bufev, |
| 268 | evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) |
| 269 | { |
| 270 | bufev->readcb = readcb; |
| 271 | bufev->writecb = writecb; |
| 272 | bufev->errorcb = errorcb; |
| 273 |
| 274 | bufev->cbarg = cbarg; |
| 275 | } |
| 276 |
| 277 | void |
| 278 | bufferevent_setfd(struct bufferevent *bufev, int fd) |
| 279 | { |
| 280 | event_del(&bufev->ev_read); |
| 281 | event_del(&bufev->ev_write); |
| 282 |
| 283 | event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev); |
| 284 | event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev); |
| 285 | if (bufev->ev_base != NULL) { |
| 286 | event_base_set(bufev->ev_base, &bufev->ev_read); |
| 287 | event_base_set(bufev->ev_base, &bufev->ev_write); |
| 288 | } |
| 289 |
| 290 | /* might have to manually trigger event registration */ |
| 291 | } |
| 292 |
| 293 | int |
| 294 | bufferevent_priority_set(struct bufferevent *bufev, int priority) |
| 295 | { |
| 296 | if (event_priority_set(&bufev->ev_read, priority) == -1) |
| 297 | return (-1); |
| 298 | if (event_priority_set(&bufev->ev_write, priority) == -1) |
| 299 | return (-1); |
| 300 |
| 301 | return (0); |
| 302 | } |
| 303 |
| 304 | /* Closing the file descriptor is the responsibility of the caller */ |
| 305 |
| 306 | void |
| 307 | bufferevent_free(struct bufferevent *bufev) |
| 308 | { |
| 309 | event_del(&bufev->ev_read); |
| 310 | event_del(&bufev->ev_write); |
| 311 |
| 312 | evbuffer_free(bufev->input); |
| 313 | evbuffer_free(bufev->output); |
| 314 |
| 315 | free(bufev); |
| 316 | } |
| 317 |
| 318 | /* |
| 319 | * Returns 0 on success; |
| 320 | * -1 on failure. |
| 321 | */ |
| 322 |
| 323 | int |
| 324 | bufferevent_write(struct bufferevent *bufev, const void *data, size_t size) |
| 325 | { |
| 326 | int res; |
| 327 |
| 328 | res = evbuffer_add(bufev->output, data, size); |
| 329 |
| 330 | if (res == -1) |
| 331 | return (res); |
| 332 |
| 333 | /* If everything is okay, we need to schedule a write */ |
| 334 | if (size > 0 && (bufev->enabled & EV_WRITE)) |
| 335 | bufferevent_add(&bufev->ev_write, bufev->timeout_write); |
| 336 |
| 337 | return (res); |
| 338 | } |
| 339 |
| 340 | int |
| 341 | bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf) |
| 342 | { |
| 343 | int res; |
| 344 |
| 345 | res = bufferevent_write(bufev, buf->buffer, buf->off); |
| 346 | if (res != -1) |
| 347 | evbuffer_drain(buf, buf->off); |
| 348 |
| 349 | return (res); |
| 350 | } |
| 351 |
| 352 | size_t |
| 353 | bufferevent_read(struct bufferevent *bufev, void *data, size_t size) |
| 354 | { |
| 355 | struct evbuffer *buf = bufev->input; |
| 356 |
| 357 | if (buf->off < size) |
| 358 | size = buf->off; |
| 359 |
| 360 | /* Copy the available data to the user buffer */ |
| 361 | memcpy(data, buf->buffer, size); |
| 362 |
| 363 | if (size) |
| 364 | evbuffer_drain(buf, size); |
| 365 |
| 366 | return (size); |
| 367 | } |
| 368 |
| 369 | int |
| 370 | bufferevent_enable(struct bufferevent *bufev, short event) |
| 371 | { |
| 372 | if (event & EV_READ) { |
| 373 | if (bufferevent_add(&bufev->ev_read, bufev->timeout_read) == -1) |
| 374 | return (-1); |
| 375 | } |
| 376 | if (event & EV_WRITE) { |
| 377 | if (bufferevent_add(&bufev->ev_write, bufev->timeout_write) == -1) |
| 378 | return (-1); |
| 379 | } |
| 380 |
| 381 | bufev->enabled |= event; |
| 382 | return (0); |
| 383 | } |
| 384 |
| 385 | int |
| 386 | bufferevent_disable(struct bufferevent *bufev, short event) |
| 387 | { |
| 388 | if (event & EV_READ) { |
| 389 | if (event_del(&bufev->ev_read) == -1) |
| 390 | return (-1); |
| 391 | } |
| 392 | if (event & EV_WRITE) { |
| 393 | if (event_del(&bufev->ev_write) == -1) |
| 394 | return (-1); |
| 395 | } |
| 396 |
| 397 | bufev->enabled &= ~event; |
| 398 | return (0); |
| 399 | } |
| 400 |
| 401 | /* |
| 402 | * Sets the read and write timeout for a buffered event. |
| 403 | */ |
| 404 |
| 405 | void |
| 406 | bufferevent_settimeout(struct bufferevent *bufev, |
| 407 | int timeout_read, int timeout_write) { |
| 408 | bufev->timeout_read = timeout_read; |
| 409 | bufev->timeout_write = timeout_write; |
| 410 |
| 411 | if (event_pending(&bufev->ev_read, EV_READ, NULL)) |
| 412 | bufferevent_add(&bufev->ev_read, timeout_read); |
| 413 | if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) |
| 414 | bufferevent_add(&bufev->ev_write, timeout_write); |
| 415 | } |
| 416 |
| 417 | /* |
| 418 | * Sets the water marks |
| 419 | */ |
| 420 |
| 421 | void |
| 422 | bufferevent_setwatermark(struct bufferevent *bufev, short events, |
| 423 | size_t lowmark, size_t highmark) |
| 424 | { |
| 425 | if (events & EV_READ) { |
| 426 | bufev->wm_read.low = lowmark; |
| 427 | bufev->wm_read.high = highmark; |
| 428 | } |
| 429 |
| 430 | if (events & EV_WRITE) { |
| 431 | bufev->wm_write.low = lowmark; |
| 432 | bufev->wm_write.high = highmark; |
| 433 | } |
| 434 |
| 435 | /* If the watermarks changed then see if we should call read again */ |
| 436 | bufferevent_read_pressure_cb(bufev->input, |
| 437 | 0, EVBUFFER_LENGTH(bufev->input), bufev); |
| 438 | } |
| 439 |
| 440 | int |
| 441 | bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) |
| 442 | { |
| 443 | int res; |
| 444 |
| 445 | bufev->ev_base = base; |
| 446 |
| 447 | res = event_base_set(base, &bufev->ev_read); |
| 448 | if (res == -1) |
| 449 | return (res); |
| 450 |
| 451 | res = event_base_set(base, &bufev->ev_write); |
| 452 | return (res); |
| 453 | } |
| 454 |