2
0

hiredis.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of Redis nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef __HIREDIS_H
  32. #define __HIREDIS_H
  33. #include <stdio.h> /* for size_t */
  34. #include <stdarg.h> /* for va_list */
  35. #include <sys/time.h> /* for struct timeval */
  36. #define HIREDIS_MAJOR 0
  37. #define HIREDIS_MINOR 10
  38. #define HIREDIS_PATCH 1
  39. #define REDIS_ERR -1
  40. #define REDIS_OK 0
  41. /* When an error occurs, the err flag in a context is set to hold the type of
  42. * error that occured. REDIS_ERR_IO means there was an I/O error and you
  43. * should use the "errno" variable to find out what is wrong.
  44. * For other values, the "errstr" field will hold a description. */
  45. #define REDIS_ERR_IO 1 /* Error in read or write */
  46. #define REDIS_ERR_EOF 3 /* End of file */
  47. #define REDIS_ERR_PROTOCOL 4 /* Protocol error */
  48. #define REDIS_ERR_OOM 5 /* Out of memory */
  49. #define REDIS_ERR_OTHER 2 /* Everything else... */
  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. #define REDIS_REPLY_STRING 1
  71. #define REDIS_REPLY_ARRAY 2
  72. #define REDIS_REPLY_INTEGER 3
  73. #define REDIS_REPLY_NIL 4
  74. #define REDIS_REPLY_STATUS 5
  75. #define REDIS_REPLY_ERROR 6
  76. #define REDIS_READER_MAX_BUF (1024*16) /* Default max unused reader buffer. */
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /* This is the reply object returned by redisCommand() */
  81. typedef struct redisReply {
  82. int type; /* REDIS_REPLY_* */
  83. long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  84. int len; /* Length of string */
  85. char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
  86. size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
  87. struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
  88. } redisReply;
  89. typedef struct redisReadTask {
  90. int type;
  91. int elements; /* number of elements in multibulk container */
  92. int idx; /* index in parent (array) object */
  93. void *obj; /* holds user-generated value for a read task */
  94. struct redisReadTask *parent; /* parent task */
  95. void *privdata; /* user-settable arbitrary field */
  96. } redisReadTask;
  97. typedef struct redisReplyObjectFunctions {
  98. void *(*createString)(const redisReadTask*, char*, size_t);
  99. void *(*createArray)(const redisReadTask*, int);
  100. void *(*createInteger)(const redisReadTask*, long long);
  101. void *(*createNil)(const redisReadTask*);
  102. void (*freeObject)(void*);
  103. } redisReplyObjectFunctions;
  104. /* State for the protocol parser */
  105. typedef struct redisReader {
  106. int err; /* Error flags, 0 when there is no error */
  107. char errstr[128]; /* String representation of error when applicable */
  108. char *buf; /* Read buffer */
  109. size_t pos; /* Buffer cursor */
  110. size_t len; /* Buffer length */
  111. size_t maxbuf; /* Max length of unused buffer */
  112. redisReadTask rstack[9];
  113. int ridx; /* Index of current read task */
  114. void *reply; /* Temporary reply pointer */
  115. redisReplyObjectFunctions *fn;
  116. void *privdata;
  117. } redisReader;
  118. /* Public API for the protocol parser. */
  119. redisReader *redisReaderCreate(void);
  120. void redisReaderFree(redisReader *r);
  121. int redisReaderFeed(redisReader *r, const char *buf, size_t len);
  122. int redisReaderGetReply(redisReader *r, void **reply);
  123. /* Backwards compatibility, can be removed on big version bump. */
  124. #define redisReplyReaderCreate redisReaderCreate
  125. #define redisReplyReaderFree redisReaderFree
  126. #define redisReplyReaderFeed redisReaderFeed
  127. #define redisReplyReaderGetReply redisReaderGetReply
  128. #define redisReplyReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p))
  129. #define redisReplyReaderGetObject(_r) (((redisReader*)(_r))->reply)
  130. #define redisReplyReaderGetError(_r) (((redisReader*)(_r))->errstr)
  131. /* Function to free the reply objects hiredis returns by default. */
  132. void freeReplyObject(void *reply);
  133. /* Functions to format a command according to the protocol. */
  134. int redisvFormatCommand(char **target, const char *format, va_list ap);
  135. int redisFormatCommand(char **target, const char *format, ...);
  136. int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
  137. /* Context for a connection to Redis */
  138. typedef struct redisContext {
  139. int err; /* Error flags, 0 when there is no error */
  140. char errstr[128]; /* String representation of error when applicable */
  141. int fd;
  142. int flags;
  143. char *obuf; /* Write buffer */
  144. redisReader *reader; /* Protocol reader */
  145. } redisContext;
  146. redisContext *redisConnect(const char *ip, int port);
  147. redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);
  148. redisContext *redisConnectNonBlock(const char *ip, int port);
  149. redisContext *redisConnectUnix(const char *path);
  150. redisContext *redisConnectUnixWithTimeout(const char *path, struct timeval tv);
  151. redisContext *redisConnectUnixNonBlock(const char *path);
  152. int redisSetTimeout(redisContext *c, struct timeval tv);
  153. void redisFree(redisContext *c);
  154. int redisBufferRead(redisContext *c);
  155. int redisBufferWrite(redisContext *c, int *done);
  156. /* In a blocking context, this function first checks if there are unconsumed
  157. * replies to return and returns one if so. Otherwise, it flushes the output
  158. * buffer to the socket and reads until it has a reply. In a non-blocking
  159. * context, it will return unconsumed replies until there are no more. */
  160. int redisGetReply(redisContext *c, void **reply);
  161. int redisGetReplyFromReader(redisContext *c, void **reply);
  162. /* Write a command to the output buffer. Use these functions in blocking mode
  163. * to get a pipeline of commands. */
  164. int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
  165. int redisAppendCommand(redisContext *c, const char *format, ...);
  166. int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  167. /* Issue a command to Redis. In a blocking context, it is identical to calling
  168. * redisAppendCommand, followed by redisGetReply. The function will return
  169. * NULL if there was an error in performing the request, otherwise it will
  170. * return the reply. In a non-blocking context, it is identical to calling
  171. * only redisAppendCommand and will always return NULL. */
  172. void *redisvCommand(redisContext *c, const char *format, va_list ap);
  173. void *redisCommand(redisContext *c, const char *format, ...);
  174. void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif