hiredis 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +10 -0
- data/Rakefile +77 -0
- data/ext/hiredis_ext/connection.c +215 -0
- data/ext/hiredis_ext/extconf.rb +21 -0
- data/ext/hiredis_ext/hiredis_ext.c +11 -0
- data/ext/hiredis_ext/hiredis_ext.h +38 -0
- data/ext/hiredis_ext/reader.c +140 -0
- data/lib/hiredis.rb +11 -0
- data/lib/hiredis/connection.rb +11 -0
- data/lib/hiredis/version.rb +3 -0
- data/vendor/hiredis/COPYING +10 -0
- data/vendor/hiredis/Makefile +102 -0
- data/vendor/hiredis/async.c +284 -0
- data/vendor/hiredis/async.h +94 -0
- data/vendor/hiredis/fmacros.h +15 -0
- data/vendor/hiredis/hiredis.c +926 -0
- data/vendor/hiredis/hiredis.h +157 -0
- data/vendor/hiredis/net.c +167 -0
- data/vendor/hiredis/net.h +37 -0
- data/vendor/hiredis/sds.c +479 -0
- data/vendor/hiredis/sds.h +77 -0
- data/vendor/hiredis/test.c +400 -0
- data/vendor/hiredis/util.h +40 -0
- metadata +115 -0
data/lib/hiredis.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Copyright (c) 2006-2009, Salvatore Sanfilippo
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
* 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.
|
8
|
+
* 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.
|
9
|
+
|
10
|
+
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.
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# Hiredis Makefile
|
2
|
+
# Copyright (C) 2010 Salvatore Sanfilippo <antirez at gmail dot com>
|
3
|
+
# This file is released under the BSD license, see the COPYING file
|
4
|
+
|
5
|
+
OBJ = net.o hiredis.o sds.o async.o
|
6
|
+
BINS = hiredis-example hiredis-test
|
7
|
+
|
8
|
+
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
|
9
|
+
OPTIMIZATION?=-O2
|
10
|
+
ifeq ($(uname_S),SunOS)
|
11
|
+
CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -D__EXTENSIONS__ -D_XPG6
|
12
|
+
CCLINK?= -ldl -lnsl -lsocket -lm -lpthread
|
13
|
+
DYLIBNAME?=libhiredis.so
|
14
|
+
DYLIB_MAKE_CMD?=gcc -shared -Wl,-soname,${DYLIBNAME} -o ${DYLIBNAME} ${OBJ}
|
15
|
+
STLIBNAME?=libhiredis.a
|
16
|
+
STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ}
|
17
|
+
else ifeq ($(uname_S),Darwin)
|
18
|
+
CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF)
|
19
|
+
CCLINK?= -lm -pthread
|
20
|
+
OBJARCH?= -arch i386 -arch x86_64
|
21
|
+
DYLIBNAME?=libhiredis.dylib
|
22
|
+
DYLIB_MAKE_CMD?=libtool -dynamic -o ${DYLIBNAME} -lm ${DEBUG} - ${OBJ}
|
23
|
+
STLIBNAME?=libhiredis.a
|
24
|
+
STLIB_MAKE_CMD?=libtool -static -o ${STLIBNAME} - ${OBJ}
|
25
|
+
else
|
26
|
+
CFLAGS?= -std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF)
|
27
|
+
CCLINK?= -lm -pthread
|
28
|
+
DYLIBNAME?=libhiredis.so
|
29
|
+
DYLIB_MAKE_CMD?=gcc -shared -Wl,-soname,${DYLIBNAME} -o ${DYLIBNAME} ${OBJ}
|
30
|
+
STLIBNAME?=libhiredis.a
|
31
|
+
STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ}
|
32
|
+
endif
|
33
|
+
CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF)
|
34
|
+
DEBUG?= -g -ggdb
|
35
|
+
|
36
|
+
PREFIX?= /usr/local
|
37
|
+
INSTALL_INC= $(PREFIX)/include/hiredis
|
38
|
+
INSTALL_LIB= $(PREFIX)/lib
|
39
|
+
INSTALL= cp -a
|
40
|
+
|
41
|
+
all: ${DYLIBNAME} ${BINS}
|
42
|
+
|
43
|
+
# Deps (use make dep to generate this)
|
44
|
+
net.o: net.c fmacros.h net.h
|
45
|
+
async.o: async.c async.h hiredis.h sds.h util.h
|
46
|
+
example-libev.o: example-libev.c hiredis.h async.h adapters/libev.h
|
47
|
+
example-libevent.o: example-libevent.c hiredis.h async.h adapters/libevent.h
|
48
|
+
example.o: example.c hiredis.h
|
49
|
+
hiredis.o: hiredis.c hiredis.h net.h sds.h util.h
|
50
|
+
sds.o: sds.c sds.h
|
51
|
+
test.o: test.c hiredis.h
|
52
|
+
|
53
|
+
${DYLIBNAME}: ${OBJ}
|
54
|
+
${DYLIB_MAKE_CMD}
|
55
|
+
|
56
|
+
${STLIBNAME}: ${OBJ}
|
57
|
+
${STLIB_MAKE_CMD}
|
58
|
+
|
59
|
+
dynamic: ${DYLIBNAME}
|
60
|
+
static: ${STLIBNAME}
|
61
|
+
|
62
|
+
# Binaries:
|
63
|
+
hiredis-example-libevent: example-libevent.o ${DYLIBNAME}
|
64
|
+
$(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -levent -Wl,-rpath,. example-libevent.c
|
65
|
+
|
66
|
+
hiredis-example-libev: example-libev.o ${DYLIBNAME}
|
67
|
+
$(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -lev -Wl,-rpath,. example-libev.c
|
68
|
+
|
69
|
+
hiredis-%: %.o ${DYLIBNAME}
|
70
|
+
$(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -Wl,-rpath,. $<
|
71
|
+
|
72
|
+
test: hiredis-test
|
73
|
+
./hiredis-test
|
74
|
+
|
75
|
+
.c.o:
|
76
|
+
$(CC) -c $(CFLAGS) $(OBJARCH) $(DEBUG) $(COMPILE_TIME) $<
|
77
|
+
|
78
|
+
clean:
|
79
|
+
rm -rf ${DYLIBNAME} ${STLIBNAME} $(BINS) hiredis-example* *.o *.gcda *.gcno *.gcov
|
80
|
+
|
81
|
+
dep:
|
82
|
+
$(CC) -MM *.c
|
83
|
+
|
84
|
+
install: ${DYLIBNAME} ${STLIBNAME}
|
85
|
+
mkdir -p $(INSTALL_INC) $(INSTALL_LIB)
|
86
|
+
$(INSTALL) hiredis.h async.h adapters $(INSTALL_INC)
|
87
|
+
$(INSTALL) ${DYLIBNAME} ${STLIBNAME} $(INSTALL_LIB)
|
88
|
+
|
89
|
+
32bit:
|
90
|
+
@echo ""
|
91
|
+
@echo "WARNING: if it fails under Linux you probably need to install libc6-dev-i386"
|
92
|
+
@echo ""
|
93
|
+
make ARCH="-m32"
|
94
|
+
|
95
|
+
gprof:
|
96
|
+
make PROF="-pg"
|
97
|
+
|
98
|
+
gcov:
|
99
|
+
make PROF="-fprofile-arcs -ftest-coverage"
|
100
|
+
|
101
|
+
noopt:
|
102
|
+
make OPTIMIZATION=""
|
@@ -0,0 +1,284 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
7
|
+
*
|
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.
|
16
|
+
*
|
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.
|
28
|
+
*/
|
29
|
+
|
30
|
+
#include <string.h>
|
31
|
+
#include <assert.h>
|
32
|
+
#include "async.h"
|
33
|
+
#include "sds.h"
|
34
|
+
#include "util.h"
|
35
|
+
|
36
|
+
/* Forward declaration of function in hiredis.c */
|
37
|
+
void __redisAppendCommand(redisContext *c, char *cmd, size_t len);
|
38
|
+
|
39
|
+
static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
|
40
|
+
redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext));
|
41
|
+
/* Set all bytes in the async part of the context to 0 */
|
42
|
+
memset(ac+sizeof(redisContext),0,sizeof(redisAsyncContext)-sizeof(redisContext));
|
43
|
+
return ac;
|
44
|
+
}
|
45
|
+
|
46
|
+
/* We want the error field to be accessible directly instead of requiring
|
47
|
+
* an indirection to the redisContext struct. */
|
48
|
+
static void __redisAsyncCopyError(redisAsyncContext *ac) {
|
49
|
+
redisContext *c = &(ac->c);
|
50
|
+
ac->err = c->err;
|
51
|
+
ac->errstr = c->errstr;
|
52
|
+
}
|
53
|
+
|
54
|
+
redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
|
55
|
+
redisContext *c = redisConnectNonBlock(ip,port);
|
56
|
+
redisAsyncContext *ac = redisAsyncInitialize(c);
|
57
|
+
__redisAsyncCopyError(ac);
|
58
|
+
return ac;
|
59
|
+
}
|
60
|
+
|
61
|
+
redisAsyncContext *redisAsyncConnectUnix(const char *path) {
|
62
|
+
redisContext *c = redisConnectUnixNonBlock(path);
|
63
|
+
redisAsyncContext *ac = redisAsyncInitialize(c);
|
64
|
+
__redisAsyncCopyError(ac);
|
65
|
+
return ac;
|
66
|
+
}
|
67
|
+
|
68
|
+
int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFunctions *fn) {
|
69
|
+
redisContext *c = &(ac->c);
|
70
|
+
return redisSetReplyObjectFunctions(c,fn);
|
71
|
+
}
|
72
|
+
|
73
|
+
int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) {
|
74
|
+
if (ac->onDisconnect == NULL) {
|
75
|
+
ac->onDisconnect = fn;
|
76
|
+
return REDIS_OK;
|
77
|
+
}
|
78
|
+
return REDIS_ERR;
|
79
|
+
}
|
80
|
+
|
81
|
+
/* Helper functions to push/shift callbacks */
|
82
|
+
static int __redisPushCallback(redisCallbackList *list, redisCallback *source) {
|
83
|
+
redisCallback *cb;
|
84
|
+
|
85
|
+
/* Copy callback from stack to heap */
|
86
|
+
cb = calloc(1,sizeof(*cb));
|
87
|
+
if (!cb) redisOOM();
|
88
|
+
if (source != NULL) {
|
89
|
+
cb->fn = source->fn;
|
90
|
+
cb->privdata = source->privdata;
|
91
|
+
}
|
92
|
+
|
93
|
+
/* Store callback in list */
|
94
|
+
if (list->head == NULL)
|
95
|
+
list->head = cb;
|
96
|
+
if (list->tail != NULL)
|
97
|
+
list->tail->next = cb;
|
98
|
+
list->tail = cb;
|
99
|
+
return REDIS_OK;
|
100
|
+
}
|
101
|
+
|
102
|
+
static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) {
|
103
|
+
redisCallback *cb = list->head;
|
104
|
+
if (cb != NULL) {
|
105
|
+
list->head = cb->next;
|
106
|
+
if (cb == list->tail)
|
107
|
+
list->tail = NULL;
|
108
|
+
|
109
|
+
/* Copy callback from heap to stack */
|
110
|
+
if (target != NULL)
|
111
|
+
memcpy(target,cb,sizeof(*cb));
|
112
|
+
free(cb);
|
113
|
+
return REDIS_OK;
|
114
|
+
}
|
115
|
+
return REDIS_ERR;
|
116
|
+
}
|
117
|
+
|
118
|
+
/* Tries to do a clean disconnect from Redis, meaning it stops new commands
|
119
|
+
* from being issued, but tries to flush the output buffer and execute
|
120
|
+
* callbacks for all remaining replies.
|
121
|
+
*
|
122
|
+
* This functions is generally called from within a callback, so the
|
123
|
+
* processCallbacks function will pick up the flag when there are no
|
124
|
+
* more replies. */
|
125
|
+
void redisAsyncDisconnect(redisAsyncContext *ac) {
|
126
|
+
redisContext *c = &(ac->c);
|
127
|
+
c->flags |= REDIS_DISCONNECTING;
|
128
|
+
}
|
129
|
+
|
130
|
+
/* Helper function to make the disconnect happen and clean up. */
|
131
|
+
static void __redisAsyncDisconnect(redisAsyncContext *ac) {
|
132
|
+
redisContext *c = &(ac->c);
|
133
|
+
redisCallback cb;
|
134
|
+
int status;
|
135
|
+
|
136
|
+
/* Make sure error is accessible if there is any */
|
137
|
+
__redisAsyncCopyError(ac);
|
138
|
+
status = (ac->err == 0) ? REDIS_OK : REDIS_ERR;
|
139
|
+
|
140
|
+
if (status == REDIS_OK) {
|
141
|
+
/* When the connection is cleanly disconnected, there should not
|
142
|
+
* be pending callbacks. */
|
143
|
+
assert(__redisShiftCallback(&ac->replies,NULL) == REDIS_ERR);
|
144
|
+
} else {
|
145
|
+
/* Callbacks should not be able to issue new commands. */
|
146
|
+
c->flags |= REDIS_DISCONNECTING;
|
147
|
+
|
148
|
+
/* Execute pending callbacks with NULL reply. */
|
149
|
+
while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) {
|
150
|
+
if (cb.fn != NULL)
|
151
|
+
cb.fn(ac,NULL,cb.privdata);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
/* Signal event lib to clean up */
|
156
|
+
if (ac->evCleanup) ac->evCleanup(ac->data);
|
157
|
+
|
158
|
+
/* Execute callback with proper status */
|
159
|
+
if (ac->onDisconnect) ac->onDisconnect(ac,status);
|
160
|
+
|
161
|
+
/* Cleanup self */
|
162
|
+
redisFree(c);
|
163
|
+
}
|
164
|
+
|
165
|
+
void redisProcessCallbacks(redisAsyncContext *ac) {
|
166
|
+
redisContext *c = &(ac->c);
|
167
|
+
redisCallback cb;
|
168
|
+
void *reply = NULL;
|
169
|
+
int status;
|
170
|
+
|
171
|
+
while((status = redisGetReply(c,&reply)) == REDIS_OK) {
|
172
|
+
if (reply == NULL) {
|
173
|
+
/* When the connection is being disconnected and there are
|
174
|
+
* no more replies, this is the cue to really disconnect. */
|
175
|
+
if (c->flags & REDIS_DISCONNECTING && sdslen(c->obuf) == 0) {
|
176
|
+
__redisAsyncDisconnect(ac);
|
177
|
+
return;
|
178
|
+
}
|
179
|
+
|
180
|
+
/* When the connection is not being disconnected, simply stop
|
181
|
+
* trying to get replies and wait for the next loop tick. */
|
182
|
+
break;
|
183
|
+
}
|
184
|
+
|
185
|
+
/* Shift callback and execute it */
|
186
|
+
assert(__redisShiftCallback(&ac->replies,&cb) == REDIS_OK);
|
187
|
+
if (cb.fn != NULL) {
|
188
|
+
cb.fn(ac,reply,cb.privdata);
|
189
|
+
} else {
|
190
|
+
c->fn->freeObject(reply);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
/* Disconnect when there was an error reading the reply */
|
195
|
+
if (status != REDIS_OK)
|
196
|
+
__redisAsyncDisconnect(ac);
|
197
|
+
}
|
198
|
+
|
199
|
+
/* This function should be called when the socket is readable.
|
200
|
+
* It processes all replies that can be read and executes their callbacks.
|
201
|
+
*/
|
202
|
+
void redisAsyncHandleRead(redisAsyncContext *ac) {
|
203
|
+
redisContext *c = &(ac->c);
|
204
|
+
|
205
|
+
if (redisBufferRead(c) == REDIS_ERR) {
|
206
|
+
__redisAsyncDisconnect(ac);
|
207
|
+
} else {
|
208
|
+
/* Always re-schedule reads */
|
209
|
+
if (ac->evAddRead) ac->evAddRead(ac->data);
|
210
|
+
redisProcessCallbacks(ac);
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
void redisAsyncHandleWrite(redisAsyncContext *ac) {
|
215
|
+
redisContext *c = &(ac->c);
|
216
|
+
int done = 0;
|
217
|
+
|
218
|
+
if (redisBufferWrite(c,&done) == REDIS_ERR) {
|
219
|
+
__redisAsyncDisconnect(ac);
|
220
|
+
} else {
|
221
|
+
/* Continue writing when not done, stop writing otherwise */
|
222
|
+
if (!done) {
|
223
|
+
if (ac->evAddWrite) ac->evAddWrite(ac->data);
|
224
|
+
} else {
|
225
|
+
if (ac->evDelWrite) ac->evDelWrite(ac->data);
|
226
|
+
}
|
227
|
+
|
228
|
+
/* Always schedule reads when something was written */
|
229
|
+
if (ac->evAddRead) ac->evAddRead(ac->data);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
/* Helper function for the redisAsyncCommand* family of functions.
|
234
|
+
*
|
235
|
+
* Write a formatted command to the output buffer and register the provided
|
236
|
+
* callback function with the context.
|
237
|
+
*/
|
238
|
+
static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) {
|
239
|
+
redisContext *c = &(ac->c);
|
240
|
+
redisCallback cb;
|
241
|
+
|
242
|
+
/* Don't accept new commands when the connection is lazily closed. */
|
243
|
+
if (c->flags & REDIS_DISCONNECTING) return REDIS_ERR;
|
244
|
+
__redisAppendCommand(c,cmd,len);
|
245
|
+
|
246
|
+
/* Store callback */
|
247
|
+
cb.fn = fn;
|
248
|
+
cb.privdata = privdata;
|
249
|
+
__redisPushCallback(&ac->replies,&cb);
|
250
|
+
|
251
|
+
/* Always schedule a write when the write buffer is non-empty */
|
252
|
+
if (ac->evAddWrite) ac->evAddWrite(ac->data);
|
253
|
+
|
254
|
+
return REDIS_OK;
|
255
|
+
}
|
256
|
+
|
257
|
+
int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) {
|
258
|
+
char *cmd;
|
259
|
+
int len;
|
260
|
+
int status;
|
261
|
+
len = redisvFormatCommand(&cmd,format,ap);
|
262
|
+
status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
|
263
|
+
free(cmd);
|
264
|
+
return status;
|
265
|
+
}
|
266
|
+
|
267
|
+
int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...) {
|
268
|
+
va_list ap;
|
269
|
+
int status;
|
270
|
+
va_start(ap,format);
|
271
|
+
status = redisvAsyncCommand(ac,fn,privdata,format,ap);
|
272
|
+
va_end(ap);
|
273
|
+
return status;
|
274
|
+
}
|
275
|
+
|
276
|
+
int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen) {
|
277
|
+
char *cmd;
|
278
|
+
int len;
|
279
|
+
int status;
|
280
|
+
len = redisFormatCommandArgv(&cmd,argc,argv,argvlen);
|
281
|
+
status = __redisAsyncCommand(ac,fn,privdata,cmd,len);
|
282
|
+
free(cmd);
|
283
|
+
return status;
|
284
|
+
}
|
@@ -0,0 +1,94 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
7
|
+
*
|
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.
|
16
|
+
*
|
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.
|
28
|
+
*/
|
29
|
+
|
30
|
+
#ifndef __HIREDIS_ASYNC_H
|
31
|
+
#define __HIREDIS_ASYNC_H
|
32
|
+
#include "hiredis.h"
|
33
|
+
|
34
|
+
struct redisAsyncContext; /* need forward declaration of redisAsyncContext */
|
35
|
+
|
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 */
|
40
|
+
redisCallbackFn *fn;
|
41
|
+
void *privdata;
|
42
|
+
} redisCallback;
|
43
|
+
|
44
|
+
/* List of callbacks for either regular replies or pub/sub */
|
45
|
+
typedef struct redisCallbackList {
|
46
|
+
redisCallback *head, *tail;
|
47
|
+
} redisCallbackList;
|
48
|
+
|
49
|
+
/* Disconnect callback prototype */
|
50
|
+
typedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status);
|
51
|
+
|
52
|
+
/* Context for an async connection to Redis */
|
53
|
+
typedef struct redisAsyncContext {
|
54
|
+
/* Hold the regular context, so it can be realloc'ed. */
|
55
|
+
redisContext c;
|
56
|
+
|
57
|
+
/* Setup error flags so they can be used directly. */
|
58
|
+
int err;
|
59
|
+
char *errstr;
|
60
|
+
|
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);
|
68
|
+
void *data;
|
69
|
+
|
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;
|
73
|
+
|
74
|
+
/* Reply callbacks */
|
75
|
+
redisCallbackList replies;
|
76
|
+
} redisAsyncContext;
|
77
|
+
|
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);
|
83
|
+
|
84
|
+
/* Handle read/write events */
|
85
|
+
void redisAsyncHandleRead(redisAsyncContext *ac);
|
86
|
+
void redisAsyncHandleWrite(redisAsyncContext *ac);
|
87
|
+
|
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);
|
93
|
+
|
94
|
+
#endif
|