read.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_READ_H
  32. #define __HIREDIS_READ_H
  33. #include <stdio.h> /* for size_t */
  34. #define REDIS_ERR -1
  35. #define REDIS_OK 0
  36. /* When an error occurs, the err flag in a context is set to hold the type of
  37. * error that occurred. REDIS_ERR_IO means there was an I/O error and you
  38. * should use the "errno" variable to find out what is wrong.
  39. * For other values, the "errstr" field will hold a description. */
  40. #define REDIS_ERR_IO 1 /* Error in read or write */
  41. #define REDIS_ERR_EOF 3 /* End of file */
  42. #define REDIS_ERR_PROTOCOL 4 /* Protocol error */
  43. #define REDIS_ERR_OOM 5 /* Out of memory */
  44. #define REDIS_ERR_TIMEOUT 6 /* Timed out */
  45. #define REDIS_ERR_OTHER 2 /* Everything else... */
  46. #define REDIS_REPLY_STRING 1
  47. #define REDIS_REPLY_ARRAY 2
  48. #define REDIS_REPLY_INTEGER 3
  49. #define REDIS_REPLY_NIL 4
  50. #define REDIS_REPLY_STATUS 5
  51. #define REDIS_REPLY_ERROR 6
  52. #define REDIS_REPLY_DOUBLE 7
  53. #define REDIS_REPLY_BOOL 8
  54. #define REDIS_REPLY_MAP 9
  55. #define REDIS_REPLY_SET 10
  56. #define REDIS_REPLY_ATTR 11
  57. #define REDIS_REPLY_PUSH 12
  58. #define REDIS_REPLY_BIGNUM 13
  59. #define REDIS_REPLY_VERB 14
  60. /* Default max unused reader buffer. */
  61. #define REDIS_READER_MAX_BUF (1024*16)
  62. /* Default multi-bulk element limit */
  63. #define REDIS_READER_MAX_ARRAY_ELEMENTS ((1LL<<32) - 1)
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. typedef struct redisReadTask {
  68. int type;
  69. long long elements; /* number of elements in multibulk container */
  70. int idx; /* index in parent (array) object */
  71. void *obj; /* holds user-generated value for a read task */
  72. struct redisReadTask *parent; /* parent task */
  73. void *privdata; /* user-settable arbitrary field */
  74. } redisReadTask;
  75. typedef struct redisReplyObjectFunctions {
  76. void *(*createString)(const redisReadTask*, char*, size_t);
  77. void *(*createArray)(const redisReadTask*, size_t);
  78. void *(*createInteger)(const redisReadTask*, long long);
  79. void *(*createDouble)(const redisReadTask*, double, char*, size_t);
  80. void *(*createNil)(const redisReadTask*);
  81. void *(*createBool)(const redisReadTask*, int);
  82. void (*freeObject)(void*);
  83. } redisReplyObjectFunctions;
  84. typedef struct redisReader {
  85. int err; /* Error flags, 0 when there is no error */
  86. char errstr[128]; /* String representation of error when applicable */
  87. char *buf; /* Read buffer */
  88. size_t pos; /* Buffer cursor */
  89. size_t len; /* Buffer length */
  90. size_t maxbuf; /* Max length of unused buffer */
  91. long long maxelements; /* Max multi-bulk elements */
  92. redisReadTask **task;
  93. int tasks;
  94. int ridx; /* Index of current read task */
  95. void *reply; /* Temporary reply pointer */
  96. redisReplyObjectFunctions *fn;
  97. void *privdata;
  98. } redisReader;
  99. /* Public API for the protocol parser. */
  100. redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn);
  101. void redisReaderFree(redisReader *r);
  102. int redisReaderFeed(redisReader *r, const char *buf, size_t len);
  103. int redisReaderGetReply(redisReader *r, void **reply);
  104. #define redisReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p))
  105. #define redisReaderGetObject(_r) (((redisReader*)(_r))->reply)
  106. #define redisReaderGetError(_r) (((redisReader*)(_r))->errstr)
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif