hiredis.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  4. * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,
  5. * Jan-Erik Rediger <janerik at fnordig dot com>
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * * Neither the name of Redis nor the names of its contributors may be used
  18. * to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef __HIREDIS_H
  34. #define __HIREDIS_H
  35. #include "read.h"
  36. #include <stdarg.h> /* for va_list */
  37. #ifndef _MSC_VER
  38. #include <sys/time.h> /* for struct timeval */
  39. #else
  40. struct timeval; /* forward declaration */
  41. #endif
  42. #include <stdint.h> /* uintXX_t, etc */
  43. #include "sds.h" /* for sds */
  44. #define HIREDIS_MAJOR 0
  45. #define HIREDIS_MINOR 14
  46. #define HIREDIS_PATCH 0
  47. #define HIREDIS_SONAME 0.14
  48. /* Connection type can be blocking or non-blocking and is set in the
  49. * least significant bit of the flags field in redisContext. */
  50. #define REDIS_BLOCK 0x1
  51. /* Connection may be disconnected before being free'd. The second bit
  52. * in the flags field is set when the context is connected. */
  53. #define REDIS_CONNECTED 0x2
  54. /* The async API might try to disconnect cleanly and flush the output
  55. * buffer and read all subsequent replies before disconnecting.
  56. * This flag means no new commands can come in and the connection
  57. * should be terminated once all replies have been read. */
  58. #define REDIS_DISCONNECTING 0x4
  59. /* Flag specific to the async API which means that the context should be clean
  60. * up as soon as possible. */
  61. #define REDIS_FREEING 0x8
  62. /* Flag that is set when an async callback is executed. */
  63. #define REDIS_IN_CALLBACK 0x10
  64. /* Flag that is set when the async context has one or more subscriptions. */
  65. #define REDIS_SUBSCRIBED 0x20
  66. /* Flag that is set when monitor mode is active */
  67. #define REDIS_MONITORING 0x40
  68. /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
  69. #define REDIS_REUSEADDR 0x80
  70. /**
  71. * Flag that indicates the user does not want the context to
  72. * be automatically freed upon error
  73. */
  74. #define REDIS_NO_AUTO_FREE 0x200
  75. #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
  76. /* number of times we retry to connect in the case of EADDRNOTAVAIL and
  77. * SO_REUSEADDR is being used. */
  78. #define REDIS_CONNECT_RETRIES 10
  79. #ifdef __cplusplus
  80. extern "C" {
  81. #endif
  82. /* This is the reply object returned by redisCommand() */
  83. typedef struct redisReply {
  84. int type; /* REDIS_REPLY_* */
  85. long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  86. double dval; /* The double when type is REDIS_REPLY_DOUBLE */
  87. size_t len; /* Length of string */
  88. char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
  89. and REDIS_REPLY_DOUBLE (in additionl to dval). */
  90. char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null
  91. terminated 3 character content type, such as "txt". */
  92. size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
  93. struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  94. } redisReply;
  95. redisReader *redisReaderCreate(void);
  96. /* Function to free the reply objects hiredis returns by default. */
  97. void freeReplyObject(void *reply);
  98. /* Functions to format a command according to the protocol. */
  99. int redisvFormatCommand(char **target, const char *format, va_list ap);
  100. int redisFormatCommand(char **target, const char *format, ...);
  101. int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
  102. int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
  103. void redisFreeCommand(char *cmd);
  104. void redisFreeSdsCommand(sds cmd);
  105. enum redisConnectionType {
  106. REDIS_CONN_TCP,
  107. REDIS_CONN_UNIX,
  108. REDIS_CONN_USERFD
  109. };
  110. struct redisSsl;
  111. #define REDIS_OPT_NONBLOCK 0x01
  112. #define REDIS_OPT_REUSEADDR 0x02
  113. /**
  114. * Don't automatically free the async object on a connection failure,
  115. * or other implicit conditions. Only free on an explicit call to disconnect() or free()
  116. */
  117. #define REDIS_OPT_NOAUTOFREE 0x04
  118. /* In Unix systems a file descriptor is a regular signed int, with -1
  119. * representing an invalid descriptor. In Windows it is a SOCKET
  120. * (32- or 64-bit unsigned integer depending on the architecture), where
  121. * all bits set (~0) is INVALID_SOCKET. */
  122. #ifndef _WIN32
  123. typedef int redisFD;
  124. #define REDIS_INVALID_FD -1
  125. #else
  126. #ifdef _WIN64
  127. typedef unsigned long long redisFD; /* SOCKET = 64-bit UINT_PTR */
  128. #else
  129. typedef unsigned long redisFD; /* SOCKET = 32-bit UINT_PTR */
  130. #endif
  131. #define REDIS_INVALID_FD ((redisFD)(~0)) /* INVALID_SOCKET */
  132. #endif
  133. typedef struct {
  134. /*
  135. * the type of connection to use. This also indicates which
  136. * `endpoint` member field to use
  137. */
  138. int type;
  139. /* bit field of REDIS_OPT_xxx */
  140. int options;
  141. /* timeout value. if NULL, no timeout is used */
  142. const struct timeval *timeout;
  143. union {
  144. /** use this field for tcp/ip connections */
  145. struct {
  146. const char *source_addr;
  147. const char *ip;
  148. int port;
  149. } tcp;
  150. /** use this field for unix domain sockets */
  151. const char *unix_socket;
  152. /**
  153. * use this field to have hiredis operate an already-open
  154. * file descriptor */
  155. redisFD fd;
  156. } endpoint;
  157. } redisOptions;
  158. /**
  159. * Helper macros to initialize options to their specified fields.
  160. */
  161. #define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \
  162. (opts)->type = REDIS_CONN_TCP; \
  163. (opts)->endpoint.tcp.ip = ip_; \
  164. (opts)->endpoint.tcp.port = port_;
  165. #define REDIS_OPTIONS_SET_UNIX(opts, path) \
  166. (opts)->type = REDIS_CONN_UNIX; \
  167. (opts)->endpoint.unix_socket = path;
  168. struct redisAsyncContext;
  169. struct redisContext;
  170. typedef struct redisContextFuncs {
  171. void (*free_privdata)(void *);
  172. void (*async_read)(struct redisAsyncContext *);
  173. void (*async_write)(struct redisAsyncContext *);
  174. int (*read)(struct redisContext *, char *, size_t);
  175. int (*write)(struct redisContext *);
  176. } redisContextFuncs;
  177. /* Context for a connection to Redis */
  178. typedef struct redisContext {
  179. const redisContextFuncs *funcs; /* Function table */
  180. int err; /* Error flags, 0 when there is no error */
  181. char errstr[128]; /* String representation of error when applicable */
  182. redisFD fd;
  183. int flags;
  184. char *obuf; /* Write buffer */
  185. redisReader *reader; /* Protocol reader */
  186. enum redisConnectionType connection_type;
  187. struct timeval *timeout;
  188. struct {
  189. char *host;
  190. char *source_addr;
  191. int port;
  192. } tcp;
  193. struct {
  194. char *path;
  195. } unix_sock;
  196. /* For non-blocking connect */
  197. struct sockadr *saddr;
  198. size_t addrlen;
  199. /* Additional private data for hiredis addons such as SSL */
  200. void *privdata;
  201. } redisContext;
  202. redisContext *redisConnectWithOptions(const redisOptions *options);
  203. redisContext *redisConnect(const char *ip, int port);
  204. redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
  205. redisContext *redisConnectNonBlock(const char *ip, int port);
  206. redisContext *redisConnectBindNonBlock(const char *ip, int port,
  207. const char *source_addr);
  208. redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
  209. const char *source_addr);
  210. redisContext *redisConnectUnix(const char *path);
  211. redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
  212. redisContext *redisConnectUnixNonBlock(const char *path);
  213. redisContext *redisConnectFd(redisFD fd);
  214. /**
  215. * Reconnect the given context using the saved information.
  216. *
  217. * This re-uses the exact same connect options as in the initial connection.
  218. * host, ip (or path), timeout and bind address are reused,
  219. * flags are used unmodified from the existing context.
  220. *
  221. * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
  222. */
  223. int redisReconnect(redisContext *c);
  224. int redisSetTimeout(redisContext *c, const struct timeval tv);
  225. int redisEnableKeepAlive(redisContext *c);
  226. void redisFree(redisContext *c);
  227. redisFD redisFreeKeepFd(redisContext *c);
  228. int redisBufferRead(redisContext *c);
  229. int redisBufferWrite(redisContext *c, int *done);
  230. /* In a blocking context, this function first checks if there are unconsumed
  231. * replies to return and returns one if so. Otherwise, it flushes the output
  232. * buffer to the socket and reads until it has a reply. In a non-blocking
  233. * context, it will return unconsumed replies until there are no more. */
  234. int redisGetReply(redisContext *c, void **reply);
  235. int redisGetReplyFromReader(redisContext *c, void **reply);
  236. /* Write a formatted command to the output buffer. Use these functions in blocking mode
  237. * to get a pipeline of commands. */
  238. int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
  239. /* Write a command to the output buffer. Use these functions in blocking mode
  240. * to get a pipeline of commands. */
  241. int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
  242. int redisAppendCommand(redisContext *c, const char *format, ...);
  243. int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  244. /* Issue a command to Redis. In a blocking context, it is identical to calling
  245. * redisAppendCommand, followed by redisGetReply. The function will return
  246. * NULL if there was an error in performing the request, otherwise it will
  247. * return the reply. In a non-blocking context, it is identical to calling
  248. * only redisAppendCommand and will always return NULL. */
  249. void *redisvCommand(redisContext *c, const char *format, va_list ap);
  250. void *redisCommand(redisContext *c, const char *format, ...);
  251. void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif