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
38 struct redisAsyncContext
; /* need forward declaration of redisAsyncContext */
40 /* Reply callback prototype and container */
41 typedef void (redisCallbackFn
)(struct redisAsyncContext
*, void*, void*);
42 typedef struct redisCallback
{
43 struct redisCallback
*next
; /* simple singly linked list */
48 /* List of callbacks for either regular replies or pub/sub */
49 typedef struct redisCallbackList
{
50 redisCallback
*head
, *tail
;
53 /* Connection callback prototypes */
54 typedef void (redisDisconnectCallback
)(const struct redisAsyncContext
*, int status
);
55 typedef void (redisConnectCallback
)(const struct redisAsyncContext
*);
57 /* Context for an async connection to Redis */
58 typedef struct redisAsyncContext
{
59 /* Hold the regular context, so it can be realloc'ed. */
62 /* Setup error flags so they can be used directly. */
66 /* Not used by hiredis */
69 /* Used by the different event lib adapters to store their private data */
72 /* Called when the library expects to start reading/writing.
73 * The supplied functions should be idempotent. */
74 void (*evAddRead
)(void *privdata
);
75 void (*evDelRead
)(void *privdata
);
76 void (*evAddWrite
)(void *privdata
);
77 void (*evDelWrite
)(void *privdata
);
78 void (*evCleanup
)(void *privdata
);
80 /* Called when either the connection is terminated due to an error or per
81 * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
82 redisDisconnectCallback
*onDisconnect
;
84 /* Called when the first write event was received. */
85 redisConnectCallback
*onConnect
;
88 redisCallbackList replies
;
91 /* Functions that proxy to hiredis */
92 redisAsyncContext
*redisAsyncConnect(const char *ip
, int port
);
93 int redisAsyncSetReplyObjectFunctions(redisAsyncContext
*ac
, redisReplyObjectFunctions
*fn
);
94 int redisAsyncSetConnectCallback(redisAsyncContext
*ac
, redisConnectCallback
*fn
);
95 int redisAsyncSetDisconnectCallback(redisAsyncContext
*ac
, redisDisconnectCallback
*fn
);
96 void redisAsyncDisconnect(redisAsyncContext
*ac
);
98 /* Handle read/write events */
99 void redisAsyncHandleRead(redisAsyncContext
*ac
);
100 void redisAsyncHandleWrite(redisAsyncContext
*ac
);
102 /* Command functions for an async context. Write the command to the
103 * output buffer and register the provided callback. */
104 int redisvAsyncCommand(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, const char *format
, va_list ap
);
105 int redisAsyncCommand(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, const char *format
, ...);
106 int redisAsyncCommandArgv(redisAsyncContext
*ac
, redisCallbackFn
*fn
, void *privdata
, int argc
, const char **argv
, const size_t *argvlen
);