Simulation Core
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. typedef long long ssize_t;
  42. #endif
  43. #include <stdint.h> /* uintXX_t, etc */
  44. #include "sds.h" /* for sds */
  45. #include "alloc.h" /* for allocation wrappers */
  46. #define HIREDIS_MAJOR 1
  47. #define HIREDIS_MINOR 1
  48. #define HIREDIS_PATCH 0
  49. #define HIREDIS_SONAME 1.1.0
  50. /* Connection type can be blocking or non-blocking and is set in the
  51. * least significant bit of the flags field in redisContext. */
  52. #define REDIS_BLOCK 0x1
  53. /* Connection may be disconnected before being free'd. The second bit
  54. * in the flags field is set when the context is connected. */
  55. #define REDIS_CONNECTED 0x2
  56. /* The async API might try to disconnect cleanly and flush the output
  57. * buffer and read all subsequent replies before disconnecting.
  58. * This flag means no new commands can come in and the connection
  59. * should be terminated once all replies have been read. */
  60. #define REDIS_DISCONNECTING 0x4
  61. /* Flag specific to the async API which means that the context should be clean
  62. * up as soon as possible. */
  63. #define REDIS_FREEING 0x8
  64. /* Flag that is set when an async callback is executed. */
  65. #define REDIS_IN_CALLBACK 0x10
  66. /* Flag that is set when the async context has one or more subscriptions. */
  67. #define REDIS_SUBSCRIBED 0x20
  68. /* Flag that is set when monitor mode is active */
  69. #define REDIS_MONITORING 0x40
  70. /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
  71. #define REDIS_REUSEADDR 0x80
  72. /* Flag that is set when the async connection supports push replies. */
  73. #define REDIS_SUPPORTS_PUSH 0x100
  74. /**
  75. * Flag that indicates the user does not want the context to
  76. * be automatically freed upon error
  77. */
  78. #define REDIS_NO_AUTO_FREE 0x200
  79. /* Flag that indicates the user does not want replies to be automatically freed */
  80. #define REDIS_NO_AUTO_FREE_REPLIES 0x400
  81. /* Flags to prefer IPv6 or IPv4 when doing DNS lookup. (If both are set,
  82. * AF_UNSPEC is used.) */
  83. #define REDIS_PREFER_IPV4 0x800
  84. #define REDIS_PREFER_IPV6 0x1000
  85. #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
  86. /* number of times we retry to connect in the case of EADDRNOTAVAIL and
  87. * SO_REUSEADDR is being used. */
  88. #define REDIS_CONNECT_RETRIES 10
  89. /* Forward declarations for structs defined elsewhere */
  90. struct redisAsyncContext;
  91. struct redisContext;
  92. /* RESP3 push helpers and callback prototypes */
  93. #define redisIsPushReply(r) (((redisReply*)(r))->type == REDIS_REPLY_PUSH)
  94. typedef void (redisPushFn)(void *, void *);
  95. typedef void (redisAsyncPushFn)(struct redisAsyncContext *, void *);
  96. #ifdef __cplusplus
  97. extern "C" {
  98. #endif
  99. /* This is the reply object returned by redisCommand() */
  100. typedef struct redisReply {
  101. int type; /* REDIS_REPLY_* */
  102. long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  103. double dval; /* The double when type is REDIS_REPLY_DOUBLE */
  104. size_t len; /* Length of string */
  105. char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
  106. REDIS_REPLY_VERB, REDIS_REPLY_DOUBLE (in additional to dval),
  107. and REDIS_REPLY_BIGNUM. */
  108. char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null
  109. terminated 3 character content type, such as "txt". */
  110. size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
  111. struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  112. } redisReply;
  113. redisReader *redisReaderCreate(void);
  114. /* Function to free the reply objects hiredis returns by default. */
  115. void freeReplyObject(void *reply);
  116. /* Functions to format a command according to the protocol. */
  117. int redisvFormatCommand(char **target, const char *format, va_list ap);
  118. int redisFormatCommand(char **target, const char *format, ...);
  119. long long redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
  120. long long redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
  121. void redisFreeCommand(char *cmd);
  122. void redisFreeSdsCommand(sds cmd);
  123. enum redisConnectionType {
  124. REDIS_CONN_TCP,
  125. REDIS_CONN_UNIX,
  126. REDIS_CONN_USERFD
  127. };
  128. struct redisSsl;
  129. #define REDIS_OPT_NONBLOCK 0x01
  130. #define REDIS_OPT_REUSEADDR 0x02
  131. #define REDIS_OPT_PREFER_IPV4 0x04
  132. #define REDIS_OPT_PREFER_IPV6 0x08
  133. #define REDIS_OPT_PREFER_IP_UNSPEC (REDIS_OPT_PREFER_IPV4 | REDIS_OPT_PREFER_IPV6)
  134. /**
  135. * Don't automatically free the async object on a connection failure,
  136. * or other implicit conditions. Only free on an explicit call to disconnect() or free()
  137. */
  138. #define REDIS_OPT_NOAUTOFREE 0x04
  139. /* Don't automatically intercept and free RESP3 PUSH replies. */
  140. #define REDIS_OPT_NO_PUSH_AUTOFREE 0x08
  141. /**
  142. * Don't automatically free replies
  143. */
  144. #define REDIS_OPT_NOAUTOFREEREPLIES 0x10
  145. /* In Unix systems a file descriptor is a regular signed int, with -1
  146. * representing an invalid descriptor. In Windows it is a SOCKET
  147. * (32- or 64-bit unsigned integer depending on the architecture), where
  148. * all bits set (~0) is INVALID_SOCKET. */
  149. #ifndef _WIN32
  150. typedef int redisFD;
  151. #define REDIS_INVALID_FD -1
  152. #else
  153. #ifdef _WIN64
  154. typedef unsigned long long redisFD; /* SOCKET = 64-bit UINT_PTR */
  155. #else
  156. typedef unsigned long redisFD; /* SOCKET = 32-bit UINT_PTR */
  157. #endif
  158. #define REDIS_INVALID_FD ((redisFD)(~0)) /* INVALID_SOCKET */
  159. #endif
  160. typedef struct {
  161. /*
  162. * the type of connection to use. This also indicates which
  163. * `endpoint` member field to use
  164. */
  165. int type;
  166. /* bit field of REDIS_OPT_xxx */
  167. int options;
  168. /* timeout value for connect operation. If NULL, no timeout is used */
  169. const struct timeval *connect_timeout;
  170. /* timeout value for commands. If NULL, no timeout is used. This can be
  171. * updated at runtime with redisSetTimeout/redisAsyncSetTimeout. */
  172. const struct timeval *command_timeout;
  173. union {
  174. /** use this field for tcp/ip connections */
  175. struct {
  176. const char *source_addr;
  177. const char *ip;
  178. int port;
  179. } tcp;
  180. /** use this field for unix domain sockets */
  181. const char *unix_socket;
  182. /**
  183. * use this field to have hiredis operate an already-open
  184. * file descriptor */
  185. redisFD fd;
  186. } endpoint;
  187. /* Optional user defined data/destructor */
  188. void *privdata;
  189. void (*free_privdata)(void *);
  190. /* A user defined PUSH message callback */
  191. redisPushFn *push_cb;
  192. redisAsyncPushFn *async_push_cb;
  193. } redisOptions;
  194. /**
  195. * Helper macros to initialize options to their specified fields.
  196. */
  197. #define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) do { \
  198. (opts)->type = REDIS_CONN_TCP; \
  199. (opts)->endpoint.tcp.ip = ip_; \
  200. (opts)->endpoint.tcp.port = port_; \
  201. } while(0)
  202. #define REDIS_OPTIONS_SET_UNIX(opts, path) do { \
  203. (opts)->type = REDIS_CONN_UNIX; \
  204. (opts)->endpoint.unix_socket = path; \
  205. } while(0)
  206. #define REDIS_OPTIONS_SET_PRIVDATA(opts, data, dtor) do { \
  207. (opts)->privdata = data; \
  208. (opts)->free_privdata = dtor; \
  209. } while(0)
  210. typedef struct redisContextFuncs {
  211. void (*close)(struct redisContext *);
  212. void (*free_privctx)(void *);
  213. void (*async_read)(struct redisAsyncContext *);
  214. void (*async_write)(struct redisAsyncContext *);
  215. /* Read/Write data to the underlying communication stream, returning the
  216. * number of bytes read/written. In the event of an unrecoverable error
  217. * these functions shall return a value < 0. In the event of a
  218. * recoverable error, they should return 0. */
  219. ssize_t (*read)(struct redisContext *, char *, size_t);
  220. ssize_t (*write)(struct redisContext *);
  221. } redisContextFuncs;
  222. /* Context for a connection to Redis */
  223. typedef struct redisContext {
  224. const redisContextFuncs *funcs; /* Function table */
  225. int err; /* Error flags, 0 when there is no error */
  226. char errstr[128]; /* String representation of error when applicable */
  227. redisFD fd;
  228. int flags;
  229. char *obuf; /* Write buffer */
  230. redisReader *reader; /* Protocol reader */
  231. enum redisConnectionType connection_type;
  232. struct timeval *connect_timeout;
  233. struct timeval *command_timeout;
  234. struct {
  235. char *host;
  236. char *source_addr;
  237. int port;
  238. } tcp;
  239. struct {
  240. char *path;
  241. } unix_sock;
  242. /* For non-blocking connect */
  243. struct sockaddr *saddr;
  244. size_t addrlen;
  245. /* Optional data and corresponding destructor users can use to provide
  246. * context to a given redisContext. Not used by hiredis. */
  247. void *privdata;
  248. void (*free_privdata)(void *);
  249. /* Internal context pointer presently used by hiredis to manage
  250. * SSL connections. */
  251. void *privctx;
  252. /* An optional RESP3 PUSH handler */
  253. redisPushFn *push_cb;
  254. } redisContext;
  255. redisContext *redisConnectWithOptions(const redisOptions *options);
  256. redisContext *redisConnect(const char *ip, int port);
  257. redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
  258. redisContext *redisConnectNonBlock(const char *ip, int port);
  259. redisContext *redisConnectBindNonBlock(const char *ip, int port,
  260. const char *source_addr);
  261. redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
  262. const char *source_addr);
  263. redisContext *redisConnectUnix(const char *path);
  264. redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
  265. redisContext *redisConnectUnixNonBlock(const char *path);
  266. redisContext *redisConnectFd(redisFD fd);
  267. /**
  268. * Reconnect the given context using the saved information.
  269. *
  270. * This re-uses the exact same connect options as in the initial connection.
  271. * host, ip (or path), timeout and bind address are reused,
  272. * flags are used unmodified from the existing context.
  273. *
  274. * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
  275. */
  276. int redisReconnect(redisContext *c);
  277. redisPushFn *redisSetPushCallback(redisContext *c, redisPushFn *fn);
  278. int redisSetTimeout(redisContext *c, const struct timeval tv);
  279. int redisEnableKeepAlive(redisContext *c);
  280. void redisFree(redisContext *c);
  281. redisFD redisFreeKeepFd(redisContext *c);
  282. int redisBufferRead(redisContext *c);
  283. int redisBufferWrite(redisContext *c, int *done);
  284. /* In a blocking context, this function first checks if there are unconsumed
  285. * replies to return and returns one if so. Otherwise, it flushes the output
  286. * buffer to the socket and reads until it has a reply. In a non-blocking
  287. * context, it will return unconsumed replies until there are no more. */
  288. int redisGetReply(redisContext *c, void **reply);
  289. int redisGetReplyFromReader(redisContext *c, void **reply);
  290. /* Write a formatted command to the output buffer. Use these functions in blocking mode
  291. * to get a pipeline of commands. */
  292. int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
  293. /* Write a command to the output buffer. Use these functions in blocking mode
  294. * to get a pipeline of commands. */
  295. int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
  296. int redisAppendCommand(redisContext *c, const char *format, ...);
  297. int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  298. /* Issue a command to Redis. In a blocking context, it is identical to calling
  299. * redisAppendCommand, followed by redisGetReply. The function will return
  300. * NULL if there was an error in performing the request, otherwise it will
  301. * return the reply. In a non-blocking context, it is identical to calling
  302. * only redisAppendCommand and will always return NULL. */
  303. void *redisvCommand(redisContext *c, const char *format, va_list ap);
  304. void *redisCommand(redisContext *c, const char *format, ...);
  305. void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif