Fire callback when the context is being disconnected by the user

This commit is contained in:
Pieter Noordhuis 2010-10-07 17:36:22 +02:00
parent e5dd32d809
commit 9cd3b03d84
2 changed files with 23 additions and 0 deletions

View File

@ -586,6 +586,13 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
return c; return c;
} }
void redisDisconnect(redisContext *c) {
if (c->cbDisconnect != NULL)
c->cbDisconnect(c,c->privdataDisconnect);
close(c->fd);
c->flags &= ~REDIS_CONNECTED;
}
void redisFree(redisContext *c) { void redisFree(redisContext *c) {
if (c->cbFree != NULL) if (c->cbFree != NULL)
c->cbFree(c,c->privdataFree); c->cbFree(c,c->privdataFree);
@ -605,6 +612,7 @@ void redisFree(redisContext *c) {
redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn) { redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn) {
redisContext *c = redisContextInit(fn); redisContext *c = redisContextInit(fn);
c->flags |= REDIS_BLOCK; c->flags |= REDIS_BLOCK;
c->flags |= REDIS_CONNECTED;
redisContextConnect(c,ip,port); redisContextConnect(c,ip,port);
return c; return c;
} }
@ -612,10 +620,17 @@ redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn) {
redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions *fn) { redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions *fn) {
redisContext *c = redisContextInit(fn); redisContext *c = redisContextInit(fn);
c->flags &= ~REDIS_BLOCK; c->flags &= ~REDIS_BLOCK;
c->flags |= REDIS_CONNECTED;
redisContextConnect(c,ip,port); redisContextConnect(c,ip,port);
return c; return c;
} }
/* Register callback that is triggered when redisDisconnect is called. */
void redisSetDisconnectCallback(redisContext *c, redisContextCallback *fn, void *privdata) {
c->cbDisconnect = fn;
c->privdataDisconnect = privdata;
}
/* Register callback that is triggered when a command is put in the output /* Register callback that is triggered when a command is put in the output
* buffer when the context is non-blocking. */ * buffer when the context is non-blocking. */
void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata) { void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata) {

View File

@ -37,6 +37,10 @@
* least significant bit of the flags field in redisContext. */ * least significant bit of the flags field in redisContext. */
#define REDIS_BLOCK 0x1 #define REDIS_BLOCK 0x1
/* Connection may be disconnected before being free'd. The second bit
* in the flags field is set when the context is connected. */
#define REDIS_CONNECTED 0x2
#define REDIS_ERROR -1 #define REDIS_ERROR -1
#define REDIS_REPLY_ERROR 0 #define REDIS_REPLY_ERROR 0
#define REDIS_REPLY_STRING 1 #define REDIS_REPLY_STRING 1
@ -94,6 +98,8 @@ typedef struct redisContext {
void *reader; void *reader;
/* Non-reply callbacks */ /* Non-reply callbacks */
redisContextCallback *cbDisconnect;
void *privdataDisconnect;
redisContextCallback *cbCommand; redisContextCallback *cbCommand;
void *privdataCommand; void *privdataCommand;
redisContextCallback *cbFree; redisContextCallback *cbFree;
@ -115,8 +121,10 @@ int redisReplyReaderGetReply(void *reader, void **reply);
redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn); redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn);
redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions *fn); redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions *fn);
void redisSetDisconnectCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata); void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisSetFreeCallback(redisContext *c, redisContextCallback *fn, void *privdata); void redisSetFreeCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisDisconnect(redisContext *c);
void redisFree(redisContext *c); void redisFree(redisContext *c);
int redisBufferRead(redisContext *c); int redisBufferRead(redisContext *c);
int redisBufferWrite(redisContext *c, int *done); int redisBufferWrite(redisContext *c, int *done);