诸暨麻将添加redis
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

299 строки
11 KiB

  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. #include "alloc.h" /* for allocation wrappers */
  45. #define HIREDIS_MAJOR 0
  46. #define HIREDIS_MINOR 14
  47. #define HIREDIS_PATCH 0
  48. #define HIREDIS_SONAME 0.14
  49. /* Connection type can be blocking or non-blocking and is set in the
  50. * least significant bit of the flags field in redisContext. */
  51. #define REDIS_BLOCK 0x1
  52. /* Connection may be disconnected before being free'd. The second bit
  53. * in the flags field is set when the context is connected. */
  54. #define REDIS_CONNECTED 0x2
  55. /* The async API might try to disconnect cleanly and flush the output
  56. * buffer and read all subsequent replies before disconnecting.
  57. * This flag means no new commands can come in and the connection
  58. * should be terminated once all replies have been read. */
  59. #define REDIS_DISCONNECTING 0x4
  60. /* Flag specific to the async API which means that the context should be clean
  61. * up as soon as possible. */
  62. #define REDIS_FREEING 0x8
  63. /* Flag that is set when an async callback is executed. */
  64. #define REDIS_IN_CALLBACK 0x10
  65. /* Flag that is set when the async context has one or more subscriptions. */
  66. #define REDIS_SUBSCRIBED 0x20
  67. /* Flag that is set when monitor mode is active */
  68. #define REDIS_MONITORING 0x40
  69. /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
  70. #define REDIS_REUSEADDR 0x80
  71. /**
  72. * Flag that indicates the user does not want the context to
  73. * be automatically freed upon error
  74. */
  75. #define REDIS_NO_AUTO_FREE 0x200
  76. #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
  77. /* number of times we retry to connect in the case of EADDRNOTAVAIL and
  78. * SO_REUSEADDR is being used. */
  79. #define REDIS_CONNECT_RETRIES 10
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /* This is the reply object returned by redisCommand() */
  84. typedef struct redisReply {
  85. int type; /* REDIS_REPLY_* */
  86. long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  87. double dval; /* The double when type is REDIS_REPLY_DOUBLE */
  88. size_t len; /* Length of string */
  89. char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
  90. and REDIS_REPLY_DOUBLE (in additional to dval). */
  91. size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
  92. struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  93. } redisReply;
  94. redisReader *redisReaderCreate(void);
  95. /* Function to free the reply objects hiredis returns by default. */
  96. void freeReplyObject(void *reply);
  97. /* Functions to format a command according to the protocol. */
  98. int redisvFormatCommand(char **target, const char *format, va_list ap);
  99. int redisFormatCommand(char **target, const char *format, ...);
  100. int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
  101. int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
  102. void redisFreeCommand(char *cmd);
  103. void redisFreeSdsCommand(sds cmd);
  104. enum redisConnectionType {
  105. REDIS_CONN_TCP,
  106. REDIS_CONN_UNIX,
  107. REDIS_CONN_USERFD
  108. };
  109. struct redisSsl;
  110. #define REDIS_OPT_NONBLOCK 0x01
  111. #define REDIS_OPT_REUSEADDR 0x02
  112. /**
  113. * Don't automatically free the async object on a connection failure,
  114. * or other implicit conditions. Only free on an explicit call to disconnect() or free()
  115. */
  116. #define REDIS_OPT_NOAUTOFREE 0x04
  117. /* In Unix systems a file descriptor is a regular signed int, with -1
  118. * representing an invalid descriptor. In Windows it is a SOCKET
  119. * (32- or 64-bit unsigned integer depending on the architecture), where
  120. * all bits set (~0) is INVALID_SOCKET. */
  121. #ifndef _WIN32
  122. typedef int redisFD;
  123. #define REDIS_INVALID_FD -1
  124. #else
  125. #ifdef _WIN64
  126. typedef unsigned long long redisFD; /* SOCKET = 64-bit UINT_PTR */
  127. #else
  128. typedef unsigned long redisFD; /* SOCKET = 32-bit UINT_PTR */
  129. #endif
  130. #define REDIS_INVALID_FD ((redisFD)(~0)) /* INVALID_SOCKET */
  131. #endif
  132. typedef struct {
  133. /*
  134. * the type of connection to use. This also indicates which
  135. * `endpoint` member field to use
  136. */
  137. int type;
  138. /* bit field of REDIS_OPT_xxx */
  139. int options;
  140. /* timeout value. if NULL, no timeout is used */
  141. const struct timeval *timeout;
  142. union {
  143. /** use this field for tcp/ip connections */
  144. struct {
  145. const char *source_addr;
  146. const char *ip;
  147. int port;
  148. } tcp;
  149. /** use this field for unix domain sockets */
  150. const char *unix_socket;
  151. /**
  152. * use this field to have hiredis operate an already-open
  153. * file descriptor */
  154. redisFD fd;
  155. } endpoint;
  156. } redisOptions;
  157. /**
  158. * Helper macros to initialize options to their specified fields.
  159. */
  160. #define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \
  161. (opts)->type = REDIS_CONN_TCP; \
  162. (opts)->endpoint.tcp.ip = ip_; \
  163. (opts)->endpoint.tcp.port = port_;
  164. #define REDIS_OPTIONS_SET_UNIX(opts, path) \
  165. (opts)->type = REDIS_CONN_UNIX; \
  166. (opts)->endpoint.unix_socket = path;
  167. struct redisAsyncContext;
  168. struct redisContext;
  169. typedef struct redisContextFuncs {
  170. void (*free_privdata)(void *);
  171. void (*async_read)(struct redisAsyncContext *);
  172. void (*async_write)(struct redisAsyncContext *);
  173. int (*read)(struct redisContext *, char *, size_t);
  174. int (*write)(struct redisContext *);
  175. } redisContextFuncs;
  176. /* Context for a connection to Redis */
  177. typedef struct redisContext {
  178. const redisContextFuncs *funcs; /* Function table */
  179. int err; /* Error flags, 0 when there is no error */
  180. char errstr[128]; /* String representation of error when applicable */
  181. redisFD fd;
  182. int flags;
  183. char *obuf; /* Write buffer */
  184. redisReader *reader; /* Protocol reader */
  185. enum redisConnectionType connection_type;
  186. struct timeval *timeout;
  187. struct {
  188. char *host;
  189. char *source_addr;
  190. int port;
  191. } tcp;
  192. struct {
  193. char *path;
  194. } unix_sock;
  195. /* For non-blocking connect */
  196. struct sockadr *saddr;
  197. size_t addrlen;
  198. /* Additional private data for hiredis addons such as SSL */
  199. void *privdata;
  200. } redisContext;
  201. redisContext *redisConnectWithOptions(const redisOptions *options);
  202. redisContext *redisConnect(const char *ip, int port);
  203. redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
  204. redisContext *redisConnectNonBlock(const char *ip, int port);
  205. redisContext *redisConnectBindNonBlock(const char *ip, int port,
  206. const char *source_addr);
  207. redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
  208. const char *source_addr);
  209. redisContext *redisConnectUnix(const char *path);
  210. redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
  211. redisContext *redisConnectUnixNonBlock(const char *path);
  212. redisContext *redisConnectFd(redisFD fd);
  213. /**
  214. * Reconnect the given context using the saved information.
  215. *
  216. * This re-uses the exact same connect options as in the initial connection.
  217. * host, ip (or path), timeout and bind address are reused,
  218. * flags are used unmodified from the existing context.
  219. *
  220. * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
  221. */
  222. int redisReconnect(redisContext *c);
  223. int redisSetTimeout(redisContext *c, const struct timeval tv);
  224. int redisEnableKeepAlive(redisContext *c);
  225. void redisFree(redisContext *c);
  226. redisFD redisFreeKeepFd(redisContext *c);
  227. int redisBufferRead(redisContext *c);
  228. int redisBufferWrite(redisContext *c, int *done);
  229. /* In a blocking context, this function first checks if there are unconsumed
  230. * replies to return and returns one if so. Otherwise, it flushes the output
  231. * buffer to the socket and reads until it has a reply. In a non-blocking
  232. * context, it will return unconsumed replies until there are no more. */
  233. int redisGetReply(redisContext *c, void **reply);
  234. int redisGetReplyFromReader(redisContext *c, void **reply);
  235. /* Write a formatted command to the output buffer. Use these functions in blocking mode
  236. * to get a pipeline of commands. */
  237. int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
  238. /* Write a command to the output buffer. Use these functions in blocking mode
  239. * to get a pipeline of commands. */
  240. int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
  241. int redisAppendCommand(redisContext *c, const char *format, ...);
  242. int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  243. /* Issue a command to Redis. In a blocking context, it is identical to calling
  244. * redisAppendCommand, followed by redisGetReply. The function will return
  245. * NULL if there was an error in performing the request, otherwise it will
  246. * return the reply. In a non-blocking context, it is identical to calling
  247. * only redisAppendCommand and will always return NULL. */
  248. void *redisvCommand(redisContext *c, const char *format, va_list ap);
  249. void *redisCommand(redisContext *c, const char *format, ...);
  250. void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  251. #ifdef __cplusplus
  252. }
  253. #endif
  254. #endif