2 * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
3 * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
33 #ifndef __HIREDIS_READ_H
34 #define __HIREDIS_READ_H
35 #include <stdio.h> /* for size_t */
40 /* When an error occurs, the err flag in a context is set to hold the type of
41 * error that occured. REDIS_ERR_IO means there was an I/O error and you
42 * should use the "errno" variable to find out what is wrong.
43 * For other values, the "errstr" field will hold a description. */
44 #define REDIS_ERR_IO 1 /* Error in read or write */
45 #define REDIS_ERR_EOF 3 /* End of file */
46 #define REDIS_ERR_PROTOCOL 4 /* Protocol error */
47 #define REDIS_ERR_OOM 5 /* Out of memory */
48 #define REDIS_ERR_OTHER 2 /* Everything else... */
50 #define REDIS_REPLY_STRING 1
51 #define REDIS_REPLY_ARRAY 2
52 #define REDIS_REPLY_INTEGER 3
53 #define REDIS_REPLY_NIL 4
54 #define REDIS_REPLY_STATUS 5
55 #define REDIS_REPLY_ERROR 6
57 #define REDIS_READER_MAX_BUF (1024*16) /* Default max unused reader buffer. */
63 typedef struct redisReadTask {
65 int elements; /* number of elements in multibulk container */
66 int idx; /* index in parent (array) object */
67 void *obj; /* holds user-generated value for a read task */
68 struct redisReadTask *parent; /* parent task */
69 void *privdata; /* user-settable arbitrary field */
72 typedef struct redisReplyObjectFunctions {
73 void *(*createString)(const redisReadTask*, char*, size_t);
74 void *(*createArray)(const redisReadTask*, int);
75 void *(*createInteger)(const redisReadTask*, long long);
76 void *(*createNil)(const redisReadTask*);
77 void (*freeObject)(void*);
78 } redisReplyObjectFunctions;
80 typedef struct redisReader {
81 int err; /* Error flags, 0 when there is no error */
82 char errstr[128]; /* String representation of error when applicable */
84 char *buf; /* Read buffer */
85 size_t pos; /* Buffer cursor */
86 size_t len; /* Buffer length */
87 size_t maxbuf; /* Max length of unused buffer */
89 redisReadTask rstack[9];
90 int ridx; /* Index of current read task */
91 void *reply; /* Temporary reply pointer */
93 redisReplyObjectFunctions *fn;
97 /* Public API for the protocol parser. */
98 redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn);
99 void redisReaderFree(redisReader *r);
100 int redisReaderFeed(redisReader *r, const char *buf, size_t len);
101 int redisReaderGetReply(redisReader *r, void **reply);
103 /* Backwards compatibility, can be removed on big version bump. */
104 #define redisReplyReaderCreate redisReaderCreate
105 #define redisReplyReaderFree redisReaderFree
106 #define redisReplyReaderFeed redisReaderFeed
107 #define redisReplyReaderGetReply redisReaderGetReply
108 #define redisReplyReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p))
109 #define redisReplyReaderGetObject(_r) (((redisReader*)(_r))->reply)
110 #define redisReplyReaderGetError(_r) (((redisReader*)(_r))->errstr)