hiredis.h 9.4 KB

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