Move context callbacks into struct and make privdata const

This commit is contained in:
Pieter Noordhuis 2010-10-18 12:37:33 +02:00
parent 7496458c68
commit 61ba85ce57
2 changed files with 30 additions and 26 deletions

View File

@ -587,8 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
}
void redisDisconnect(redisContext *c) {
if (c->cbDisconnect != NULL)
c->cbDisconnect(c,c->privdataDisconnect);
if (c->cbDisconnect.fn != NULL)
c->cbDisconnect.fn(c,c->cbDisconnect.privdata);
close(c->fd);
c->flags &= ~REDIS_CONNECTED;
}
@ -599,8 +599,8 @@ void redisFree(redisContext *c) {
redisDisconnect(c);
/* Fire free callback and clear all allocations. */
if (c->cbFree != NULL)
c->cbFree(c,c->privdataFree);
if (c->cbFree.fn != NULL)
c->cbFree.fn(c,c->cbFree.privdata);
if (c->error != NULL)
sdsfree(c->error);
if (c->obuf != NULL)
@ -631,22 +631,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
}
/* Register callback that is triggered when redisDisconnect is called. */
void redisSetDisconnectCallback(redisContext *c, redisContextCallback *fn, void *privdata) {
c->cbDisconnect = fn;
c->privdataDisconnect = privdata;
void redisSetDisconnectCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata) {
c->cbDisconnect.fn = fn;
c->cbDisconnect.privdata = privdata;
}
/* 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;
void redisSetCommandCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata) {
c->cbCommand.fn = fn;
c->cbCommand.privdata = 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;
void redisSetFreeCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata) {
c->cbFree.fn = fn;
c->cbFree.privdata = privdata;
}
/* Use this function to handle a read event on the descriptor. It will try
@ -785,8 +785,8 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
c->cpos++;
/* Fire write callback */
if (c->cbCommand != NULL)
c->cbCommand(c,c->privdataCommand);
if (c->cbCommand.fn != NULL)
c->cbCommand.fn(c,c->cbCommand.privdata);
return REDIS_OK;
}

View File

@ -77,10 +77,17 @@ typedef struct redisReplyObjectFunctions {
struct redisContext; /* need forward declaration of redisContext */
/* Callbacks triggered on non-reply events. */
typedef void (redisContextCallback)(struct redisContext*, void*);
typedef void (redisContextCallbackFn)(struct redisContext*, const void*);
/* Reply callback prototype and container */
typedef void redisCallbackFn(struct redisContext*, redisReply*, const void*);
typedef void (redisCallbackFn)(struct redisContext*, redisReply*, const void*);
/* Callback containers */
typedef struct redisContextCallback {
redisContextCallbackFn *fn;
const void *privdata;
} redisContextCallback;
typedef struct redisCallback {
redisCallbackFn *fn;
const void *privdata;
@ -98,12 +105,9 @@ typedef struct redisContext {
void *reader;
/* Non-reply callbacks */
redisContextCallback *cbDisconnect;
void *privdataDisconnect;
redisContextCallback *cbCommand;
void *privdataCommand;
redisContextCallback *cbFree;
void *privdataFree;
redisContextCallback cbDisconnect;
redisContextCallback cbCommand;
redisContextCallback cbFree;
/* Reply callbacks */
redisCallback *callbacks;
@ -131,16 +135,16 @@ int redisProcessCallbacks(redisContext *c);
/* The disconnect callback is called *immediately* when redisDisconnect()
* is called. It is called only once for every redisContext (since hiredis
* currently does not support reconnecting an existing context). */
void redisSetDisconnectCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisSetDisconnectCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata);
/* The command callback is called every time redisCommand() is called in a
* non-blocking context. It is called *after* the formatted command has been
* appended to the write buffer. */
void redisSetCommandCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisSetCommandCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata);
/* The free callback is called *before* all allocations are free'd. Use it to
* release resources that depend/use the redisContext that is being free'd. */
void redisSetFreeCallback(redisContext *c, redisContextCallback *fn, void *privdata);
void redisSetFreeCallback(redisContext *c, redisContextCallbackFn *fn, const void *privdata);
/* Issue a command to Redis. In a blocking context, it returns the reply. When
* an error occurs, it returns NULL and you should read redisContext->error