diff options
author | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-12-31 13:31:24 +0100 |
---|---|---|
committer | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2010-12-31 15:49:26 +0100 |
commit | e6621d05b4b7a2bcc866757255297012b749dbac (patch) | |
tree | 09118350dfaabbf119d186cfac1aa4de9c4bae52 | |
parent | b758e52e44580c45dd863ba884613fcd9ec61a71 (diff) |
Add fields for subscribed channel/pattern names
-rw-r--r-- | async.c | 45 | ||||
-rw-r--r-- | async.h | 10 |
2 files changed, 54 insertions, 1 deletions
@@ -38,6 +38,47 @@ /* Forward declaration of function in hiredis.c */ void __redisAppendCommand(redisContext *c, char *cmd, size_t len); +/* Functions managing dictionary of callbacks for pub/sub. */ +static unsigned int callbackHash(const void *key) { + return dictGenHashFunction((unsigned char*)key,sdslen((char*)key)); +} + +static void *callbackValDup(void *privdata, const void *src) { + ((void) privdata); + redisCallback *dup = malloc(sizeof(*dup)); + memcpy(dup,src,sizeof(*dup)); + return dup; +} + +static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) { + int l1, l2; + ((void) privdata); + + l1 = sdslen((sds)key1); + l2 = sdslen((sds)key2); + if (l1 != l2) return 0; + return memcmp(key1,key2,l1) == 0; +} + +static void callbackKeyDestructor(void *privdata, void *key) { + ((void) privdata); + sdsfree((sds)key); +} + +static void callbackValDestructor(void *privdata, void *val) { + ((void) privdata); + free(val); +} + +static dictType callbackDict = { + callbackHash, + NULL, + callbackValDup, + callbackKeyCompare, + callbackKeyDestructor, + callbackValDestructor +}; + static redisAsyncContext *redisAsyncInitialize(redisContext *c) { redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext)); c = &(ac->c); @@ -63,6 +104,10 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) { ac->replies.head = NULL; ac->replies.tail = NULL; + ac->sub.invalid.head = NULL; + ac->sub.invalid.tail = NULL; + ac->sub.channels = dictCreate(&callbackDict,NULL); + ac->sub.patterns = dictCreate(&callbackDict,NULL); return ac; } @@ -32,6 +32,7 @@ #ifndef __HIREDIS_ASYNC_H #define __HIREDIS_ASYNC_H #include "hiredis.h" +#include "dict.h" #ifdef __cplusplus extern "C" { @@ -88,8 +89,15 @@ typedef struct redisAsyncContext { /* Called when the first write event was received. */ redisConnectCallback *onConnect; - /* Reply callbacks */ + /* Regular command callbacks */ redisCallbackList replies; + + /* Subscription callbacks */ + struct { + redisCallbackList invalid; + dict *channels; + dict *patterns; + } sub; } redisAsyncContext; /* Functions that proxy to hiredis */ |