diff options
Diffstat (limited to 'src/pevent.h')
-rw-r--r-- | src/pevent.h | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/src/pevent.h b/src/pevent.h index 0f387aa..9384405 100644 --- a/src/pevent.h +++ b/src/pevent.h @@ -34,13 +34,18 @@ #ifndef POTD_EVENT_H #define POTD_EVENT_H 1 +#include <stdio.h> +#include <unistd.h> #include <sys/epoll.h> +#include <string.h> #include "socket.h" #define POTD_MAXFD 32 #define POTD_MAXEVENTS 64 #define POTD_EVENTBUF_REALLOCSIZ 5 +#define WRITE_BUF(fd) { fd, {0}, 0, NULL } +#define EMPTY_BUF { -1, {0}, 0, NULL } typedef enum forward_state { CON_OK, CON_IN_TERMINATED, CON_OUT_TERMINATED, @@ -52,6 +57,8 @@ typedef struct event_buf { char buf[BUFSIZ]; size_t buf_used; + + void *buf_user_data; } event_buf; typedef struct event_ctx { @@ -67,10 +74,10 @@ typedef struct event_ctx { size_t buffer_used; } event_ctx; -typedef int (*on_event_cb) (event_ctx *ev_ctx, event_buf *buf, +typedef int (*on_event_cb) (event_ctx *ev_ctx, int src_fd, void *user_data); -typedef int (*on_data_cb) (event_ctx *ev_ctx, int src_fd, int dst_fd, - char *buf, size_t siz, void *user_data); +typedef int (*on_data_cb) (event_ctx *ev_ctx, event_buf *read_buf, + event_buf *write_buf, void *user_data); void event_init(event_ctx **ctx); @@ -81,9 +88,9 @@ int event_setup(event_ctx *ctx); int event_validate_ctx(event_ctx *ctx); -int event_add_sock(event_ctx *ctx, psocket *sock); +int event_add_sock(event_ctx *ctx, psocket *sock, void *buf_user_data); -int event_add_fd(event_ctx *ctx, int fd); +int event_add_fd(event_ctx *ctx, int fd, void *buf_user_data); int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data); @@ -91,4 +98,45 @@ 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, char *data, size_t size); + +ssize_t event_buf_drain(event_buf *write_buf); + +static inline size_t event_buf_avail(event_buf *buf) +{ + return sizeof(buf->buf) - buf->buf_used; +} + +static inline ssize_t event_buf_read(event_buf *read_buf) +{ + ssize_t siz; + + siz = read(read_buf->fd, read_buf->buf + read_buf->buf_used, + event_buf_avail(read_buf)); + if (siz > 0) + read_buf->buf_used += siz; + return siz; +} + +static inline void event_buf_discard(event_buf *input, size_t siz) +{ + if (siz <= input->buf_used) { + memmove(input->buf + siz, input->buf, input->buf_used - siz); + input->buf_used -= siz; + } +} + +static inline void event_buf_discardall(event_buf *input) +{ + event_buf_discard(input, input->buf_used); +} + +static inline int event_buf_dup(event_buf *input, event_buf *output) +{ + int rc = event_buf_fill(output, input->buf, input->buf_used); + if (!rc) + event_buf_discardall(input); + return rc; +} + #endif |