Scope event library related data and hooks to a struct

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

View File

@ -82,7 +82,7 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
redisLibevEvents *e;
/* Nothing should be attached when something is already attached */
if (ac->_adapter_data != NULL)
if (ac->ev.data != NULL)
return REDIS_ERR;
/* Create container for context and r/w events */
@ -98,12 +98,12 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
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->_adapter_data = e;
ac->ev.addRead = redisLibevAddRead;
ac->ev.delRead = redisLibevDelRead;
ac->ev.addWrite = redisLibevAddWrite;
ac->ev.delWrite = redisLibevDelWrite;
ac->ev.cleanup = redisLibevCleanup;
ac->ev.data = e;
/* Initialize read/write events */
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;
/* Nothing should be attached when something is already attached */
if (ac->_adapter_data != NULL)
if (ac->ev.data != NULL)
return REDIS_ERR;
/* Create container for context and r/w events */
@ -60,12 +60,12 @@ int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
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->_adapter_data = e;
ac->ev.addRead = redisLibeventAddRead;
ac->ev.delRead = redisLibeventDelRead;
ac->ev.addWrite = redisLibeventAddWrite;
ac->ev.delWrite = redisLibeventDelWrite;
ac->ev.cleanup = redisLibeventCleanup;
ac->ev.data = e;
/* Initialize and install read/write events */
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->errstr = NULL;
ac->data = NULL;
ac->_adapter_data = NULL;
ac->evAddRead = NULL;
ac->evDelRead = NULL;
ac->evAddWrite = NULL;
ac->evDelWrite = NULL;
ac->evCleanup = NULL;
ac->ev.data = NULL;
ac->ev.addRead = NULL;
ac->ev.delRead = NULL;
ac->ev.addWrite = NULL;
ac->ev.delWrite = NULL;
ac->ev.cleanup = NULL;
ac->onConnect = 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 first write event to be fired. This assumes the related event
* 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_ERR;
@ -166,7 +166,7 @@ static void __redisAsyncFree(redisAsyncContext *ac) {
}
/* 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
* this context, the status will always be REDIS_OK. */
@ -279,7 +279,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) {
__redisAsyncDisconnect(ac);
} else {
/* Always re-schedule reads */
if (ac->evAddRead) ac->evAddRead(ac->_adapter_data);
if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
redisProcessCallbacks(ac);
}
}
@ -293,13 +293,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
} else {
/* Continue writing when not done, stop writing otherwise */
if (!done) {
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data);
if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
} else {
if (ac->evDelWrite) ac->evDelWrite(ac->_adapter_data);
if (ac->ev.delWrite) ac->ev.delWrite(ac->ev.data);
}
/* 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. */
if (!(c->flags & REDIS_CONNECTED)) {
@ -328,7 +328,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void
__redisPushCallback(&ac->replies,&cb);
/* 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;
}

20
async.h
View File

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