async.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of Redis nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "fmacros.h"
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <strings.h>
  35. #include <assert.h>
  36. #include <ctype.h>
  37. #include <errno.h>
  38. #include "async.h"
  39. #include "net.h"
  40. #include "dict.c"
  41. #include "sds.h"
  42. #define _EL_ADD_READ(ctx) do { \
  43. if ((ctx)->ev.addRead) (ctx)->ev.addRead((ctx)->ev.data); \
  44. } while(0)
  45. #define _EL_DEL_READ(ctx) do { \
  46. if ((ctx)->ev.delRead) (ctx)->ev.delRead((ctx)->ev.data); \
  47. } while(0)
  48. #define _EL_ADD_WRITE(ctx) do { \
  49. if ((ctx)->ev.addWrite) (ctx)->ev.addWrite((ctx)->ev.data); \
  50. } while(0)
  51. #define _EL_DEL_WRITE(ctx) do { \
  52. if ((ctx)->ev.delWrite) (ctx)->ev.delWrite((ctx)->ev.data); \
  53. } while(0)
  54. #define _EL_CLEANUP(ctx) do { \
  55. if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \
  56. } while(0);
  57. /* Forward declaration of function in hiredis.c */
  58. void __redisAppendCommand(redisContext *c, char *cmd, size_t len);
  59. /* Functions managing dictionary of callbacks for pub/sub. */
  60. static unsigned int callbackHash(const void *key) {
  61. return dictGenHashFunction((unsigned char*)key,sdslen((char*)key));
  62. }
  63. static void *callbackValDup(void *privdata, const void *src) {
  64. ((void) privdata);
  65. redisCallback *dup = malloc(sizeof(*dup));
  66. memcpy(dup,src,sizeof(*dup));
  67. return dup;
  68. }
  69. static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) {
  70. int l1, l2;
  71. ((void) privdata);
  72. l1 = sdslen((sds)key1);
  73. l2 = sdslen((sds)key2);
  74. if (l1 != l2) return 0;
  75. return memcmp(key1,key2,l1) == 0;
  76. }
  77. static void callbackKeyDestructor(void *privdata, void *key) {
  78. ((void) privdata);
  79. sdsfree((sds)key);
  80. }
  81. static void callbackValDestructor(void *privdata, void *val) {
  82. ((void) privdata);
  83. free(val);
  84. }
  85. static dictType callbackDict = {
  86. callbackHash,
  87. NULL,
  88. callbackValDup,
  89. callbackKeyCompare,
  90. callbackKeyDestructor,
  91. callbackValDestructor
  92. };
  93. static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
  94. redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext));
  95. c = &(ac->c);
  96. /* The regular connect functions will always set the flag REDIS_CONNECTED.
  97. * For the async API, we want to wait until the first write event is
  98. * received up before setting this flag, so reset it here. */
  99. c->flags &= ~REDIS_CONNECTED;
  100. ac->err = 0;
  101. ac->errstr = NULL;
  102. ac->data = NULL;
  103. ac->ev.data = NULL;
  104. ac->ev.addRead = NULL;
  105. ac->ev.delRead = NULL;
  106. ac->ev.addWrite = NULL;
  107. ac->ev.delWrite = NULL;
  108. ac->ev.cleanup = NULL;
  109. ac->onConnect = NULL;
  110. ac->onDisconnect = NULL;
  111. ac->replies.head = NULL;
  112. ac->replies.tail = NULL;
  113. ac->sub.invalid.head = NULL;
  114. ac->sub.invalid.tail = NULL;
  115. ac->sub.channels = dictCreate(&callbackDict,NULL);
  116. ac->sub.patterns = dictCreate(&callbackDict,NULL);
  117. return ac;
  118. }
  119. /* We want the error field to be accessible directly instead of requiring
  120. * an indirection to the redisContext struct. */
  121. static void __redisAsyncCopyError(redisAsyncContext *ac) {
  122. redisContext *c = &(ac->c);
  123. ac->err = c->err;
  124. ac->errstr = c->errstr;
  125. }
  126. redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
  127. redisContext *c = redisConnectNonBlock(ip,port);
  128. redisAsyncContext *ac = redisAsyncInitialize(c);
  129. __redisAsyncCopyError(ac);
  130. return ac;
  131. }
  132. redisAsyncContext *redisAsyncConnectUnix(const char *path) {
  133. redisContext *c = redisConnectUnixNonBlock(path);
  134. redisAsyncContext *ac = redisAsyncInitialize(c);
  135. __redisAsyncCopyError(ac);
  136. return ac;
  137. }
  138. int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) {
  139. if (ac->onConnect == NULL) {
  140. ac->onConnect = fn;
  141. /* The common way to detect an established connection is to wait for
  142. * the first write event to be fired. This assumes the related event
  143. * library functions are already set. */
  144. _EL_ADD_WRITE(ac);
  145. return REDIS_OK;
  146. }
  147. return REDIS_ERR;
  148. }
  149. int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) {
  150. if (ac->onDisconnect == NULL) {
  151. ac->onDisconnect = fn;
  152. return REDIS_OK;
  153. }
  154. return REDIS_ERR;
  155. }
  156. /* Helper functions to push/shift callbacks */
  157. static int __redisPushCallback(redisCallbackList *list, redisCallback *source) {
  158. redisCallback *cb;
  159. /* Copy callback from stack to heap */
  160. cb = malloc(sizeof(*cb));
  161. if (source != NULL) {
  162. memcpy(cb,source,sizeof(*cb));
  163. cb->next = NULL;
  164. }
  165. /* Store callback in list */
  166. if (list->head == NULL)
  167. list->head = cb;
  168. if (list->tail != NULL)
  169. list->tail->next = cb;
  170. list->tail = cb;
  171. return REDIS_OK;
  172. }
  173. static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) {
  174. redisCallback *cb = list->head;
  175. if (cb != NULL) {
  176. list->head = cb->next;
  177. if (cb == list->tail)
  178. list->tail = NULL;
  179. /* Copy callback from heap to stack */
  180. if (target != NULL)
  181. memcpy(target,cb,sizeof(*cb));
  182. free(cb);
  183. return REDIS_OK;
  184. }
  185. return REDIS_ERR;
  186. }
  187. static void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) {
  188. redisContext *c = &(ac->c);
  189. if (cb->fn != NULL) {
  190. c->flags |= REDIS_IN_CALLBACK;
  191. cb->fn(ac,reply,cb->privdata);
  192. c->flags &= ~REDIS_IN_CALLBACK;
  193. }
  194. }
  195. /* Helper function to free the context. */
  196. static void __redisAsyncFree(redisAsyncContext *ac) {
  197. redisContext *c = &(ac->c);
  198. redisCallback cb;
  199. dictIterator *it;
  200. dictEntry *de;
  201. /* Execute pending callbacks with NULL reply. */
  202. while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK)
  203. __redisRunCallback(ac,&cb,NULL);
  204. /* Execute callbacks for invalid commands */
  205. while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK)
  206. __redisRunCallback(ac,&cb,NULL);
  207. /* Run subscription callbacks callbacks with NULL reply */
  208. it = dictGetIterator(ac->sub.channels);
  209. while ((de = dictNext(it)) != NULL)
  210. __redisRunCallback(ac,dictGetEntryVal(de),NULL);
  211. dictReleaseIterator(it);
  212. dictRelease(ac->sub.channels);
  213. it = dictGetIterator(ac->sub.patterns);
  214. while ((de = dictNext(it)) != NULL)
  215. __redisRunCallback(ac,dictGetEntryVal(de),NULL);
  216. dictReleaseIterator(it);
  217. dictRelease(ac->sub.patterns);
  218. /* Signal event lib to clean up */
  219. _EL_CLEANUP(ac);
  220. /* Execute disconnect callback. When redisAsyncFree() initiated destroying
  221. * this context, the status will always be REDIS_OK. */
  222. if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) {
  223. if (c->flags & REDIS_FREEING) {
  224. ac->onDisconnect(ac,REDIS_OK);
  225. } else {
  226. ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR);
  227. }
  228. }
  229. /* Cleanup self */
  230. redisFree(c);
  231. }
  232. /* Free the async context. When this function is called from a callback,
  233. * control needs to be returned to redisProcessCallbacks() before actual
  234. * free'ing. To do so, a flag is set on the context which is picked up by
  235. * redisProcessCallbacks(). Otherwise, the context is immediately free'd. */
  236. void redisAsyncFree(redisAsyncContext *ac) {
  237. redisContext *c = &(ac->c);
  238. c->flags |= REDIS_FREEING;
  239. if (!(c->flags & REDIS_IN_CALLBACK))
  240. __redisAsyncFree(ac);
  241. }
  242. /* Helper function to make the disconnect happen and clean up. */
  243. static void __redisAsyncDisconnect(redisAsyncContext *ac) {
  244. redisContext *c = &(ac->c);
  245. /* Make sure error is accessible if there is any */
  246. __redisAsyncCopyError(ac);
  247. if (ac->err == 0) {
  248. /* For clean disconnects, there should be no pending callbacks. */
  249. assert(__redisShiftCallback(&ac->replies,NULL) == REDIS_ERR);
  250. } else {
  251. /* Disconnection is caused by an error, make sure that pending
  252. * callbacks cannot call new commands. */
  253. c->flags |= REDIS_DISCONNECTING;
  254. }
  255. /* For non-clean disconnects, __redisAsyncFree() will execute pending
  256. * callbacks with a NULL-reply. */
  257. __redisAsyncFree(ac);
  258. }
  259. /* Tries to do a clean disconnect from Redis, meaning it stops new commands
  260. * from being issued, but tries to flush the output buffer and execute
  261. * callbacks for all remaining replies. When this function is called from a
  262. * callback, there might be more replies and we can safely defer disconnecting
  263. * to redisProcessCallbacks(). Otherwise, we can only disconnect immediately
  264. * when there are no pending callbacks. */
  265. void redisAsyncDisconnect(redisAsyncContext *ac) {
  266. redisContext *c = &(ac->c);
  267. c->flags |= REDIS_DISCONNECTING;
  268. if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL)
  269. __redisAsyncDisconnect(ac);
  270. }
  271. static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) {
  272. redisContext *c = &(ac->c);
  273. dict *callbacks;
  274. dictEntry *de;
  275. int pvariant;
  276. char *stype;
  277. sds sname;
  278. /* Custom reply functions are not supported for pub/sub. This will fail
  279. * very hard when they are used... */
  280. if (reply->type == REDIS_REPLY_ARRAY) {
  281. assert(reply->elements >= 2);
  282. assert(reply->element[0]->type == REDIS_REPLY_STRING);
  283. stype = reply->element[0]->str;
  284. pvariant = (tolower(stype[0]) == 'p') ? 1 : 0;
  285. if (pvariant)
  286. callbacks = ac->sub.patterns;
  287. else
  288. callbacks = ac->sub.channels;
  289. /* Locate the right callback */
  290. assert(reply->element[1]->type == REDIS_REPLY_STRING);
  291. sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len);
  292. de = dictFind(callbacks,sname);
  293. if (de != NULL) {
  294. memcpy(dstcb,dictGetEntryVal(de),sizeof(*dstcb));
  295. /* If this is an unsubscribe message, remove it. */
  296. if (strcasecmp(stype+pvariant,"unsubscribe") == 0) {
  297. dictDelete(callbacks,sname);
  298. /* If this was the last unsubscribe message, revert to
  299. * non-subscribe mode. */
  300. assert(reply->element[2]->type == REDIS_REPLY_INTEGER);
  301. if (reply->element[2]->integer == 0)
  302. c->flags &= ~REDIS_SUBSCRIBED;
  303. }
  304. }
  305. sdsfree(sname);
  306. } else {
  307. /* Shift callback for invalid commands. */
  308. __redisShiftCallback(&ac->sub.invalid,dstcb);
  309. }
  310. return REDIS_OK;
  311. }
  312. void redisProcessCallbacks(redisAsyncContext *ac) {
  313. redisContext *c = &(ac->c);
  314. redisCallback cb;
  315. void *reply = NULL;
  316. int status;
  317. while((status = redisGetReply(c,&reply)) == REDIS_OK) {
  318. if (reply == NULL) {
  319. /* When the connection is being disconnected and there are
  320. * no more replies, this is the cue to really disconnect. */
  321. if (c->flags & REDIS_DISCONNECTING && sdslen(c->obuf) == 0) {
  322. __redisAsyncDisconnect(ac);
  323. return;
  324. }
  325. /* If monitor mode, repush callback */
  326. if(c->flags & REDIS_MONITORING) {
  327. __redisPushCallback(&ac->replies,&cb);
  328. }
  329. /* When the connection is not being disconnected, simply stop
  330. * trying to get replies and wait for the next loop tick. */
  331. break;
  332. }
  333. /* Even if the context is subscribed, pending regular callbacks will
  334. * get a reply before pub/sub messages arrive. */
  335. if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) {
  336. /*
  337. * A spontaneous reply in a not-subscribed context can be the error
  338. * reply that is sent when a new connection exceeds the maximum
  339. * number of allowed connections on the server side.
  340. *
  341. * This is seen as an error instead of a regular reply because the
  342. * server closes the connection after sending it.
  343. *
  344. * To prevent the error from being overwritten by an EOF error the
  345. * connection is closed here. See issue #43.
  346. *
  347. * Another possibility is that the server is loading its dataset.
  348. * In this case we also want to close the connection, and have the
  349. * user wait until the server is ready to take our request.
  350. */
  351. if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) {
  352. c->err = REDIS_ERR_OTHER;
  353. snprintf(c->errstr,sizeof(c->errstr),"%s",((redisReply*)reply)->str);
  354. __redisAsyncDisconnect(ac);
  355. return;
  356. }
  357. /* No more regular callbacks and no errors, the context *must* be subscribed or monitoring. */
  358. assert((c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING));
  359. if(c->flags & REDIS_SUBSCRIBED)
  360. __redisGetSubscribeCallback(ac,reply,&cb);
  361. }
  362. if (cb.fn != NULL) {
  363. __redisRunCallback(ac,&cb,reply);
  364. c->reader->fn->freeObject(reply);
  365. /* Proceed with free'ing when redisAsyncFree() was called. */
  366. if (c->flags & REDIS_FREEING) {
  367. __redisAsyncFree(ac);
  368. return;
  369. }
  370. } else {
  371. /* No callback for this reply. This can either be a NULL callback,
  372. * or there were no callbacks to begin with. Either way, don't
  373. * abort with an error, but simply ignore it because the client
  374. * doesn't know what the server will spit out over the wire. */
  375. c->reader->fn->freeObject(reply);
  376. }
  377. }
  378. /* Disconnect when there was an error reading the reply */
  379. if (status != REDIS_OK)
  380. __redisAsyncDisconnect(ac);
  381. }
  382. /* Internal helper function to detect socket status the first time a read or
  383. * write event fires. When connecting was not succesful, the connect callback
  384. * is called with a REDIS_ERR status and the context is free'd. */
  385. static int __redisAsyncHandleConnect(redisAsyncContext *ac) {
  386. redisContext *c = &(ac->c);
  387. if (redisCheckSocketError(c,c->fd) == REDIS_ERR) {
  388. /* Try again later when connect(2) is still in progress. */
  389. if (errno == EINPROGRESS)
  390. return REDIS_OK;
  391. if (ac->onConnect) ac->onConnect(ac,REDIS_ERR);
  392. __redisAsyncDisconnect(ac);
  393. return REDIS_ERR;
  394. }
  395. /* Mark context as connected. */
  396. c->flags |= REDIS_CONNECTED;
  397. if (ac->onConnect) ac->onConnect(ac,REDIS_OK);
  398. return REDIS_OK;
  399. }
  400. /* This function should be called when the socket is readable.
  401. * It processes all replies that can be read and executes their callbacks.
  402. */
  403. void redisAsyncHandleRead(redisAsyncContext *ac) {
  404. redisContext *c = &(ac->c);
  405. if (!(c->flags & REDIS_CONNECTED)) {
  406. /* Abort connect was not successful. */
  407. if (__redisAsyncHandleConnect(ac) != REDIS_OK)
  408. return;
  409. /* Try again later when the context is still not connected. */
  410. if (!(c->flags & REDIS_CONNECTED))
  411. return;
  412. }
  413. if (redisBufferRead(c) == REDIS_ERR) {
  414. __redisAsyncDisconnect(ac);
  415. } else {
  416. /* Always re-schedule reads */
  417. _EL_ADD_READ(ac);
  418. redisProcessCallbacks(ac);
  419. }
  420. }
  421. void redisAsyncHandleWrite(redisAsyncContext *ac) {
  422. redisContext *c = &(ac->c);
  423. int done = 0;
  424. if (!(c->flags & REDIS_CONNECTED)) {
  425. /* Abort connect was not successful. */
  426. if (__redisAsyncHandleConnect(ac) != REDIS_OK)
  427. return;
  428. /* Try again later when the context is still not connected. */
  429. if (!(c->flags & REDIS_CONNECTED))
  430. return;
  431. }
  432. if (redisBufferWrite(c,&done) == REDIS_ERR) {
  433. __redisAsyncDisconnect(ac);
  434. } else {
  435. /* Continue writing when not done, stop writing otherwise */
  436. if (!done)
  437. _EL_ADD_WRITE(ac);
  438. else
  439. _EL_DEL_WRITE(ac);
  440. /* Always schedule reads after writes */
  441. _EL_ADD_READ(ac);
  442. }
  443. }
  444. /* Sets a pointer to the first argument and its length starting at p. Returns
  445. * the number of bytes to skip to get to the following argument. */
  446. static char *nextArgument(char *start, char **str, size_t *len) {
  447. char *p = start;
  448. if (p[0] != '$') {
  449. p = strchr(p,'$');
  450. if (p == NULL) return NULL;
  451. }
  452. *len = (int)strtol(p+1,NULL,10);
  453. p = strchr(p,'\r');
  454. assert(p);
  455. *str = p+2;
  456. return p+2+(*len)+2;
  457. }
  458. /* Helper function for the redisAsyncCommand* family of functions. Writes a
  459. * formatted command to the output buffer and registers the provided callback
  460. * function with the context. */
  461. static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) {
  462. redisContext *c = &(ac->c);
  463. redisCallback cb;
  464. int pvariant, hasnext;
  465. char *cstr, *astr;
  466. size_t clen, alen;
  467. char *p;
  468. sds sname;
  469. /* Don't accept new commands when the connection is about to be closed. */
  470. if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;
  471. /* Setup callback */
  472. cb.fn = fn;
  473. cb.privdata = privdata;
  474. /* Find out which command will be appended. */
  475. p = nextArgument(cmd,&cstr,&clen);
  476. assert(p != NULL);
  477. hasnext = (p[0] == '$');
  478. pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;
  479. cstr += pvariant;
  480. clen -= pvariant;
  481. if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) {
  482. c->flags |= REDIS_SUBSCRIBED;
  483. /* Add every channel/pattern to the list of subscription callbacks. */
  484. while ((p = nextArgument(p,&astr,&alen)) != NULL) {
  485. sname = sdsnewlen(astr,alen);
  486. if (pvariant)
  487. dictReplace(ac->sub.patterns,sname,&cb);
  488. else
  489. dictReplace(ac->sub.channels,sname,&cb);
  490. }
  491. } else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) {
  492. /* It is only useful to call (P)UNSUBSCRIBE when the context is
  493. * subscribed to one or more channels or patterns. */
  494. if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;
  495. /* (P)UNSUBSCRIBE does not have its own response: every channel or
  496. * pattern that is unsubscribed will receive a message. This means we
  497. * should not append a callback function for this command. */
  498. } else if(strncasecmp(cstr,"monitor\r\n",9) == 0) {
  499. /* Set monitor flag and push callback */
  500. c->flags |= REDIS_MONITORING;
  501. __redisPushCallback(&ac->replies,&cb);
  502. } else {
  503. if (c->flags & REDIS_SUBSCRIBED)
  504. /* This will likely result in an error reply, but it needs to be
  505. * received and passed to the callback. */
  506. __redisPushCallback(&ac->sub.invalid,&cb);
  507. else
  508. __redisPushCallback(&ac->replies,&cb);
  509. }
  510. __redisAppendCommand(c,cmd,len);
  511. /* Always schedule a write when the write buffer is non-empty */
  512. _EL_ADD_WRITE(ac);
  513. return REDIS_OK;
  514. }
  515. int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) {
  516. char *cmd;
  517. int len;
  518. int status;
  519. len = redisvFormatCommand(&cmd,format,ap);
  520. status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
  521. free(cmd);
  522. return status;
  523. }
  524. int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...) {
  525. va_list ap;
  526. int status;
  527. va_start(ap,format);
  528. status = redisvAsyncCommand(ac,fn,privdata,format,ap);
  529. va_end(ap);
  530. return status;
  531. }
  532. int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen) {
  533. char *cmd;
  534. int len;
  535. int status;
  536. len = redisFormatCommandArgv(&cmd,argc,argv,argvlen);
  537. status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
  538. free(cmd);
  539. return status;
  540. }