2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
3 * Copyright (c) 2010, 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 */
37 #define HIREDIS_MAJOR 0
38 #define HIREDIS_MINOR 9
39 #define HIREDIS_PATCH 2
44 /* When an error occurs, the err flag in a context is set to hold the type of
45 * error that occured. REDIS_ERR_IO means there was an I/O error and you
46 * should use the "errno" variable to find out what is wrong.
47 * For other values, the "errstr" field will hold a description. */
48 #define REDIS_ERR_IO 1 /* error in read or write */
49 #define REDIS_ERR_EOF 3 /* eof */
50 #define REDIS_ERR_PROTOCOL 4 /* protocol error */
51 #define REDIS_ERR_OTHER 2 /* something else */
53 /* Connection type can be blocking or non-blocking and is set in the
54 * least significant bit of the flags field in redisContext. */
55 #define REDIS_BLOCK 0x1
57 /* Connection may be disconnected before being free'd. The second bit
58 * in the flags field is set when the context is connected. */
59 #define REDIS_CONNECTED 0x2
61 /* The async API might try to disconnect cleanly and flush the output
62 * buffer and read all subsequent replies before disconnecting.
63 * This flag means no new commands can come in and the connection
64 * should be terminated once all replies have been read. */
65 #define REDIS_DISCONNECTING 0x4
67 #define REDIS_REPLY_STRING 1
68 #define REDIS_REPLY_ARRAY 2
69 #define REDIS_REPLY_INTEGER 3
70 #define REDIS_REPLY_NIL 4
71 #define REDIS_REPLY_STATUS 5
72 #define REDIS_REPLY_ERROR 6
78 /* This is the reply object returned by redisCommand() */
79 typedef struct redisReply
{
80 int type
; /* REDIS_REPLY_* */
81 long long integer
; /* The integer when type is REDIS_REPLY_INTEGER */
82 int len
; /* Length of string */
83 char *str
; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
84 size_t elements
; /* number of elements, for REDIS_REPLY_ARRAY */
85 struct redisReply
**element
; /* elements vector for REDIS_REPLY_ARRAY */
88 typedef struct redisReadTask
{
90 int elements
; /* number of elements in multibulk container */
91 int idx
; /* index in parent (array) object */
92 void *obj
; /* holds user-generated value for a read task */
93 struct redisReadTask
*parent
; /* parent task */
94 void *privdata
; /* user-settable arbitrary field */
97 typedef struct redisReplyObjectFunctions
{
98 void *(*createString
)(const redisReadTask
*, char*, size_t);
99 void *(*createArray
)(const redisReadTask
*, int);
100 void *(*createInteger
)(const redisReadTask
*, long long);
101 void *(*createNil
)(const redisReadTask
*);
102 void (*freeObject
)(void*);
103 } redisReplyObjectFunctions
;
105 struct redisContext
; /* need forward declaration of redisContext */
107 /* Context for a connection to Redis */
108 typedef struct redisContext
{
111 char *obuf
; /* Write buffer */
112 int err
; /* Error flags, 0 when there is no error */
113 char *errstr
; /* String representation of error when applicable */
115 /* Function set for reply buildup and reply reader */
116 redisReplyObjectFunctions
*fn
;
120 void freeReplyObject(void *reply
);
121 void *redisReplyReaderCreate();
122 int redisReplyReaderSetReplyObjectFunctions(void *reader
, redisReplyObjectFunctions
*fn
);
123 int redisReplyReaderSetPrivdata(void *reader
, void *privdata
);
124 void *redisReplyReaderGetObject(void *reader
);
125 char *redisReplyReaderGetError(void *reader
);
126 void redisReplyReaderFree(void *ptr
);
127 void redisReplyReaderFeed(void *reader
, char *buf
, size_t len
);
128 int redisReplyReaderGetReply(void *reader
, void **reply
);
130 /* Functions to format a command according to the protocol. */
131 int redisvFormatCommand(char **target
, const char *format
, va_list ap
);
132 int redisFormatCommand(char **target
, const char *format
, ...);
133 int redisFormatCommandArgv(char **target
, int argc
, const char **argv
, const size_t *argvlen
);
135 redisContext
*redisConnect(const char *ip
, int port
);
136 redisContext
*redisConnectNonBlock(const char *ip
, int port
);
137 redisContext
*redisConnectUnix(const char *path
);
138 redisContext
*redisConnectUnixNonBlock(const char *path
);
139 int redisSetReplyObjectFunctions(redisContext
*c
, redisReplyObjectFunctions
*fn
);
140 void redisFree(redisContext
*c
);
141 int redisBufferRead(redisContext
*c
);
142 int redisBufferWrite(redisContext
*c
, int *done
);
144 /* In a blocking context, this function first checks if there are unconsumed
145 * replies to return and returns one if so. Otherwise, it flushes the output
146 * buffer to the socket and reads until it has a reply. In a non-blocking
147 * context, it will return unconsumed replies until there are no more. */
148 int redisGetReply(redisContext
*c
, void **reply
);
149 int redisGetReplyFromReader(redisContext
*c
, void **reply
);
151 /* Write a command to the output buffer. Use these functions in blocking mode
152 * to get a pipeline of commands. */
153 void redisvAppendCommand(redisContext
*c
, const char *format
, va_list ap
);
154 void redisAppendCommand(redisContext
*c
, const char *format
, ...);
155 void redisAppendCommandArgv(redisContext
*c
, int argc
, const char **argv
, const size_t *argvlen
);
157 /* Issue a command to Redis. In a blocking context, it is identical to calling
158 * redisAppendCommand, followed by redisGetReply. The function will return
159 * NULL if there was an error in performing the request, otherwise it will
160 * return the reply. In a non-blocking context, it is identical to calling
161 * only redisAppendCommand and will always return NULL. */
162 void *redisvCommand(redisContext
*c
, const char *format
, va_list ap
);
163 void *redisCommand(redisContext
*c
, const char *format
, ...);
164 void *redisCommandArgv(redisContext
*c
, int argc
, const char **argv
, const size_t *argvlen
);