You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hiredis.h 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <sys/time.h> /* for struct timeval */
  38. #include <stdint.h> /* uintXX_t, etc */
  39. #include <string.h> /* strerror_r, etc */
  40. #include "sds.h" /* for sds */
  41. #define HIREDIS_MAJOR 0
  42. #define HIREDIS_MINOR 13
  43. #define HIREDIS_PATCH 3
  44. #define HIREDIS_SONAME 0.13
  45. /* Connection type can be blocking or non-blocking and is set in the
  46. * least significant bit of the flags field in redisContext. */
  47. #define REDIS_BLOCK 0x1
  48. /* Connection may be disconnected before being free'd. The second bit
  49. * in the flags field is set when the context is connected. */
  50. #define REDIS_CONNECTED 0x2
  51. /* The async API might try to disconnect cleanly and flush the output
  52. * buffer and read all subsequent replies before disconnecting.
  53. * This flag means no new commands can come in and the connection
  54. * should be terminated once all replies have been read. */
  55. #define REDIS_DISCONNECTING 0x4
  56. /* Flag specific to the async API which means that the context should be clean
  57. * up as soon as possible. */
  58. #define REDIS_FREEING 0x8
  59. /* Flag that is set when an async callback is executed. */
  60. #define REDIS_IN_CALLBACK 0x10
  61. /* Flag that is set when the async context has one or more subscriptions. */
  62. #define REDIS_SUBSCRIBED 0x20
  63. /* Flag that is set when monitor mode is active */
  64. #define REDIS_MONITORING 0x40
  65. /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
  66. #define REDIS_REUSEADDR 0x80
  67. #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
  68. /* number of times we retry to connect in the case of EADDRNOTAVAIL and
  69. * SO_REUSEADDR is being used. */
  70. #define REDIS_CONNECT_RETRIES 10
  71. /* strerror_r has two completely different prototypes and behaviors
  72. * depending on system issues, so we need to operate on the error buffer
  73. * differently depending on which strerror_r we're using. */
  74. #ifndef _GNU_SOURCE
  75. /* "regular" POSIX strerror_r that does the right thing. */
  76. #define __redis_strerror_r(errno, buf, len) \
  77. do { \
  78. strerror_r((errno), (buf), (len)); \
  79. } while (0)
  80. #else
  81. /* "bad" GNU strerror_r we need to clean up after. */
  82. #define __redis_strerror_r(errno, buf, len) \
  83. do { \
  84. char *err_str = strerror((errno)); \
  85. /* If return value _isn't_ the start of the buffer we passed in, \
  86. * then GNU strerror_r returned an internal static buffer and we \
  87. * need to copy the result into our private buffer. */ \
  88. if (err_str != (buf)) { \
  89. buf[(len)-1] = '\0'; \
  90. strncat((buf), err_str, ((len) - 1)); \
  91. } \
  92. } while (0)
  93. #endif
  94. #ifdef __cplusplus
  95. extern "C" {
  96. #endif
  97. /* This is the reply object returned by redisCommand() */
  98. typedef struct redisReply {
  99. int type; /* REDIS_REPLY_* */
  100. long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  101. int len; /* Length of string */
  102. char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
  103. size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
  104. struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  105. } redisReply;
  106. redisReader *redisReaderCreate(void);
  107. /* Function to free the reply objects hiredis returns by default. */
  108. void freeReplyObject(void *reply);
  109. /* Functions to format a command according to the protocol. */
  110. #ifdef __GNUC__
  111. __attribute__((format(printf, 2, 0)))
  112. #endif
  113. int redisvFormatCommand(char **target, const char *format, va_list ap);
  114. #ifdef __GNUC__
  115. __attribute__((format(printf, 2, 3)))
  116. #endif
  117. int redisFormatCommand(char **target, const char *format, ...);
  118. int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
  119. int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
  120. void redisFreeCommand(char *cmd);
  121. void redisFreeSdsCommand(sds cmd);
  122. enum redisConnectionType {
  123. REDIS_CONN_TCP,
  124. REDIS_CONN_UNIX,
  125. };
  126. /* Context for a connection to Redis */
  127. typedef struct redisContext {
  128. int err; /* Error flags, 0 when there is no error */
  129. char errstr[128]; /* String representation of error when applicable */
  130. int fd;
  131. int flags;
  132. char *obuf; /* Write buffer */
  133. redisReader *reader; /* Protocol reader */
  134. enum redisConnectionType connection_type;
  135. struct timeval *timeout;
  136. struct {
  137. char *host;
  138. char *source_addr;
  139. int port;
  140. } tcp;
  141. struct {
  142. char *path;
  143. } unix_sock;
  144. } redisContext;
  145. redisContext *redisConnect(const char *ip, int port);
  146. redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
  147. redisContext *redisConnectNonBlock(const char *ip, int port);
  148. redisContext *redisConnectBindNonBlock(const char *ip, int port,
  149. const char *source_addr);
  150. redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
  151. const char *source_addr);
  152. redisContext *redisConnectUnix(const char *path);
  153. redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
  154. redisContext *redisConnectUnixNonBlock(const char *path);
  155. redisContext *redisConnectFd(int fd);
  156. /**
  157. * Reconnect the given context using the saved information.
  158. *
  159. * This re-uses the exact same connect options as in the initial connection.
  160. * host, ip (or path), timeout and bind address are reused,
  161. * flags are used unmodified from the existing context.
  162. *
  163. * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
  164. */
  165. int redisReconnect(redisContext *c);
  166. int redisSetTimeout(redisContext *c, const struct timeval tv);
  167. int redisEnableKeepAlive(redisContext *c);
  168. void redisFree(redisContext *c);
  169. int redisFreeKeepFd(redisContext *c);
  170. int redisBufferRead(redisContext *c);
  171. int redisBufferWrite(redisContext *c, int *done);
  172. /* In a blocking context, this function first checks if there are unconsumed
  173. * replies to return and returns one if so. Otherwise, it flushes the output
  174. * buffer to the socket and reads until it has a reply. In a non-blocking
  175. * context, it will return unconsumed replies until there are no more. */
  176. int redisGetReply(redisContext *c, void **reply);
  177. int redisGetReplyFromReader(redisContext *c, void **reply);
  178. /* Write a formatted command to the output buffer. Use these functions in blocking mode
  179. * to get a pipeline of commands. */
  180. int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
  181. /* Write a command to the output buffer. Use these functions in blocking mode
  182. * to get a pipeline of commands. */
  183. #ifdef __GNUC__
  184. __attribute__((format(printf, 2, 0)))
  185. #endif
  186. int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
  187. #ifdef __GNUC__
  188. __attribute__((format(printf, 2, 3)))
  189. #endif
  190. int redisAppendCommand(redisContext *c, const char *format, ...);
  191. int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  192. /* Issue a command to Redis. In a blocking context, it is identical to calling
  193. * redisAppendCommand, followed by redisGetReply. The function will return
  194. * NULL if there was an error in performing the request, otherwise it will
  195. * return the reply. In a non-blocking context, it is identical to calling
  196. * only redisAppendCommand and will always return NULL. */
  197. #ifdef __GNUC__
  198. __attribute__((format(printf, 2, 0)))
  199. #endif
  200. void *redisvCommand(redisContext *c, const char *format, va_list ap);
  201. #ifdef __GNUC__
  202. __attribute__((format(printf, 2, 3)))
  203. #endif
  204. void *redisCommand(redisContext *c, const char *format, ...);
  205. void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif