summaryrefslogtreecommitdiff
path: root/hiredis.c
diff options
context:
space:
mode:
Diffstat (limited to 'hiredis.c')
-rw-r--r--hiredis.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/hiredis.c b/hiredis.c
index 29968c9..e59919d 100644
--- a/hiredis.c
+++ b/hiredis.c
@@ -575,7 +575,7 @@ static int redisContextConnect(redisContext *c, const char *ip, int port) {
}
static redisContext *redisContextInit(redisReplyFunctions *fn) {
- redisContext *c = malloc(sizeof(*c));
+ redisContext *c = calloc(sizeof(redisContext),1);
c->error = NULL;
c->obuf = sdsempty();
c->fn = fn == NULL ? &defaultFunctions : fn;
@@ -587,6 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
}
void redisFree(redisContext *c) {
+ if (c->cbFree != NULL)
+ c->cbFree(c,c->privdataFree);
if (c->error != NULL)
sdsfree(c->error);
if (c->obuf != NULL)
@@ -614,6 +616,19 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
return c;
}
+/* Register callback that is triggered when a command is put in the output
+ * buffer when the context is non-blocking. */
+void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata) {
+ c->cbCommand = fn;
+ c->privdataCommand = privdata;
+}
+
+/* Register callback that is triggered when the context is free'd. */
+void redisSetFreeCallback(redisContext *c, redisContextCallback *fn, void *privdata) {
+ c->cbFree = fn;
+ c->privdataFree = privdata;
+}
+
/* Use this function to handle a read event on the descriptor. It will try
* and read some bytes from the socket and feed them to the reply parser.
*
@@ -749,6 +764,10 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
}
c->cpos++;
+ /* Fire write callback */
+ if (c->cbCommand != NULL)
+ c->cbCommand(c,c->privdataCommand);
+
return REDIS_OK;
}