diff options
author | lns <matzeton@googlemail.com> | 2018-08-22 12:59:19 +0200 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2018-08-22 12:59:19 +0200 |
commit | e7b96666c03f1536fa89149204b363951523560e (patch) | |
tree | 1139ac1c95d6db882a488dd03bcf8de47143dce3 | |
parent | 63f2669df12d6833d0049313594e1f366e0c4127 (diff) |
event buffer fill/drain
Signed-off-by: lns <matzeton@googlemail.com>
-rw-r--r-- | src/pevent.c | 34 | ||||
-rw-r--r-- | src/pevent.h | 4 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/pevent.c b/src/pevent.c index d0565f3..452b621 100644 --- a/src/pevent.c +++ b/src/pevent.c @@ -121,7 +121,7 @@ add_eventbuf(event_ctx *ctx) if (siz < ctx->buffer_used + 1) { siz += POTD_EVENTBUF_REALLOCSIZ; - ctx->buffer_array = realloc(ctx->buffer_array, + ctx->buffer_array = (event_buf *) realloc(ctx->buffer_array, sizeof(*ctx->buffer_array) * siz); assert(ctx->buffer_array); @@ -226,9 +226,12 @@ int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data) ctx->has_error = 1; } else { - if (!on_event(ctx, ctx->events[i].data.ptr, user_data) && !ctx->has_error) + if (!on_event(ctx, (event_buf *) ctx->events[i].data.ptr, + user_data) && !ctx->has_error) + { W2("Event callback failed: [fd: %d , npoll: %d]", buf->fd, n); + } } if (!ctx->active || ctx->has_error) @@ -323,3 +326,30 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data, } return rc; } + +int event_buf_fill(event_buf *buf, unsigned char *data, size_t size) +{ + if (size > buf->buf_used && event_buf_drain(buf) < 0) + return 1; + if (size > sizeof(buf->buf)) + return 1; + memcpy(buf->buf + buf->buf_used, data, size); + buf->buf_used += size; + + return 0; +} + +ssize_t event_buf_drain(event_buf *buf) +{ + ssize_t written; + + if (!buf->buf_used) + return 0; + + written = write(buf->fd, buf->buf, buf->buf_used); + if (written < 0) + return -1; + buf->buf_used -= written; + + return written; +} diff --git a/src/pevent.h b/src/pevent.h index 1c50073..714a7f8 100644 --- a/src/pevent.h +++ b/src/pevent.h @@ -94,4 +94,8 @@ forward_state event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data, void *user_data); +int event_buf_fill(event_buf *buf, unsigned char *data, size_t size); + +ssize_t event_buf_drain(event_buf *buf); + #endif |