2 * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
3 * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
34 #include <stdio.h> /* for size_t */
35 #include <stdarg.h> /* for va_list */
36 #include <sys/time.h> /* for struct timeval */
38 #define HIREDIS_MAJOR 0
39 #define HIREDIS_MINOR 10
40 #define HIREDIS_PATCH 1
45 /* When an error occurs, the err flag in a context is set to hold the type of
46 * error that occured. REDIS_ERR_IO means there was an I/O error and you
47 * should use the "errno" variable to find out what is wrong.
48 * For other values, the "errstr" field will hold a description. */
49 #define REDIS_ERR_IO 1 /* Error in read or write */
50 #define REDIS_ERR_EOF 3 /* End of file */
51 #define REDIS_ERR_PROTOCOL 4 /* Protocol error */
52 #define REDIS_ERR_OOM 5 /* Out of memory */
53 #define REDIS_ERR_OTHER 2 /* Everything else... */
55 /* Connection type can be blocking or non-blocking and is set in the
56 * least significant bit of the flags field in redisContext. */
57 #define REDIS_BLOCK 0x1
59 /* Connection may be disconnected before being free'd. The second bit
60 * in the flags field is set when the context is connected. */
61 #define REDIS_CONNECTED 0x2
63 /* The async API might try to disconnect cleanly and flush the output
64 * buffer and read all subsequent replies before disconnecting.
65 * This flag means no new commands can come in and the connection
66 * should be terminated once all replies have been read. */
67 #define REDIS_DISCONNECTING 0x4
69 /* Flag specific to the async API which means that the context should be clean
70 * up as soon as possible. */
71 #define REDIS_FREEING 0x8
73 /* Flag that is set when an async callback is executed. */
74 #define REDIS_IN_CALLBACK 0x10
76 /* Flag that is set when the async context has one or more subscriptions. */
77 #define REDIS_SUBSCRIBED 0x20
79 #define REDIS_REPLY_STRING 1
80 #define REDIS_REPLY_ARRAY 2
81 #define REDIS_REPLY_INTEGER 3
82 #define REDIS_REPLY_NIL 4
83 #define REDIS_REPLY_STATUS 5
84 #define REDIS_REPLY_ERROR 6
90 /* This is the reply object returned by redisCommand() */
91 typedef struct redisReply
{
92 int type
; /* REDIS_REPLY_* */
93 long long integer
; /* The integer when type is REDIS_REPLY_INTEGER */
94 int len
; /* Length of string */
95 char *str
; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
96 size_t elements
; /* number of elements, for REDIS_REPLY_ARRAY */
97 struct redisReply
**element
; /* elements vector for REDIS_REPLY_ARRAY */
100 typedef struct redisReadTask
{
102 int elements
; /* number of elements in multibulk container */
103 int idx
; /* index in parent (array) object */
104 void *obj
; /* holds user-generated value for a read task */
105 struct redisReadTask
*parent
; /* parent task */
106 void *privdata
; /* user-settable arbitrary field */
109 typedef struct redisReplyObjectFunctions
{
110 void *(*createString
)(const redisReadTask
*, char*, size_t);
111 void *(*createArray
)(const redisReadTask
*, int);
112 void *(*createInteger
)(const redisReadTask
*, long long);
113 void *(*createNil
)(const redisReadTask
*);
114 void (*freeObject
)(void*);
115 } redisReplyObjectFunctions
;
117 /* State for the protocol parser */
118 typedef struct redisReader
{
119 int err
; /* Error flags, 0 when there is no error */
120 char errstr
[128]; /* String representation of error when applicable */
122 char *buf
; /* Read buffer */
123 size_t pos
; /* Buffer cursor */
124 size_t len
; /* Buffer length */
126 redisReadTask rstack
[4];
127 int ridx
; /* Index of current read task */
128 void *reply
; /* Temporary reply pointer */
130 redisReplyObjectFunctions
*fn
;
134 /* Public API for the protocol parser. */
135 redisReader
*redisReaderCreate(void);
136 void redisReaderFree(redisReader
*r
);
137 int redisReaderFeed(redisReader
*r
, const char *buf
, size_t len
);
138 int redisReaderGetReply(redisReader
*r
, void **reply
);
140 /* Backwards compatibility, can be removed on big version bump. */
141 #define redisReplyReaderCreate redisReaderCreate
142 #define redisReplyReaderFree redisReaderFree
143 #define redisReplyReaderFeed redisReaderFeed
144 #define redisReplyReaderGetReply redisReaderGetReply
145 #define redisReplyReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p))
146 #define redisReplyReaderGetObject(_r) (((redisReader*)(_r))->reply)
147 #define redisReplyReaderGetError(_r) (((redisReader*)(_r))->errstr)
149 /* Function to free the reply objects hiredis returns by default. */
150 void freeReplyObject(void *reply
);
152 /* Functions to format a command according to the protocol. */
153 int redisvFormatCommand(char **target
, const char *format
, va_list ap
);
154 int redisFormatCommand(char **target
, const char *format
, ...);
155 int redisFormatCommandArgv(char **target
, int argc
, const char **argv
, const size_t *argvlen
);
157 /* Context for a connection to Redis */
158 typedef struct redisContext
{
159 int err
; /* Error flags, 0 when there is no error */
160 char errstr
[128]; /* String representation of error when applicable */
163 char *obuf
; /* Write buffer */
164 redisReader
*reader
; /* Protocol reader */
167 redisContext
*redisConnect(const char *ip
, int port
);
168 redisContext
*redisConnectWithTimeout(const char *ip
, int port
, struct timeval tv
);
169 redisContext
*redisConnectNonBlock(const char *ip
, int port
);
170 redisContext
*redisConnectUnix(const char *path
);
171 redisContext
*redisConnectUnixWithTimeout(const char *path
, struct timeval tv
);
172 redisContext
*redisConnectUnixNonBlock(const char *path
);
173 int redisSetTimeout(redisContext
*c
, struct timeval tv
);
174 void redisFree(redisContext
*c
);
175 int redisBufferRead(redisContext
*c
);
176 int redisBufferWrite(redisContext
*c
, int *done
);
178 /* In a blocking context, this function first checks if there are unconsumed
179 * replies to return and returns one if so. Otherwise, it flushes the output
180 * buffer to the socket and reads until it has a reply. In a non-blocking
181 * context, it will return unconsumed replies until there are no more. */
182 int redisGetReply(redisContext
*c
, void **reply
);
183 int redisGetReplyFromReader(redisContext
*c
, void **reply
);
185 /* Write a command to the output buffer. Use these functions in blocking mode
186 * to get a pipeline of commands. */
187 int redisvAppendCommand(redisContext
*c
, const char *format
, va_list ap
);
188 int redisAppendCommand(redisContext
*c
, const char *format
, ...);
189 int redisAppendCommandArgv(redisContext
*c
, int argc
, const char **argv
, const size_t *argvlen
);
191 /* Issue a command to Redis. In a blocking context, it is identical to calling
192 * redisAppendCommand, followed by redisGetReply. The function will return
193 * NULL if there was an error in performing the request, otherwise it will
194 * return the reply. In a non-blocking context, it is identical to calling
195 * only redisAppendCommand and will always return NULL. */
196 void *redisvCommand(redisContext
*c
, const char *format
, va_list ap
);
197 void *redisCommand(redisContext
*c
, const char *format
, ...);
198 void *redisCommandArgv(redisContext
*c
, int argc
, const char **argv
, const size_t *argvlen
);