diff options
author | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-11-01 10:42:32 +0100 |
---|---|---|
committer | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-11-01 10:42:32 +0100 |
commit | 89e06225358c9b44998107bb55e80963904a30ce (patch) | |
tree | 350ce99ee5fbf974b8437bf561f9825d7607619b /adapters | |
parent | d5fc7d8c53db80fc0f5cbe59c3fe371c56c2b501 (diff) |
Move libev/libevent headers to adapters directory
Diffstat (limited to 'adapters')
-rw-r--r-- | adapters/libev.h | 92 | ||||
-rw-r--r-- | adapters/libevent.h | 76 |
2 files changed, 168 insertions, 0 deletions
diff --git a/adapters/libev.h b/adapters/libev.h new file mode 100644 index 0000000..79c069d --- /dev/null +++ b/adapters/libev.h @@ -0,0 +1,92 @@ +#include <sys/types.h> +#include <ev.h> +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibevEvents { + redisAsyncContext *context; + struct ev_loop *loop; + int reading, writing; + ev_io rev, wev; +} redisLibevEvents; + +void redisLibevReadEvent(struct ev_loop *loop, ev_io *watcher, int revents) { + ((void)loop); ((void)revents); + redisLibevEvents *e = watcher->data; + redisAsyncHandleRead(e->context); +} + +void redisLibevWriteEvent(struct ev_loop *loop, ev_io *watcher, int revents) { + ((void)loop); ((void)revents); + redisLibevEvents *e = watcher->data; + redisAsyncHandleWrite(e->context); +} + +void redisLibevAddRead(void *privdata) { + redisLibevEvents *e = privdata; + if (!e->reading) { + e->reading = 1; + ev_io_start(e->loop,&e->rev); + } +} + +void redisLibevDelRead(void *privdata) { + redisLibevEvents *e = privdata; + if (e->reading) { + e->reading = 0; + ev_io_stop(e->loop,&e->rev); + } +} + +void redisLibevAddWrite(void *privdata) { + redisLibevEvents *e = privdata; + if (!e->writing) { + e->writing = 1; + ev_io_start(e->loop,&e->wev); + } +} + +void redisLibevDelWrite(void *privdata) { + redisLibevEvents *e = privdata; + if (e->writing) { + e->writing = 0; + ev_io_stop(e->loop,&e->wev); + } +} + +void redisLibevCleanup(void *privdata) { + redisLibevEvents *e = privdata; + redisLibevDelRead(privdata); + redisLibevDelWrite(privdata); + free(e); +} + +int redisLibevAttach(redisAsyncContext *ac, struct ev_loop *loop) { + redisContext *c = &(ac->c); + redisLibevEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = malloc(sizeof(*e)); + e->context = ac; + e->loop = loop; + e->reading = e->writing = 0; + e->rev.data = e; + e->wev.data = e; + + /* Register functions to start/stop listening for events */ + ac->evAddRead = redisLibevAddRead; + ac->evDelRead = redisLibevDelRead; + ac->evAddWrite = redisLibevAddWrite; + ac->evDelWrite = redisLibevDelWrite; + ac->evCleanup = redisLibevCleanup; + ac->data = e; + + /* Initialize read/write events */ + ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); + ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); + return REDIS_OK; +} diff --git a/adapters/libevent.h b/adapters/libevent.h new file mode 100644 index 0000000..1b759c1 --- /dev/null +++ b/adapters/libevent.h @@ -0,0 +1,76 @@ +#include <sys/types.h> +#include <event.h> +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibeventEvents { + redisAsyncContext *context; + struct event rev, wev; +} redisLibeventEvents; + +void redisLibeventReadEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = arg; + redisAsyncHandleRead(e->context); +} + +void redisLibeventWriteEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = arg; + redisAsyncHandleWrite(e->context); +} + +void redisLibeventAddRead(void *privdata) { + redisLibeventEvents *e = privdata; + event_add(&e->rev,NULL); +} + +void redisLibeventDelRead(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->rev); +} + +void redisLibeventAddWrite(void *privdata) { + redisLibeventEvents *e = privdata; + event_add(&e->wev,NULL); +} + +void redisLibeventDelWrite(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->wev); +} + +void redisLibeventCleanup(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->rev); + event_del(&e->wev); + free(e); +} + +int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) { + redisContext *c = &(ac->c); + redisLibeventEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = malloc(sizeof(*e)); + e->context = ac; + + /* Register functions to start/stop listening for events */ + ac->evAddRead = redisLibeventAddRead; + ac->evDelRead = redisLibeventDelRead; + ac->evAddWrite = redisLibeventAddWrite; + ac->evDelWrite = redisLibeventDelWrite; + ac->evCleanup = redisLibeventCleanup; + ac->data = e; + + /* Initialize and install read/write events */ + event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); + event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e); + event_base_set(base,&e->rev); + event_base_set(base,&e->wev); + return REDIS_OK; +} |