]> git.saurik.com Git - redis.git/blame - deps/hiredis/hiredis.h
Update hiredis
[redis.git] / deps / hiredis / hiredis.h
CommitLineData
24f753a8
PN
1/*
2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __HIREDIS_H
31#define __HIREDIS_H
32#include <stdio.h> /* for size_t */
33#include <stdarg.h> /* for va_list */
34
35#define HIREDIS_MAJOR 0
36#define HIREDIS_MINOR 9
37#define HIREDIS_PATCH 0
38
39#define REDIS_ERR -1
40#define REDIS_OK 0
41
42/* When an error occurs, the err flag in a context is set to hold the type of
43 * error that occured. REDIS_ERR_IO means there was an I/O error and you
44 * should use the "errno" variable to find out what is wrong.
45 * For other values, the "errstr" field will hold a description. */
46#define REDIS_ERR_IO 1 /* error in read or write */
47#define REDIS_ERR_EOF 3 /* eof */
48#define REDIS_ERR_PROTOCOL 4 /* protocol error */
49#define REDIS_ERR_OTHER 2 /* something else */
50
51/* Connection type can be blocking or non-blocking and is set in the
52 * least significant bit of the flags field in redisContext. */
53#define REDIS_BLOCK 0x1
54
55/* Connection may be disconnected before being free'd. The second bit
56 * in the flags field is set when the context is connected. */
57#define REDIS_CONNECTED 0x2
58
59/* The async API might try to disconnect cleanly and flush the output
60 * buffer and read all subsequent replies before disconnecting.
61 * This flag means no new commands can come in and the connection
62 * should be terminated once all replies have been read. */
63#define REDIS_DISCONNECTING 0x4
64
65#define REDIS_REPLY_ERROR 0
66#define REDIS_REPLY_STRING 1
67#define REDIS_REPLY_ARRAY 2
68#define REDIS_REPLY_INTEGER 3
69#define REDIS_REPLY_NIL 4
70#define REDIS_REPLY_STATUS 5
71
72/* This is the reply object returned by redisCommand() */
73typedef struct redisReply {
74 int type; /* REDIS_REPLY_* */
75 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
76 int len; /* Length of string */
77 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
78 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
79 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
80} redisReply;
81
82typedef struct redisReadTask {
83 int type;
84 int elements; /* number of elements in multibulk container */
85 void *parent; /* optional pointer to parent object */
86 int idx; /* index in parent (array) object */
87} redisReadTask;
88
89typedef struct redisReplyObjectFunctions {
90 void *(*createString)(const redisReadTask*, char*, size_t);
91 void *(*createArray)(const redisReadTask*, int);
92 void *(*createInteger)(const redisReadTask*, long long);
93 void *(*createNil)(const redisReadTask*);
94 void (*freeObject)(void*);
95} redisReplyObjectFunctions;
96
97struct redisContext; /* need forward declaration of redisContext */
98
99/* Context for a connection to Redis */
100typedef struct redisContext {
101 int fd;
102 int flags;
103 char *obuf; /* Write buffer */
104 int err; /* Error flags, 0 when there is no error */
105 char *errstr; /* String representation of error when applicable */
106
107 /* Function set for reply buildup and reply reader */
108 redisReplyObjectFunctions *fn;
109 void *reader;
110} redisContext;
111
112void freeReplyObject(void *reply);
afc156c2
PN
113void *redisReplyReaderCreate();
114int redisReplyReaderSetReplyObjectFunctions(void *reader, redisReplyObjectFunctions *fn);
24f753a8
PN
115void *redisReplyReaderGetObject(void *reader);
116char *redisReplyReaderGetError(void *reader);
117void redisReplyReaderFree(void *ptr);
57c9babd 118void redisReplyReaderFeed(void *reader, char *buf, size_t len);
24f753a8
PN
119int redisReplyReaderGetReply(void *reader, void **reply);
120
121/* Functions to format a command according to the protocol. */
122int redisvFormatCommand(char **target, const char *format, va_list ap);
123int redisFormatCommand(char **target, const char *format, ...);
124int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
125
126redisContext *redisConnect(const char *ip, int port);
127redisContext *redisConnectNonBlock(const char *ip, int port);
128redisContext *redisConnectUnix(const char *path);
129redisContext *redisConnectUnixNonBlock(const char *path);
130int redisSetReplyObjectFunctions(redisContext *c, redisReplyObjectFunctions *fn);
131void redisFree(redisContext *c);
132int redisBufferRead(redisContext *c);
133int redisBufferWrite(redisContext *c, int *done);
134
135/* In a blocking context, this function first checks if there are unconsumed
136 * replies to return and returns one if so. Otherwise, it flushes the output
137 * buffer to the socket and reads until it has a reply. In a non-blocking
138 * context, it will return unconsumed replies until there are no more. */
139int redisGetReply(redisContext *c, void **reply);
140int redisGetReplyFromReader(redisContext *c, void **reply);
141
142/* Write a command to the output buffer. Use these functions in blocking mode
143 * to get a pipeline of commands. */
144void redisvAppendCommand(redisContext *c, const char *format, va_list ap);
145void redisAppendCommand(redisContext *c, const char *format, ...);
146void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
147
148/* Issue a command to Redis. In a blocking context, it is identical to calling
149 * redisAppendCommand, followed by redisGetReply. The function will return
150 * NULL if there was an error in performing the request, otherwise it will
151 * return the reply. In a non-blocking context, it is identical to calling
152 * only redisAppendCommand and will always return NULL. */
153void *redisvCommand(redisContext *c, const char *format, va_list ap);
154void *redisCommand(redisContext *c, const char *format, ...);
155void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
156
157#endif