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 
32 #ifndef __HIREDIS_ASYNC_H
33 #define __HIREDIS_ASYNC_H
34 #include "hiredis.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 struct redisAsyncContext; /* need forward declaration of redisAsyncContext */
41 struct dict; /* dictionary header is included in async.c */
42 
43 /* Reply callback prototype and container */
44 typedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*);
45 typedef struct redisCallback {
46     struct redisCallback *next; /* simple singly linked list */
47     redisCallbackFn *fn;
48     void *privdata;
49 } redisCallback;
50 
51 /* List of callbacks for either regular replies or pub/sub */
52 typedef struct redisCallbackList {
53     redisCallback *head, *tail;
54 } redisCallbackList;
55 
56 /* Connection callback prototypes */
57 typedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status);
58 typedef void (redisConnectCallback)(const struct redisAsyncContext*, int status);
59 
60 /* Context for an async connection to Redis */
61 typedef struct redisAsyncContext {
62     /* Hold the regular context, so it can be realloc'ed. */
63     redisContext c;
64 
65     /* Setup error flags so they can be used directly. */
66     int err;
67     char *errstr;
68 
69     /* Not used by hiredis */
70     void *data;
71 
72     /* Event library data and hooks */
73     struct {
74         void *data;
75 
76         /* Hooks that are called when the library expects to start
77          * reading/writing. These functions should be idempotent. */
78         void (*addRead)(void *privdata);
79         void (*delRead)(void *privdata);
80         void (*addWrite)(void *privdata);
81         void (*delWrite)(void *privdata);
82         void (*cleanup)(void *privdata);
83     } ev;
84 
85     /* Called when either the connection is terminated due to an error or per
86      * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
87     redisDisconnectCallback *onDisconnect;
88 
89     /* Called when the first write event was received. */
90     redisConnectCallback *onConnect;
91 
92     /* Regular command callbacks */
93     redisCallbackList replies;
94 
95     /* Subscription callbacks */
96     struct {
97         redisCallbackList invalid;
98         struct dict *channels;
99         struct dict *patterns;
100     } sub;
101 } redisAsyncContext;
102 
103 /* Functions that proxy to hiredis */
104 redisAsyncContext *redisAsyncConnect(const char *ip, int port);
105 redisAsyncContext *redisAsyncConnectBind(const char *ip, int port, const char *source_addr);
106 redisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port,
107                                                   const char *source_addr);
108 redisAsyncContext *redisAsyncConnectUnix(const char *path);
109 int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn);
110 int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);
111 void redisAsyncDisconnect(redisAsyncContext *ac);
112 void redisAsyncFree(redisAsyncContext *ac);
113 
114 /* Handle read/write events */
115 void redisAsyncHandleRead(redisAsyncContext *ac);
116 void redisAsyncHandleWrite(redisAsyncContext *ac);
117 
118 /* Command functions for an async context. Write the command to the
119  * output buffer and register the provided callback. */
120 #ifdef __GNUC__
121 __attribute__((format(printf, 4, 0)))
122 #endif
123 int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap);
124 #ifdef __GNUC__
125 __attribute__((format(printf, 4, 5)))
126 #endif
127 int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...);
128 int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);
129 int redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif
136