2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #ifndef __HIREDIS_ASYNC_H
31 #define __HIREDIS_ASYNC_H
34 struct redisAsyncContext
; /* need forward declaration of redisAsyncContext */
36 /* Reply callback prototype and container */
37 typedef void (redisCallbackFn
)(struct redisAsyncContext
*, void*, void*);
38 typedef struct redisCallback
{
39 struct redisCallback
*next
; /* simple singly linked list */
44 /* List of callbacks for either regular replies or pub/sub */
45 typedef struct redisCallbackList
{
46 redisCallback
*head
, *tail
;
49 /* Disconnect callback prototype */
50 typedef void (redisDisconnectCallback
)(const struct redisAsyncContext
*, int status
);
52 /* Context for an async connection to Redis */
53 typedef struct redisAsyncContext
{
54 /* Hold the regular context, so it can be realloc'ed. */
57 /* Setup error flags so they can be used directly. */
61 /* Called when the library expects to start reading/writing.
62 * The supplied functions should be idempotent. */
63 void (*evAddRead
)(void *privdata
);
64 void (*evDelRead
)(void *privdata
);
65 void (*evAddWrite
)(void *privdata
);
66 void (*evDelWrite
)(void *privdata
);
67 void (*evCleanup
)(void *privdata
);
70 /* Called when either the connection is terminated due to an error or per
71 * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
72 redisDisconnectCallback
*onDisconnect
;
75 redisCallbackList replies
;
78 /* Functions that proxy to hiredis */
79 redisAsyncContext
*redisAsyncConnect(const char *ip
, int port
);
80 int redisAsyncSetReplyObjectFunctions(redisAsyncContext
*ac
, redisReplyObjectFunctions
*fn
);
81 int redisAsyncSetDisconnectCallback(redisAsyncContext
*ac
, redisDisconnectCallback
*fn
);
82 void redisAsyncDisconnect(redisAsyncContext
*ac
);
84 /* Handle read/write events */
85 void redisAsyncHandleRead(redisAsyncContext
*ac
);
86 void redisAsyncHandleWrite(redisAsyncContext
*ac
);
88 /* Command functions for an async context. Write the command to the
89 * output buffer and register the provided callback. */
90 int redisvAsyncCommand(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, const char *format
, va_list ap
);
91 int redisAsyncCommand(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, const char *format
, ...);
92 int redisAsyncCommandArgv(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, int argc
, const char **argv
, const size_t *argvlen
);