Skip to content

Commit

Permalink
Scope event library related data and hooks to a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Dec 29, 2010
1 parent 8cb4d52 commit 18c55a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
14 changes: 7 additions & 7 deletions adapters/ae.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions adapters/libev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions adapters/libevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ 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 */
e = (redisLibeventEvents*)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->_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);
Expand Down
26 changes: 13 additions & 13 deletions async.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions async.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/* 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);
/* Event library data and hooks */
struct {
void *data;

/* 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). */
Expand Down

0 comments on commit 18c55a8

Please sign in to comment.