2010-10-31 20:20:47 +00:00
|
|
|
/*
|
2011-04-21 20:46:23 +00:00
|
|
|
* Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
2010-12-16 21:08:39 +00:00
|
|
|
*
|
2010-10-31 20:20:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-06-18 14:02:42 +00:00
|
|
|
#include "fmacros.h"
|
2011-07-07 21:22:26 +00:00
|
|
|
#include <stdlib.h>
|
2010-10-31 20:20:47 +00:00
|
|
|
#include <string.h>
|
2019-04-13 02:38:34 +00:00
|
|
|
#ifndef _WIN32
|
2011-01-10 21:44:37 +00:00
|
|
|
#include <strings.h>
|
2019-04-13 02:38:34 +00:00
|
|
|
#else
|
|
|
|
#define strcasecmp stricmp
|
|
|
|
#define strncasecmp strnicmp
|
|
|
|
#endif
|
2010-10-31 20:20:47 +00:00
|
|
|
#include <assert.h>
|
2010-12-31 12:46:08 +00:00
|
|
|
#include <ctype.h>
|
2011-06-27 21:52:29 +00:00
|
|
|
#include <errno.h>
|
2010-10-31 20:20:47 +00:00
|
|
|
#include "async.h"
|
2011-06-27 21:52:29 +00:00
|
|
|
#include "net.h"
|
2011-01-14 11:07:24 +00:00
|
|
|
#include "dict.c"
|
2010-10-31 20:20:47 +00:00
|
|
|
#include "sds.h"
|
2017-11-27 20:49:28 +00:00
|
|
|
#include "sslio.h"
|
2010-10-31 20:20:47 +00:00
|
|
|
|
2018-03-08 09:21:07 +00:00
|
|
|
#define _EL_ADD_READ(ctx) \
|
|
|
|
do { \
|
|
|
|
refreshTimeout(ctx); \
|
2011-06-27 20:49:25 +00:00
|
|
|
if ((ctx)->ev.addRead) (ctx)->ev.addRead((ctx)->ev.data); \
|
2018-03-08 09:21:07 +00:00
|
|
|
} while (0)
|
2011-06-27 20:49:25 +00:00
|
|
|
#define _EL_DEL_READ(ctx) do { \
|
|
|
|
if ((ctx)->ev.delRead) (ctx)->ev.delRead((ctx)->ev.data); \
|
|
|
|
} while(0)
|
2018-03-08 09:21:07 +00:00
|
|
|
#define _EL_ADD_WRITE(ctx) \
|
|
|
|
do { \
|
|
|
|
refreshTimeout(ctx); \
|
2011-06-27 20:49:25 +00:00
|
|
|
if ((ctx)->ev.addWrite) (ctx)->ev.addWrite((ctx)->ev.data); \
|
2018-03-08 09:21:07 +00:00
|
|
|
} while (0)
|
2011-06-27 20:49:25 +00:00
|
|
|
#define _EL_DEL_WRITE(ctx) do { \
|
|
|
|
if ((ctx)->ev.delWrite) (ctx)->ev.delWrite((ctx)->ev.data); \
|
|
|
|
} while(0)
|
|
|
|
#define _EL_CLEANUP(ctx) do { \
|
|
|
|
if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \
|
2019-03-11 13:01:32 +00:00
|
|
|
ctx->ev.cleanup = NULL; \
|
2011-06-27 20:49:25 +00:00
|
|
|
} while(0);
|
|
|
|
|
2018-03-08 09:21:07 +00:00
|
|
|
static void refreshTimeout(redisAsyncContext *ctx) {
|
|
|
|
if (ctx->c.timeout && ctx->ev.scheduleTimer &&
|
|
|
|
(ctx->c.timeout->tv_sec || ctx->c.timeout->tv_usec)) {
|
|
|
|
ctx->ev.scheduleTimer(ctx->ev.data, *ctx->c.timeout);
|
|
|
|
// } else {
|
|
|
|
// printf("Not scheduling timer.. (tmo=%p)\n", ctx->c.timeout);
|
|
|
|
// if (ctx->c.timeout){
|
|
|
|
// printf("tv_sec: %u. tv_usec: %u\n", ctx->c.timeout->tv_sec,
|
|
|
|
// ctx->c.timeout->tv_usec);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 13:16:01 +00:00
|
|
|
/* Forward declaration of function in hiredis.c */
|
2014-08-18 22:24:05 +00:00
|
|
|
int __redisAppendCommand(redisContext *c, const char *cmd, size_t len);
|
2010-11-01 13:16:01 +00:00
|
|
|
|
2010-12-31 12:31:24 +00:00
|
|
|
/* Functions managing dictionary of callbacks for pub/sub. */
|
|
|
|
static unsigned int callbackHash(const void *key) {
|
2013-07-11 06:31:30 +00:00
|
|
|
return dictGenHashFunction((const unsigned char *)key,
|
|
|
|
sdslen((const sds)key));
|
2010-12-31 12:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *callbackValDup(void *privdata, const void *src) {
|
|
|
|
((void) privdata);
|
|
|
|
redisCallback *dup = malloc(sizeof(*dup));
|
|
|
|
memcpy(dup,src,sizeof(*dup));
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) {
|
2013-07-11 06:31:30 +00:00
|
|
|
int l1, l2;
|
2010-12-31 12:31:24 +00:00
|
|
|
((void) privdata);
|
|
|
|
|
2013-07-11 06:31:30 +00:00
|
|
|
l1 = sdslen((const sds)key1);
|
|
|
|
l2 = sdslen((const sds)key2);
|
2010-12-31 12:31:24 +00:00
|
|
|
if (l1 != l2) return 0;
|
|
|
|
return memcmp(key1,key2,l1) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void callbackKeyDestructor(void *privdata, void *key) {
|
|
|
|
((void) privdata);
|
|
|
|
sdsfree((sds)key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void callbackValDestructor(void *privdata, void *val) {
|
|
|
|
((void) privdata);
|
|
|
|
free(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dictType callbackDict = {
|
|
|
|
callbackHash,
|
|
|
|
NULL,
|
|
|
|
callbackValDup,
|
|
|
|
callbackKeyCompare,
|
|
|
|
callbackKeyDestructor,
|
|
|
|
callbackValDestructor
|
|
|
|
};
|
|
|
|
|
2010-10-31 20:20:47 +00:00
|
|
|
static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
|
2013-01-22 14:53:17 +00:00
|
|
|
redisAsyncContext *ac;
|
|
|
|
|
|
|
|
ac = realloc(c,sizeof(redisAsyncContext));
|
|
|
|
if (ac == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-07 09:22:30 +00:00
|
|
|
c = &(ac->c);
|
|
|
|
|
|
|
|
/* The regular connect functions will always set the flag REDIS_CONNECTED.
|
|
|
|
* For the async API, we want to wait until the first write event is
|
|
|
|
* received up before setting this flag, so reset it here. */
|
|
|
|
c->flags &= ~REDIS_CONNECTED;
|
|
|
|
|
2010-11-15 20:53:22 +00:00
|
|
|
ac->err = 0;
|
|
|
|
ac->errstr = NULL;
|
|
|
|
ac->data = NULL;
|
2010-12-07 09:22:30 +00:00
|
|
|
|
2010-12-29 14:41:03 +00:00
|
|
|
ac->ev.data = NULL;
|
|
|
|
ac->ev.addRead = NULL;
|
|
|
|
ac->ev.delRead = NULL;
|
|
|
|
ac->ev.addWrite = NULL;
|
|
|
|
ac->ev.delWrite = NULL;
|
|
|
|
ac->ev.cleanup = NULL;
|
2019-02-11 16:26:15 +00:00
|
|
|
ac->ev.scheduleTimer = NULL;
|
2010-12-07 09:22:30 +00:00
|
|
|
|
|
|
|
ac->onConnect = NULL;
|
2010-11-15 20:53:22 +00:00
|
|
|
ac->onDisconnect = NULL;
|
2010-12-07 09:22:30 +00:00
|
|
|
|
2010-11-15 20:53:22 +00:00
|
|
|
ac->replies.head = NULL;
|
|
|
|
ac->replies.tail = NULL;
|
2010-12-31 12:31:24 +00:00
|
|
|
ac->sub.invalid.head = NULL;
|
|
|
|
ac->sub.invalid.tail = NULL;
|
|
|
|
ac->sub.channels = dictCreate(&callbackDict,NULL);
|
|
|
|
ac->sub.patterns = dictCreate(&callbackDict,NULL);
|
2010-10-31 20:20:47 +00:00
|
|
|
return ac;
|
|
|
|
}
|
|
|
|
|
2010-11-01 08:52:17 +00:00
|
|
|
/* We want the error field to be accessible directly instead of requiring
|
|
|
|
* an indirection to the redisContext struct. */
|
|
|
|
static void __redisAsyncCopyError(redisAsyncContext *ac) {
|
2015-01-05 15:22:22 +00:00
|
|
|
if (!ac)
|
|
|
|
return;
|
|
|
|
|
2010-11-01 08:52:17 +00:00
|
|
|
redisContext *c = &(ac->c);
|
2010-11-02 15:36:38 +00:00
|
|
|
ac->err = c->err;
|
|
|
|
ac->errstr = c->errstr;
|
2010-11-01 08:52:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 09:54:05 +00:00
|
|
|
redisAsyncContext *redisAsyncConnectWithOptions(const redisOptions *options) {
|
|
|
|
redisOptions myOptions = *options;
|
2013-01-22 09:16:30 +00:00
|
|
|
redisContext *c;
|
|
|
|
redisAsyncContext *ac;
|
|
|
|
|
2018-12-06 09:54:05 +00:00
|
|
|
myOptions.options |= REDIS_OPT_NONBLOCK;
|
|
|
|
c = redisConnectWithOptions(&myOptions);
|
|
|
|
if (c == NULL) {
|
2013-01-22 09:16:30 +00:00
|
|
|
return NULL;
|
2018-12-06 09:54:05 +00:00
|
|
|
}
|
2013-01-22 09:16:30 +00:00
|
|
|
ac = redisAsyncInitialize(c);
|
2013-01-22 14:53:17 +00:00
|
|
|
if (ac == NULL) {
|
|
|
|
redisFree(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-11-01 08:52:17 +00:00
|
|
|
__redisAsyncCopyError(ac);
|
2010-11-01 08:27:43 +00:00
|
|
|
return ac;
|
2010-10-31 20:20:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 09:54:05 +00:00
|
|
|
redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
|
|
|
|
redisOptions options = {0};
|
|
|
|
REDIS_OPTIONS_SET_TCP(&options, ip, port);
|
|
|
|
return redisAsyncConnectWithOptions(&options);
|
|
|
|
}
|
|
|
|
|
2014-04-08 17:15:36 +00:00
|
|
|
redisAsyncContext *redisAsyncConnectBind(const char *ip, int port,
|
2014-04-10 05:30:04 +00:00
|
|
|
const char *source_addr) {
|
2018-12-06 09:54:05 +00:00
|
|
|
redisOptions options = {0};
|
|
|
|
REDIS_OPTIONS_SET_TCP(&options, ip, port);
|
|
|
|
options.endpoint.tcp.source_addr = source_addr;
|
|
|
|
return redisAsyncConnectWithOptions(&options);
|
2014-04-08 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 20:24:59 +00:00
|
|
|
redisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port,
|
|
|
|
const char *source_addr) {
|
2018-12-06 09:54:05 +00:00
|
|
|
redisOptions options = {0};
|
|
|
|
REDIS_OPTIONS_SET_TCP(&options, ip, port);
|
|
|
|
options.options |= REDIS_OPT_REUSEADDR;
|
|
|
|
options.endpoint.tcp.source_addr = source_addr;
|
|
|
|
return redisAsyncConnectWithOptions(&options);
|
2014-08-14 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 10:32:35 +00:00
|
|
|
redisAsyncContext *redisAsyncConnectUnix(const char *path) {
|
2018-12-06 09:54:05 +00:00
|
|
|
redisOptions options = {0};
|
|
|
|
REDIS_OPTIONS_SET_UNIX(&options, path);
|
|
|
|
return redisAsyncConnectWithOptions(&options);
|
2010-11-03 10:32:35 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 09:22:30 +00:00
|
|
|
int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) {
|
|
|
|
if (ac->onConnect == NULL) {
|
|
|
|
ac->onConnect = fn;
|
2010-12-28 16:59:26 +00:00
|
|
|
|
|
|
|
/* The common way to detect an established connection is to wait for
|
|
|
|
* the first write event to be fired. This assumes the related event
|
|
|
|
* library functions are already set. */
|
2011-06-27 20:49:25 +00:00
|
|
|
_EL_ADD_WRITE(ac);
|
2010-12-07 09:22:30 +00:00
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
2010-11-01 08:27:43 +00:00
|
|
|
int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) {
|
|
|
|
if (ac->onDisconnect == NULL) {
|
|
|
|
ac->onDisconnect = fn;
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:01:34 +00:00
|
|
|
/* Helper functions to push/shift callbacks */
|
2010-11-01 09:10:03 +00:00
|
|
|
static int __redisPushCallback(redisCallbackList *list, redisCallback *source) {
|
|
|
|
redisCallback *cb;
|
|
|
|
|
|
|
|
/* Copy callback from stack to heap */
|
2010-12-31 10:22:13 +00:00
|
|
|
cb = malloc(sizeof(*cb));
|
2013-01-22 14:53:17 +00:00
|
|
|
if (cb == NULL)
|
|
|
|
return REDIS_ERR_OOM;
|
|
|
|
|
2010-11-01 09:13:05 +00:00
|
|
|
if (source != NULL) {
|
2010-12-31 10:22:13 +00:00
|
|
|
memcpy(cb,source,sizeof(*cb));
|
|
|
|
cb->next = NULL;
|
2010-11-01 09:13:05 +00:00
|
|
|
}
|
2010-11-01 09:10:03 +00:00
|
|
|
|
|
|
|
/* Store callback in list */
|
2010-11-01 09:01:34 +00:00
|
|
|
if (list->head == NULL)
|
|
|
|
list->head = cb;
|
|
|
|
if (list->tail != NULL)
|
|
|
|
list->tail->next = cb;
|
|
|
|
list->tail = cb;
|
2010-11-01 09:10:03 +00:00
|
|
|
return REDIS_OK;
|
2010-11-01 09:01:34 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:10:03 +00:00
|
|
|
static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) {
|
2010-11-01 09:01:34 +00:00
|
|
|
redisCallback *cb = list->head;
|
|
|
|
if (cb != NULL) {
|
|
|
|
list->head = cb->next;
|
|
|
|
if (cb == list->tail)
|
|
|
|
list->tail = NULL;
|
2010-11-01 09:10:03 +00:00
|
|
|
|
|
|
|
/* Copy callback from heap to stack */
|
|
|
|
if (target != NULL)
|
|
|
|
memcpy(target,cb,sizeof(*cb));
|
|
|
|
free(cb);
|
|
|
|
return REDIS_OK;
|
2010-11-01 09:01:34 +00:00
|
|
|
}
|
2010-11-01 09:10:03 +00:00
|
|
|
return REDIS_ERR;
|
2010-11-01 09:01:34 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 13:35:01 +00:00
|
|
|
static void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
if (cb->fn != NULL) {
|
|
|
|
c->flags |= REDIS_IN_CALLBACK;
|
|
|
|
cb->fn(ac,reply,cb->privdata);
|
|
|
|
c->flags &= ~REDIS_IN_CALLBACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 18:19:25 +00:00
|
|
|
/* Helper function to free the context. */
|
|
|
|
static void __redisAsyncFree(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
2010-12-28 19:49:18 +00:00
|
|
|
redisCallback cb;
|
2010-12-31 13:35:01 +00:00
|
|
|
dictIterator *it;
|
|
|
|
dictEntry *de;
|
2010-12-28 18:19:25 +00:00
|
|
|
|
2010-12-28 19:49:18 +00:00
|
|
|
/* Execute pending callbacks with NULL reply. */
|
2010-12-31 13:35:01 +00:00
|
|
|
while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK)
|
|
|
|
__redisRunCallback(ac,&cb,NULL);
|
|
|
|
|
|
|
|
/* Execute callbacks for invalid commands */
|
|
|
|
while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK)
|
|
|
|
__redisRunCallback(ac,&cb,NULL);
|
|
|
|
|
|
|
|
/* Run subscription callbacks callbacks with NULL reply */
|
|
|
|
it = dictGetIterator(ac->sub.channels);
|
|
|
|
while ((de = dictNext(it)) != NULL)
|
|
|
|
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
|
|
|
|
dictReleaseIterator(it);
|
|
|
|
dictRelease(ac->sub.channels);
|
|
|
|
|
|
|
|
it = dictGetIterator(ac->sub.patterns);
|
|
|
|
while ((de = dictNext(it)) != NULL)
|
|
|
|
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
|
|
|
|
dictReleaseIterator(it);
|
|
|
|
dictRelease(ac->sub.patterns);
|
2010-12-28 18:19:25 +00:00
|
|
|
|
|
|
|
/* Signal event lib to clean up */
|
2011-06-27 20:49:25 +00:00
|
|
|
_EL_CLEANUP(ac);
|
2010-12-28 18:19:25 +00:00
|
|
|
|
2010-12-28 19:49:18 +00:00
|
|
|
/* Execute disconnect callback. When redisAsyncFree() initiated destroying
|
|
|
|
* this context, the status will always be REDIS_OK. */
|
|
|
|
if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) {
|
|
|
|
if (c->flags & REDIS_FREEING) {
|
|
|
|
ac->onDisconnect(ac,REDIS_OK);
|
|
|
|
} else {
|
|
|
|
ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR);
|
|
|
|
}
|
|
|
|
}
|
2010-12-28 18:19:25 +00:00
|
|
|
|
|
|
|
/* Cleanup self */
|
|
|
|
redisFree(c);
|
|
|
|
}
|
|
|
|
|
2010-12-28 19:29:26 +00:00
|
|
|
/* Free the async context. When this function is called from a callback,
|
|
|
|
* control needs to be returned to redisProcessCallbacks() before actual
|
|
|
|
* free'ing. To do so, a flag is set on the context which is picked up by
|
|
|
|
* redisProcessCallbacks(). Otherwise, the context is immediately free'd. */
|
2010-12-28 18:19:25 +00:00
|
|
|
void redisAsyncFree(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
2010-12-28 19:49:18 +00:00
|
|
|
c->flags |= REDIS_FREEING;
|
|
|
|
if (!(c->flags & REDIS_IN_CALLBACK))
|
2010-12-28 18:19:25 +00:00
|
|
|
__redisAsyncFree(ac);
|
|
|
|
}
|
|
|
|
|
2010-11-01 08:27:43 +00:00
|
|
|
/* Helper function to make the disconnect happen and clean up. */
|
|
|
|
static void __redisAsyncDisconnect(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
|
2010-11-01 09:01:34 +00:00
|
|
|
/* Make sure error is accessible if there is any */
|
|
|
|
__redisAsyncCopyError(ac);
|
|
|
|
|
2010-12-28 18:19:25 +00:00
|
|
|
if (ac->err == 0) {
|
2010-12-28 19:49:18 +00:00
|
|
|
/* For clean disconnects, there should be no pending callbacks. */
|
2017-06-15 19:33:23 +00:00
|
|
|
int ret = __redisShiftCallback(&ac->replies,NULL);
|
|
|
|
assert(ret == REDIS_ERR);
|
2010-11-01 09:01:34 +00:00
|
|
|
} else {
|
2010-12-28 19:49:18 +00:00
|
|
|
/* Disconnection is caused by an error, make sure that pending
|
|
|
|
* callbacks cannot call new commands. */
|
2010-11-01 09:01:34 +00:00
|
|
|
c->flags |= REDIS_DISCONNECTING;
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:01:32 +00:00
|
|
|
/* cleanup event library on disconnect.
|
|
|
|
* this is safe to call multiple times */
|
|
|
|
_EL_CLEANUP(ac);
|
|
|
|
|
2010-12-28 19:49:18 +00:00
|
|
|
/* For non-clean disconnects, __redisAsyncFree() will execute pending
|
|
|
|
* callbacks with a NULL-reply. */
|
2019-02-21 16:49:25 +00:00
|
|
|
if (!(c->flags & REDIS_NO_AUTO_FREE)) {
|
|
|
|
__redisAsyncFree(ac);
|
|
|
|
}
|
2010-11-01 08:27:43 +00:00
|
|
|
}
|
|
|
|
|
2010-12-28 19:29:26 +00:00
|
|
|
/* Tries to do a clean disconnect from Redis, meaning it stops new commands
|
|
|
|
* from being issued, but tries to flush the output buffer and execute
|
|
|
|
* callbacks for all remaining replies. When this function is called from a
|
|
|
|
* callback, there might be more replies and we can safely defer disconnecting
|
|
|
|
* to redisProcessCallbacks(). Otherwise, we can only disconnect immediately
|
|
|
|
* when there are no pending callbacks. */
|
|
|
|
void redisAsyncDisconnect(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
2010-12-28 19:49:18 +00:00
|
|
|
c->flags |= REDIS_DISCONNECTING;
|
2019-02-21 16:49:25 +00:00
|
|
|
|
|
|
|
/** unset the auto-free flag here, because disconnect undoes this */
|
|
|
|
c->flags &= ~REDIS_NO_AUTO_FREE;
|
2010-12-28 19:49:18 +00:00
|
|
|
if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL)
|
2010-12-28 19:29:26 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
}
|
|
|
|
|
2010-12-31 13:18:11 +00:00
|
|
|
static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
dict *callbacks;
|
2016-09-29 15:11:27 +00:00
|
|
|
redisCallback *cb;
|
2010-12-31 13:18:11 +00:00
|
|
|
dictEntry *de;
|
|
|
|
int pvariant;
|
|
|
|
char *stype;
|
|
|
|
sds sname;
|
|
|
|
|
|
|
|
/* Custom reply functions are not supported for pub/sub. This will fail
|
|
|
|
* very hard when they are used... */
|
|
|
|
if (reply->type == REDIS_REPLY_ARRAY) {
|
|
|
|
assert(reply->elements >= 2);
|
|
|
|
assert(reply->element[0]->type == REDIS_REPLY_STRING);
|
|
|
|
stype = reply->element[0]->str;
|
|
|
|
pvariant = (tolower(stype[0]) == 'p') ? 1 : 0;
|
|
|
|
|
|
|
|
if (pvariant)
|
|
|
|
callbacks = ac->sub.patterns;
|
|
|
|
else
|
|
|
|
callbacks = ac->sub.channels;
|
|
|
|
|
|
|
|
/* Locate the right callback */
|
|
|
|
assert(reply->element[1]->type == REDIS_REPLY_STRING);
|
|
|
|
sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len);
|
|
|
|
de = dictFind(callbacks,sname);
|
|
|
|
if (de != NULL) {
|
2016-09-29 15:11:27 +00:00
|
|
|
cb = dictGetEntryVal(de);
|
|
|
|
|
|
|
|
/* If this is an subscribe reply decrease pending counter. */
|
|
|
|
if (strcasecmp(stype+pvariant,"subscribe") == 0) {
|
|
|
|
cb->pending_subs -= 1;
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:01:01 +00:00
|
|
|
memcpy(dstcb,cb,sizeof(*dstcb));
|
2010-12-31 13:18:11 +00:00
|
|
|
|
|
|
|
/* If this is an unsubscribe message, remove it. */
|
|
|
|
if (strcasecmp(stype+pvariant,"unsubscribe") == 0) {
|
2016-09-29 15:11:27 +00:00
|
|
|
if (cb->pending_subs == 0)
|
|
|
|
dictDelete(callbacks,sname);
|
2010-12-31 13:18:11 +00:00
|
|
|
|
|
|
|
/* If this was the last unsubscribe message, revert to
|
|
|
|
* non-subscribe mode. */
|
2010-12-31 13:40:39 +00:00
|
|
|
assert(reply->element[2]->type == REDIS_REPLY_INTEGER);
|
2016-09-29 15:11:27 +00:00
|
|
|
|
|
|
|
/* Unset subscribed flag only when no pipelined pending subscribe. */
|
2017-01-30 11:00:04 +00:00
|
|
|
if (reply->element[2]->integer == 0
|
|
|
|
&& dictSize(ac->sub.channels) == 0
|
|
|
|
&& dictSize(ac->sub.patterns) == 0)
|
2010-12-31 13:18:11 +00:00
|
|
|
c->flags &= ~REDIS_SUBSCRIBED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sdsfree(sname);
|
|
|
|
} else {
|
|
|
|
/* Shift callback for invalid commands. */
|
|
|
|
__redisShiftCallback(&ac->sub.invalid,dstcb);
|
|
|
|
}
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-01 08:27:43 +00:00
|
|
|
void redisProcessCallbacks(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
2016-09-29 15:11:27 +00:00
|
|
|
redisCallback cb = {NULL, NULL, 0, NULL};
|
2010-11-01 08:27:43 +00:00
|
|
|
void *reply = NULL;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
while((status = redisGetReply(c,&reply)) == REDIS_OK) {
|
|
|
|
if (reply == NULL) {
|
|
|
|
/* When the connection is being disconnected and there are
|
|
|
|
* no more replies, this is the cue to really disconnect. */
|
2015-07-27 21:35:02 +00:00
|
|
|
if (c->flags & REDIS_DISCONNECTING && sdslen(c->obuf) == 0
|
|
|
|
&& ac->replies.head == NULL) {
|
2010-11-01 08:27:43 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 00:38:45 +00:00
|
|
|
|
2012-07-11 07:53:58 +00:00
|
|
|
/* If monitor mode, repush callback */
|
|
|
|
if(c->flags & REDIS_MONITORING) {
|
|
|
|
__redisPushCallback(&ac->replies,&cb);
|
|
|
|
}
|
2010-11-01 08:27:43 +00:00
|
|
|
|
|
|
|
/* When the connection is not being disconnected, simply stop
|
|
|
|
* trying to get replies and wait for the next loop tick. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-31 10:27:32 +00:00
|
|
|
/* 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) {
|
2012-08-01 00:40:46 +00:00
|
|
|
/*
|
|
|
|
* A spontaneous reply in a not-subscribed context can be the error
|
|
|
|
* reply that is sent when a new connection exceeds the maximum
|
|
|
|
* number of allowed connections on the server side.
|
|
|
|
*
|
|
|
|
* This is seen as an error instead of a regular reply because the
|
|
|
|
* server closes the connection after sending it.
|
|
|
|
*
|
|
|
|
* To prevent the error from being overwritten by an EOF error the
|
|
|
|
* connection is closed here. See issue #43.
|
|
|
|
*
|
|
|
|
* Another possibility is that the server is loading its dataset.
|
|
|
|
* In this case we also want to close the connection, and have the
|
|
|
|
* user wait until the server is ready to take our request.
|
|
|
|
*/
|
|
|
|
if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) {
|
2011-05-24 16:59:02 +00:00
|
|
|
c->err = REDIS_ERR_OTHER;
|
2011-05-25 13:28:54 +00:00
|
|
|
snprintf(c->errstr,sizeof(c->errstr),"%s",((redisReply*)reply)->str);
|
2015-04-28 20:00:48 +00:00
|
|
|
c->reader->fn->freeObject(reply);
|
2011-05-24 15:46:19 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-11 07:53:58 +00:00
|
|
|
/* No more regular callbacks and no errors, the context *must* be subscribed or monitoring. */
|
|
|
|
assert((c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING));
|
|
|
|
if(c->flags & REDIS_SUBSCRIBED)
|
|
|
|
__redisGetSubscribeCallback(ac,reply,&cb);
|
2010-12-31 10:27:32 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:10:03 +00:00
|
|
|
if (cb.fn != NULL) {
|
2010-12-31 13:35:01 +00:00
|
|
|
__redisRunCallback(ac,&cb,reply);
|
2011-04-21 13:01:58 +00:00
|
|
|
c->reader->fn->freeObject(reply);
|
2010-12-28 18:35:26 +00:00
|
|
|
|
|
|
|
/* Proceed with free'ing when redisAsyncFree() was called. */
|
|
|
|
if (c->flags & REDIS_FREEING) {
|
|
|
|
__redisAsyncFree(ac);
|
|
|
|
return;
|
|
|
|
}
|
2010-11-01 08:27:43 +00:00
|
|
|
} else {
|
2010-12-31 10:27:32 +00:00
|
|
|
/* 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. */
|
2011-04-21 13:01:58 +00:00
|
|
|
c->reader->fn->freeObject(reply);
|
2010-11-01 08:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disconnect when there was an error reading the reply */
|
|
|
|
if (status != REDIS_OK)
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
}
|
|
|
|
|
2011-06-27 21:52:29 +00:00
|
|
|
/* Internal helper function to detect socket status the first time a read or
|
2015-10-27 17:19:24 +00:00
|
|
|
* write event fires. When connecting was not successful, the connect callback
|
2011-06-27 21:52:29 +00:00
|
|
|
* is called with a REDIS_ERR status and the context is free'd. */
|
|
|
|
static int __redisAsyncHandleConnect(redisAsyncContext *ac) {
|
2018-03-04 16:17:16 +00:00
|
|
|
int completed = 0;
|
2011-06-27 21:52:29 +00:00
|
|
|
redisContext *c = &(ac->c);
|
2018-03-05 10:17:49 +00:00
|
|
|
if (redisCheckConnectDone(c, &completed) == REDIS_ERR) {
|
2018-03-04 16:17:16 +00:00
|
|
|
/* Error! */
|
|
|
|
redisCheckSocketError(c);
|
|
|
|
if (ac->onConnect) ac->onConnect(ac, REDIS_ERR);
|
2011-06-27 21:52:29 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return REDIS_ERR;
|
2018-03-04 16:17:16 +00:00
|
|
|
} else if (completed == 1) {
|
|
|
|
/* connected! */
|
|
|
|
if (ac->onConnect) ac->onConnect(ac, REDIS_OK);
|
|
|
|
c->flags |= REDIS_CONNECTED;
|
|
|
|
return REDIS_OK;
|
|
|
|
} else {
|
|
|
|
return REDIS_OK;
|
2011-06-27 21:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:49:28 +00:00
|
|
|
/**
|
|
|
|
* Handle SSL when socket becomes available for reading. This also handles
|
2018-01-09 11:57:17 +00:00
|
|
|
* read-while-write and write-while-read.
|
|
|
|
*
|
|
|
|
* These functions will not work properly unless `HIREDIS_SSL` is defined
|
|
|
|
* (however, they will compile)
|
2017-11-27 20:49:28 +00:00
|
|
|
*/
|
|
|
|
static void asyncSslRead(redisAsyncContext *ac) {
|
|
|
|
int rv;
|
|
|
|
redisSsl *ssl = ac->c.ssl;
|
|
|
|
redisContext *c = &ac->c;
|
|
|
|
|
|
|
|
ssl->wantRead = 0;
|
|
|
|
|
|
|
|
if (ssl->pendingWrite) {
|
|
|
|
int done;
|
|
|
|
|
|
|
|
/* This is probably just a write event */
|
|
|
|
ssl->pendingWrite = 0;
|
|
|
|
rv = redisBufferWrite(c, &done);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
} else if (!done) {
|
|
|
|
_EL_ADD_WRITE(ac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = redisBufferRead(c);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
} else {
|
|
|
|
_EL_ADD_READ(ac);
|
|
|
|
redisProcessCallbacks(ac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle SSL when socket becomes available for writing
|
|
|
|
*/
|
|
|
|
static void asyncSslWrite(redisAsyncContext *ac) {
|
|
|
|
int rv, done = 0;
|
|
|
|
redisSsl *ssl = ac->c.ssl;
|
|
|
|
redisContext *c = &ac->c;
|
|
|
|
|
|
|
|
ssl->pendingWrite = 0;
|
|
|
|
rv = redisBufferWrite(c, &done);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
if (ssl->wantRead) {
|
|
|
|
/* Need to read-before-write */
|
|
|
|
ssl->pendingWrite = 1;
|
|
|
|
_EL_DEL_WRITE(ac);
|
|
|
|
} else {
|
|
|
|
/* No extra reads needed, just need to write more */
|
|
|
|
_EL_ADD_WRITE(ac);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Already done! */
|
|
|
|
_EL_DEL_WRITE(ac);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always reschedule a read */
|
|
|
|
_EL_ADD_READ(ac);
|
|
|
|
}
|
|
|
|
|
2010-10-31 20:20:47 +00:00
|
|
|
/* This function should be called when the socket is readable.
|
|
|
|
* It processes all replies that can be read and executes their callbacks.
|
|
|
|
*/
|
|
|
|
void redisAsyncHandleRead(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
|
2011-06-27 21:52:29 +00:00
|
|
|
if (!(c->flags & REDIS_CONNECTED)) {
|
|
|
|
/* Abort connect was not successful. */
|
|
|
|
if (__redisAsyncHandleConnect(ac) != REDIS_OK)
|
|
|
|
return;
|
|
|
|
/* Try again later when the context is still not connected. */
|
|
|
|
if (!(c->flags & REDIS_CONNECTED))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:49:28 +00:00
|
|
|
if (c->flags & REDIS_SSL) {
|
|
|
|
asyncSslRead(ac);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-31 20:20:47 +00:00
|
|
|
if (redisBufferRead(c) == REDIS_ERR) {
|
2010-11-01 08:27:43 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
} else {
|
|
|
|
/* Always re-schedule reads */
|
2011-06-27 20:49:25 +00:00
|
|
|
_EL_ADD_READ(ac);
|
2010-11-01 08:53:34 +00:00
|
|
|
redisProcessCallbacks(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisAsyncHandleWrite(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
int done = 0;
|
|
|
|
|
2011-06-27 21:52:29 +00:00
|
|
|
if (!(c->flags & REDIS_CONNECTED)) {
|
|
|
|
/* Abort connect was not successful. */
|
|
|
|
if (__redisAsyncHandleConnect(ac) != REDIS_OK)
|
|
|
|
return;
|
|
|
|
/* Try again later when the context is still not connected. */
|
|
|
|
if (!(c->flags & REDIS_CONNECTED))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:49:28 +00:00
|
|
|
if (c->flags & REDIS_SSL) {
|
|
|
|
asyncSslWrite(ac);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-31 20:20:47 +00:00
|
|
|
if (redisBufferWrite(c,&done) == REDIS_ERR) {
|
2010-11-01 08:27:43 +00:00
|
|
|
__redisAsyncDisconnect(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
} else {
|
|
|
|
/* Continue writing when not done, stop writing otherwise */
|
2011-06-27 20:49:25 +00:00
|
|
|
if (!done)
|
|
|
|
_EL_ADD_WRITE(ac);
|
|
|
|
else
|
|
|
|
_EL_DEL_WRITE(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
|
2010-12-07 09:22:30 +00:00
|
|
|
/* Always schedule reads after writes */
|
2011-06-27 20:49:25 +00:00
|
|
|
_EL_ADD_READ(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:07 +00:00
|
|
|
void __redisSetError(redisContext *c, int type, const char *str);
|
|
|
|
|
|
|
|
void redisAsyncHandleTimeout(redisAsyncContext *ac) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
redisCallback cb;
|
|
|
|
|
|
|
|
if ((c->flags & REDIS_CONNECTED) && ac->replies.head == NULL) {
|
|
|
|
/* Nothing to do - just an idle timeout */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c->err) {
|
|
|
|
__redisSetError(c, REDIS_ERR_TIMEOUT, "Timeout");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(c->flags & REDIS_CONNECTED) && ac->onConnect) {
|
|
|
|
ac->onConnect(ac, REDIS_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (__redisShiftCallback(&ac->replies, &cb) == REDIS_OK) {
|
|
|
|
__redisRunCallback(ac, &cb, NULL);
|
|
|
|
}
|
2019-03-11 13:01:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Don't automatically sever the connection,
|
|
|
|
* rather, allow to ignore <x> responses before the queue is clear
|
|
|
|
*/
|
|
|
|
__redisAsyncDisconnect(ac);
|
2018-03-08 09:21:07 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 12:46:08 +00:00
|
|
|
/* Sets a pointer to the first argument and its length starting at p. Returns
|
|
|
|
* the number of bytes to skip to get to the following argument. */
|
2014-08-18 22:24:05 +00:00
|
|
|
static const char *nextArgument(const char *start, const char **str, size_t *len) {
|
|
|
|
const char *p = start;
|
2010-12-31 12:46:08 +00:00
|
|
|
if (p[0] != '$') {
|
|
|
|
p = strchr(p,'$');
|
|
|
|
if (p == NULL) return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = (int)strtol(p+1,NULL,10);
|
|
|
|
p = strchr(p,'\r');
|
|
|
|
assert(p);
|
|
|
|
*str = p+2;
|
|
|
|
return p+2+(*len)+2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for the redisAsyncCommand* family of functions. Writes a
|
|
|
|
* formatted command to the output buffer and registers the provided callback
|
|
|
|
* function with the context. */
|
2014-08-18 22:24:05 +00:00
|
|
|
static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) {
|
2010-10-31 20:20:47 +00:00
|
|
|
redisContext *c = &(ac->c);
|
2010-11-01 09:10:03 +00:00
|
|
|
redisCallback cb;
|
2016-09-29 15:11:27 +00:00
|
|
|
struct dict *cbdict;
|
|
|
|
dictEntry *de;
|
|
|
|
redisCallback *existcb;
|
2010-12-31 12:46:08 +00:00
|
|
|
int pvariant, hasnext;
|
2014-08-18 22:24:05 +00:00
|
|
|
const char *cstr, *astr;
|
2010-12-31 12:46:08 +00:00
|
|
|
size_t clen, alen;
|
2014-08-18 22:24:05 +00:00
|
|
|
const char *p;
|
2010-12-31 12:46:08 +00:00
|
|
|
sds sname;
|
2014-12-08 23:45:56 +00:00
|
|
|
int ret;
|
2010-11-01 08:27:43 +00:00
|
|
|
|
2010-12-28 18:19:25 +00:00
|
|
|
/* Don't accept new commands when the connection is about to be closed. */
|
|
|
|
if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;
|
2010-10-31 20:20:47 +00:00
|
|
|
|
2010-12-31 12:46:08 +00:00
|
|
|
/* Setup callback */
|
2010-11-01 09:10:03 +00:00
|
|
|
cb.fn = fn;
|
|
|
|
cb.privdata = privdata;
|
2016-09-29 15:11:27 +00:00
|
|
|
cb.pending_subs = 1;
|
2010-12-31 12:46:08 +00:00
|
|
|
|
|
|
|
/* Find out which command will be appended. */
|
|
|
|
p = nextArgument(cmd,&cstr,&clen);
|
|
|
|
assert(p != NULL);
|
|
|
|
hasnext = (p[0] == '$');
|
|
|
|
pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;
|
|
|
|
cstr += pvariant;
|
|
|
|
clen -= pvariant;
|
|
|
|
|
|
|
|
if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) {
|
|
|
|
c->flags |= REDIS_SUBSCRIBED;
|
|
|
|
|
|
|
|
/* Add every channel/pattern to the list of subscription callbacks. */
|
|
|
|
while ((p = nextArgument(p,&astr,&alen)) != NULL) {
|
|
|
|
sname = sdsnewlen(astr,alen);
|
|
|
|
if (pvariant)
|
2016-09-29 15:11:27 +00:00
|
|
|
cbdict = ac->sub.patterns;
|
2010-12-31 12:46:08 +00:00
|
|
|
else
|
2016-09-29 15:11:27 +00:00
|
|
|
cbdict = ac->sub.channels;
|
|
|
|
|
|
|
|
de = dictFind(cbdict,sname);
|
|
|
|
|
|
|
|
if (de != NULL) {
|
|
|
|
existcb = dictGetEntryVal(de);
|
|
|
|
cb.pending_subs = existcb->pending_subs + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dictReplace(cbdict,sname,&cb);
|
2014-12-08 23:45:56 +00:00
|
|
|
|
|
|
|
if (ret == 0) sdsfree(sname);
|
2010-12-31 12:46:08 +00:00
|
|
|
}
|
|
|
|
} else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) {
|
|
|
|
/* It is only useful to call (P)UNSUBSCRIBE when the context is
|
|
|
|
* subscribed to one or more channels or patterns. */
|
|
|
|
if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;
|
|
|
|
|
|
|
|
/* (P)UNSUBSCRIBE does not have its own response: every channel or
|
|
|
|
* pattern that is unsubscribed will receive a message. This means we
|
|
|
|
* should not append a callback function for this command. */
|
2012-07-11 07:53:58 +00:00
|
|
|
} else if(strncasecmp(cstr,"monitor\r\n",9) == 0) {
|
|
|
|
/* Set monitor flag and push callback */
|
|
|
|
c->flags |= REDIS_MONITORING;
|
|
|
|
__redisPushCallback(&ac->replies,&cb);
|
2010-12-31 12:46:08 +00:00
|
|
|
} else {
|
|
|
|
if (c->flags & REDIS_SUBSCRIBED)
|
|
|
|
/* This will likely result in an error reply, but it needs to be
|
|
|
|
* received and passed to the callback. */
|
|
|
|
__redisPushCallback(&ac->sub.invalid,&cb);
|
|
|
|
else
|
|
|
|
__redisPushCallback(&ac->replies,&cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
__redisAppendCommand(c,cmd,len);
|
2010-10-31 20:20:47 +00:00
|
|
|
|
|
|
|
/* Always schedule a write when the write buffer is non-empty */
|
2011-06-27 20:49:25 +00:00
|
|
|
_EL_ADD_WRITE(ac);
|
2010-10-31 20:20:47 +00:00
|
|
|
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) {
|
|
|
|
char *cmd;
|
|
|
|
int len;
|
|
|
|
int status;
|
|
|
|
len = redisvFormatCommand(&cmd,format,ap);
|
2015-01-05 17:13:32 +00:00
|
|
|
|
|
|
|
/* We don't want to pass -1 or -2 to future functions as a length. */
|
|
|
|
if (len < 0)
|
|
|
|
return REDIS_ERR;
|
|
|
|
|
2010-10-31 20:20:47 +00:00
|
|
|
status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
|
|
|
|
free(cmd);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
int status;
|
|
|
|
va_start(ap,format);
|
|
|
|
status = redisvAsyncCommand(ac,fn,privdata,format,ap);
|
|
|
|
va_end(ap);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen) {
|
2014-08-01 23:18:36 +00:00
|
|
|
sds cmd;
|
2010-10-31 20:20:47 +00:00
|
|
|
int len;
|
|
|
|
int status;
|
2014-08-01 23:18:36 +00:00
|
|
|
len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen);
|
2017-05-15 16:19:21 +00:00
|
|
|
if (len < 0)
|
|
|
|
return REDIS_ERR;
|
2010-10-31 20:20:47 +00:00
|
|
|
status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
|
2014-08-01 23:18:36 +00:00
|
|
|
sdsfree(cmd);
|
2010-10-31 20:20:47 +00:00
|
|
|
return status;
|
|
|
|
}
|
2014-08-18 22:24:05 +00:00
|
|
|
|
|
|
|
int redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) {
|
|
|
|
int status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
|
|
|
|
return status;
|
|
|
|
}
|
2018-03-08 09:21:07 +00:00
|
|
|
|
|
|
|
void redisAsyncSetTimeout(redisAsyncContext *ac, struct timeval tv) {
|
|
|
|
if (!ac->c.timeout) {
|
|
|
|
ac->c.timeout = calloc(1, sizeof(tv));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tv.tv_sec == ac->c.timeout->tv_sec &&
|
|
|
|
tv.tv_usec == ac->c.timeout->tv_usec) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ac->c.timeout = tv;
|
2019-02-11 16:26:15 +00:00
|
|
|
}
|