summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-24 12:27:44 +0100
committerPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-24 12:27:46 +0100
commit1927c643da500f4155f2a11b7595d9006ada70e9 (patch)
treeef6a1337e04662e5278c2520b3eed6bc3682684c
parentfb49cd19887e45e1118b9a3b09f582305c842e4b (diff)
Add privdata field to reply reader
This field is set in the read tasks that are passed to the reply object functions. This allows to curry arbitrary data to these functions.
-rw-r--r--hiredis.c15
-rw-r--r--hiredis.h2
2 files changed, 17 insertions, 0 deletions
diff --git a/hiredis.c b/hiredis.c
index 3feba45..3e662ef 100644
--- a/hiredis.c
+++ b/hiredis.c
@@ -48,6 +48,7 @@ typedef struct redisReader {
redisReadTask rstack[3]; /* stack of read tasks */
int ridx; /* index of stack */
+ void *privdata; /* user-settable arbitrary field */
} redisReader;
static redisReply *createReplyObject(int type);
@@ -307,6 +308,7 @@ static int processMultiBulkItem(redisReader *r) {
r->rstack[r->ridx].elements = -1;
r->rstack[r->ridx].parent = obj;
r->rstack[r->ridx].idx = 0;
+ r->rstack[r->ridx].privdata = r->privdata;
} else {
moveToNextTask(r);
}
@@ -393,6 +395,17 @@ int redisReplyReaderSetReplyObjectFunctions(void *reader, redisReplyObjectFuncti
return REDIS_ERR;
}
+/* Set the private data field that is used in the read tasks. This argument can
+ * be used to curry arbitrary data to the custom reply object functions. */
+int redisReplyReaderSetPrivdata(void *reader, void *privdata) {
+ redisReader *r = reader;
+ if (r->reply == NULL) {
+ r->privdata = privdata;
+ return REDIS_OK;
+ }
+ return REDIS_ERR;
+}
+
/* External libraries wrapping hiredis might need access to the temporary
* variable while the reply is built up. When the reader contains an
* object in between receiving some bytes to parse, this object might
@@ -426,6 +439,7 @@ static void redisSetReplyReaderError(redisReader *r, sds err) {
r->ridx = -1;
r->error = err;
}
+int redisReplyReaderSetPrivdata(void *reader, void *privdata);
char *redisReplyReaderGetError(void *reader) {
redisReader *r = reader;
@@ -454,6 +468,7 @@ int redisReplyReaderGetReply(void *reader, void **reply) {
r->rstack[0].elements = -1;
r->rstack[0].parent = NULL;
r->rstack[0].idx = -1;
+ r->rstack[0].privdata = r->privdata;
r->ridx = 0;
}
diff --git a/hiredis.h b/hiredis.h
index 21098cc..5065043 100644
--- a/hiredis.h
+++ b/hiredis.h
@@ -88,6 +88,7 @@ typedef struct redisReadTask {
int elements; /* number of elements in multibulk container */
void *parent; /* optional pointer to parent object */
int idx; /* index in parent (array) object */
+ void *privdata; /* user-settable arbitrary field */
} redisReadTask;
typedef struct redisReplyObjectFunctions {
@@ -116,6 +117,7 @@ typedef struct redisContext {
void freeReplyObject(void *reply);
void *redisReplyReaderCreate();
int redisReplyReaderSetReplyObjectFunctions(void *reader, redisReplyObjectFunctions *fn);
+int redisReplyReaderSetPrivdata(void *reader, void *privdata);
void *redisReplyReaderGetObject(void *reader);
char *redisReplyReaderGetError(void *reader);
void redisReplyReaderFree(void *ptr);