From 3ce8d5b08b5d13f550983048ee89293e9f6897ca Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 31 Dec 2010 11:27:32 +0100 Subject: Change reply processing code to prepare for pub/sub --- async.c | 16 ++++++++++++++-- hiredis.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/async.c b/async.c index 323b489..e262bb5 100644 --- a/async.c +++ b/async.c @@ -247,12 +247,20 @@ void redisProcessCallbacks(redisAsyncContext *ac) { break; } - /* Shift callback and execute it */ - assert(__redisShiftCallback(&ac->replies,&cb) == REDIS_OK); + /* Even if the context is subscribed, pending regular callbacks will + * get a reply before pub/sub messages arrive. */ + if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) { + /* No more regular callbacks, the context *must* be subscribed. */ + assert(c->flags & REDIS_SUBSCRIBED); + + /* TODO: find the right callback for pub/sub message. */ + } + if (cb.fn != NULL) { c->flags |= REDIS_IN_CALLBACK; cb.fn(ac,reply,cb.privdata); c->flags &= ~REDIS_IN_CALLBACK; + c->fn->freeObject(reply); /* Proceed with free'ing when redisAsyncFree() was called. */ if (c->flags & REDIS_FREEING) { @@ -260,6 +268,10 @@ void redisProcessCallbacks(redisAsyncContext *ac) { return; } } else { + /* No callback for this reply. This can either be a NULL callback, + * or there were no callbacks to begin with. Either way, don't + * abort with an error, but simply ignore it because the client + * doesn't know what the server will spit out over the wire. */ c->fn->freeObject(reply); } } diff --git a/hiredis.h b/hiredis.h index 4635280..8c26a8f 100644 --- a/hiredis.h +++ b/hiredis.h @@ -71,6 +71,9 @@ /* Flag that is set when an async callback is executed. */ #define REDIS_IN_CALLBACK 0x10 +/* Flag that is set when the async context has one or more subscriptions. */ +#define REDIS_SUBSCRIBED 0x20 + #define REDIS_REPLY_STRING 1 #define REDIS_REPLY_ARRAY 2 #define REDIS_REPLY_INTEGER 3 -- cgit v1.2.3