Scope event library related data and hooks to a struct

master
Pieter Noordhuis 2010-12-29 15:41:03 +01:00
parent 8cb4d52cd2
commit 18c55a8f1e
5 changed files with 45 additions and 43 deletions

View File

@ -72,7 +72,7 @@ int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
redisAeEvents *e; redisAeEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->_adapter_data != NULL) if (ac->ev.data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -83,12 +83,12 @@ int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
e->reading = e->writing = 0; e->reading = e->writing = 0;
/* Register functions to start/stop listening for events */ /* Register functions to start/stop listening for events */
ac->evAddRead = redisAeAddRead; ac->ev.addRead = redisAeAddRead;
ac->evDelRead = redisAeDelRead; ac->ev.delRead = redisAeDelRead;
ac->evAddWrite = redisAeAddWrite; ac->ev.addWrite = redisAeAddWrite;
ac->evDelWrite = redisAeDelWrite; ac->ev.delWrite = redisAeDelWrite;
ac->evCleanup = redisAeCleanup; ac->ev.cleanup = redisAeCleanup;
ac->_adapter_data = e; ac->ev.data = e;
return REDIS_OK; return REDIS_OK;
} }

View File

@ -82,7 +82,7 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
redisLibevEvents *e; redisLibevEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->_adapter_data != NULL) if (ac->ev.data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -98,12 +98,12 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
e->wev.data = e; e->wev.data = e;
/* Register functions to start/stop listening for events */ /* Register functions to start/stop listening for events */
ac->evAddRead = redisLibevAddRead; ac->ev.addRead = redisLibevAddRead;
ac->evDelRead = redisLibevDelRead; ac->ev.delRead = redisLibevDelRead;
ac->evAddWrite = redisLibevAddWrite; ac->ev.addWrite = redisLibevAddWrite;
ac->evDelWrite = redisLibevDelWrite; ac->ev.delWrite = redisLibevDelWrite;
ac->evCleanup = redisLibevCleanup; ac->ev.cleanup = redisLibevCleanup;
ac->_adapter_data = e; ac->ev.data = e;
/* Initialize read/write events */ /* Initialize read/write events */
ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);

View File

@ -52,7 +52,7 @@ int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
redisLibeventEvents *e; redisLibeventEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->_adapter_data != NULL) if (ac->ev.data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -60,12 +60,12 @@ int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
e->context = ac; e->context = ac;
/* Register functions to start/stop listening for events */ /* Register functions to start/stop listening for events */
ac->evAddRead = redisLibeventAddRead; ac->ev.addRead = redisLibeventAddRead;
ac->evDelRead = redisLibeventDelRead; ac->ev.delRead = redisLibeventDelRead;
ac->evAddWrite = redisLibeventAddWrite; ac->ev.addWrite = redisLibeventAddWrite;
ac->evDelWrite = redisLibeventDelWrite; ac->ev.delWrite = redisLibeventDelWrite;
ac->evCleanup = redisLibeventCleanup; ac->ev.cleanup = redisLibeventCleanup;
ac->_adapter_data = e; ac->ev.data = e;
/* Initialize and install read/write events */ /* Initialize and install read/write events */
event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);

26
async.c
View File

@ -50,13 +50,13 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
ac->err = 0; ac->err = 0;
ac->errstr = NULL; ac->errstr = NULL;
ac->data = NULL; ac->data = NULL;
ac->_adapter_data = NULL;
ac->evAddRead = NULL; ac->ev.data = NULL;
ac->evDelRead = NULL; ac->ev.addRead = NULL;
ac->evAddWrite = NULL; ac->ev.delRead = NULL;
ac->evDelWrite = NULL; ac->ev.addWrite = NULL;
ac->evCleanup = NULL; ac->ev.delWrite = NULL;
ac->ev.cleanup = NULL;
ac->onConnect = NULL; ac->onConnect = NULL;
ac->onDisconnect = NULL; ac->onDisconnect = NULL;
@ -100,7 +100,7 @@ int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn
/* The common way to detect an established connection is to wait for /* The common way to detect an established connection is to wait for
* the first write event to be fired. This assumes the related event * the first write event to be fired. This assumes the related event
* library functions are already set. */ * library functions are already set. */
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data); if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
return REDIS_OK; return REDIS_OK;
} }
return REDIS_ERR; return REDIS_ERR;
@ -166,7 +166,7 @@ static void __redisAsyncFree(redisAsyncContext *ac) {
} }
/* Signal event lib to clean up */ /* Signal event lib to clean up */
if (ac->evCleanup) ac->evCleanup(ac->_adapter_data); if (ac->ev.cleanup) ac->ev.cleanup(ac->ev.data);
/* Execute disconnect callback. When redisAsyncFree() initiated destroying /* Execute disconnect callback. When redisAsyncFree() initiated destroying
* this context, the status will always be REDIS_OK. */ * this context, the status will always be REDIS_OK. */
@ -279,7 +279,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) {
__redisAsyncDisconnect(ac); __redisAsyncDisconnect(ac);
} else { } else {
/* Always re-schedule reads */ /* Always re-schedule reads */
if (ac->evAddRead) ac->evAddRead(ac->_adapter_data); if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
redisProcessCallbacks(ac); redisProcessCallbacks(ac);
} }
} }
@ -293,13 +293,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
} else { } else {
/* Continue writing when not done, stop writing otherwise */ /* Continue writing when not done, stop writing otherwise */
if (!done) { if (!done) {
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data); if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
} else { } else {
if (ac->evDelWrite) ac->evDelWrite(ac->_adapter_data); if (ac->ev.delWrite) ac->ev.delWrite(ac->ev.data);
} }
/* Always schedule reads after writes */ /* Always schedule reads after writes */
if (ac->evAddRead) ac->evAddRead(ac->_adapter_data); if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
/* Fire onConnect when this is the first write event. */ /* Fire onConnect when this is the first write event. */
if (!(c->flags & REDIS_CONNECTED)) { if (!(c->flags & REDIS_CONNECTED)) {
@ -328,7 +328,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void
__redisPushCallback(&ac->replies,&cb); __redisPushCallback(&ac->replies,&cb);
/* Always schedule a write when the write buffer is non-empty */ /* Always schedule a write when the write buffer is non-empty */
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data); if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
return REDIS_OK; return REDIS_OK;
} }

20
async.h
View File

@ -66,16 +66,18 @@ typedef struct redisAsyncContext {
/* Not used by hiredis */ /* Not used by hiredis */
void *data; void *data;
/* Used by the different event lib adapters to store their private data */ /* Event library data and hooks */
void *_adapter_data; struct {
void *data;
/* Called when the library expects to start reading/writing. /* Hooks that are called when the library expects to start
* The supplied functions should be idempotent. */ * reading/writing. These functions should be idempotent. */
void (*evAddRead)(void *privdata); void (*addRead)(void *privdata);
void (*evDelRead)(void *privdata); void (*delRead)(void *privdata);
void (*evAddWrite)(void *privdata); void (*addWrite)(void *privdata);
void (*evDelWrite)(void *privdata); void (*delWrite)(void *privdata);
void (*evCleanup)(void *privdata); void (*cleanup)(void *privdata);
} ev;
/* Called when either the connection is terminated due to an error or per /* Called when either the connection is terminated due to an error or per
* user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */ * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */