Created
July 16, 2012 23:16
-
-
Save mitsuhiko/3125730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <hiredis/hiredis.h> | |
#include <hiredis/async.h> | |
typedef struct redis_libuv_events { | |
redisAsyncContext *context; | |
uv_loop_t *loop; | |
int reading; | |
int writing; | |
uv_prepare_t read_handle; | |
uv_prepare_t write_handle; | |
} redis_libuv_events; | |
static void | |
redis_libuv_read_event(uv_prepare_t *handle, int status) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)handle->data; | |
redisAsyncHandleRead(e->context); | |
} | |
static void | |
redis_libuv_write_event(uv_prepare_t *handle, int status) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)handle->data; | |
redisAsyncHandleWrite(e->context); | |
} | |
static void | |
redis_libuv_add_read(void *privdata) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)privdata; | |
if (!e->reading) { | |
e->reading = 1; | |
uv_prepare_start(&e->read_handle, redis_libuv_read_event); | |
} | |
} | |
static void | |
redis_libuv_del_read(void *privdata) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)privdata; | |
if (e->reading) { | |
e->reading = 0; | |
uv_prepare_stop(&e->read_handle); | |
} | |
} | |
static void | |
redis_libuv_add_write(void *privdata) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)privdata; | |
if (!e->writing) { | |
e->writing = 1; | |
uv_prepare_start(&e->write_handle, redis_libuv_write_event); | |
} | |
} | |
static void | |
redis_libuv_del_write(void *privdata) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)privdata; | |
if (e->writing) { | |
e->writing = 0; | |
uv_prepare_stop(&e->write_handle); | |
} | |
} | |
static void | |
redis_libuv_cleanup(void *privdata) | |
{ | |
redis_libuv_events *e = (redis_libuv_events *)privdata; | |
redis_libuv_del_read(privdata); | |
redis_libuv_del_write(privdata); | |
free(e); | |
} | |
static int | |
redis_libuv_attach(uv_loop_t *loop, redisAsyncContext *ac) | |
{ | |
redis_libuv_events *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(redis_libuv_events)); | |
e->context = ac; | |
e->loop = loop; | |
/* Register functions to start/stop listening for events */ | |
ac->ev.addRead = redis_libuv_add_read; | |
ac->ev.delRead = redis_libuv_del_read; | |
ac->ev.addWrite = redis_libuv_add_write; | |
ac->ev.delWrite = redis_libuv_del_write; | |
ac->ev.cleanup = redis_libuv_cleanup; | |
ac->ev.data = e; | |
/* Initialize read/write events */ | |
uv_prepare_init(e->loop, &e->read_handle); | |
uv_prepare_init(e->loop, &e->write_handle); | |
e->reading = 0; | |
e->writing = 0; | |
e->read_handle.data = e; | |
e->write_handle.data = e; | |
return REDIS_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment