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

View File

@ -77,10 +77,17 @@ typedef struct redisReplyObjectFunctions {
struct redisContext; /* need forward declaration of redisContext */ struct redisContext; /* need forward declaration of redisContext */
/* Callbacks triggered on non-reply events. */ /* Callbacks triggered on non-reply events. */
typedef void (redisContextCallback)(struct redisContext*, void*); typedef void (redisContextCallbackFn)(struct redisContext*, const void*);
/* Reply callback prototype and container */ /* 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 { typedef struct redisCallback {
redisCallbackFn *fn; redisCallbackFn *fn;
const void *privdata; const void *privdata;
@ -98,12 +105,9 @@ typedef struct redisContext {
void *reader; void *reader;
/* Non-reply callbacks */ /* Non-reply callbacks */
redisContextCallback *cbDisconnect; redisContextCallback cbDisconnect;
void *privdataDisconnect; redisContextCallback cbCommand;
redisContextCallback *cbCommand; redisContextCallback cbFree;
void *privdataCommand;
redisContextCallback *cbFree;
void *privdataFree;
/* Reply callbacks */ /* Reply callbacks */
redisCallback *callbacks; redisCallback *callbacks;
@ -131,16 +135,16 @@ int redisProcessCallbacks(redisContext *c);
/* The disconnect callback is called *immediately* when redisDisconnect() /* The disconnect callback is called *immediately* when redisDisconnect()
* is called. It is called only once for every redisContext (since hiredis * is called. It is called only once for every redisContext (since hiredis
* currently does not support reconnecting an existing context). */ * 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 /* The command callback is called every time redisCommand() is called in a
* non-blocking context. It is called *after* the formatted command has been * non-blocking context. It is called *after* the formatted command has been
* appended to the write buffer. */ * 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 /* 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. */ * 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 /* 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 * an error occurs, it returns NULL and you should read redisContext->error