/hiredis/hiredis.h

http://github.com/nicolasff/webdis · C Header · 222 lines · 108 code · 33 blank · 81 comment · 4 complexity · f4d4b4fc85c2cadcaa0bd9a7c555c6c1 MD5 · raw file

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