PageRenderTime 23ms CodeModel.GetById 8ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hiredis/hiredis.h

http://github.com/nicolasff/webdis
C++ Header | 222 lines | 108 code | 33 blank | 81 comment | 4 complexity | f4d4b4fc85c2cadcaa0bd9a7c555c6c1 MD5 | raw file
  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
 34#ifndef __HIREDIS_H
 35#define __HIREDIS_H
 36#include "read.h"
 37#include <stdarg.h> /* for va_list */
 38#include <sys/time.h> /* for struct timeval */
 39#include <stdint.h> /* uintXX_t, etc */
 40#include "sds.h" /* for sds */
 41
 42#define HIREDIS_MAJOR 0
 43#define HIREDIS_MINOR 13
 44#define HIREDIS_PATCH 1
 45
 46/* Connection type can be blocking or non-blocking and is set in the
 47 * least significant bit of the flags field in redisContext. */
 48#define REDIS_BLOCK 0x1
 49
 50/* Connection may be disconnected before being free'd. The second bit
 51 * in the flags field is set when the context is connected. */
 52#define REDIS_CONNECTED 0x2
 53
 54/* The async API might try to disconnect cleanly and flush the output
 55 * buffer and read all subsequent replies before disconnecting.
 56 * This flag means no new commands can come in and the connection
 57 * should be terminated once all replies have been read. */
 58#define REDIS_DISCONNECTING 0x4
 59
 60/* Flag specific to the async API which means that the context should be clean
 61 * up as soon as possible. */
 62#define REDIS_FREEING 0x8
 63
 64/* Flag that is set when an async callback is executed. */
 65#define REDIS_IN_CALLBACK 0x10
 66
 67/* Flag that is set when the async context has one or more subscriptions. */
 68#define REDIS_SUBSCRIBED 0x20
 69
 70/* Flag that is set when monitor mode is active */
 71#define REDIS_MONITORING 0x40
 72
 73/* Flag that is set when we should set SO_REUSEADDR before calling bind() */
 74#define REDIS_REUSEADDR 0x80
 75
 76#define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
 77
 78/* number of times we retry to connect in the case of EADDRNOTAVAIL and
 79 * SO_REUSEADDR is being used. */
 80#define REDIS_CONNECT_RETRIES  10
 81
 82/* strerror_r has two completely different prototypes and behaviors
 83 * depending on system issues, so we need to operate on the error buffer
 84 * differently depending on which strerror_r we're using. */
 85#ifndef _GNU_SOURCE
 86/* "regular" POSIX strerror_r that does the right thing. */
 87#define __redis_strerror_r(errno, buf, len)                                    \
 88    do {                                                                       \
 89        strerror_r((errno), (buf), (len));                                     \
 90    } while (0)
 91#else
 92/* "bad" GNU strerror_r we need to clean up after. */
 93#define __redis_strerror_r(errno, buf, len)                                    \
 94    do {                                                                       \
 95        char *err_str = strerror_r((errno), (buf), (len));                     \
 96        /* If return value _isn't_ the start of the buffer we passed in,       \
 97         * then GNU strerror_r returned an internal static buffer and we       \
 98         * need to copy the result into our private buffer. */                 \
 99        if (err_str != (buf)) {                                                \
100            buf[(len)] = '\0';                                                 \
101            strncat((buf), err_str, ((len) - 1));                              \
102        }                                                                      \
103    } while (0)
104#endif
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
110/* This is the reply object returned by redisCommand() */
111typedef struct redisReply {
112    int type; /* REDIS_REPLY_* */
113    long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
114    int len; /* Length of string */
115    char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
116    size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
117    struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
118} redisReply;
119
120redisReader *redisReaderCreate(void);
121
122/* Function to free the reply objects hiredis returns by default. */
123void freeReplyObject(void *reply);
124
125/* Functions to format a command according to the protocol. */
126int redisvFormatCommand(char **target, const char *format, va_list ap);
127int redisFormatCommand(char **target, const char *format, ...);
128int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
129int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
130void redisFreeCommand(char *cmd);
131void redisFreeSdsCommand(sds cmd);
132
133enum redisConnectionType {
134    REDIS_CONN_TCP,
135    REDIS_CONN_UNIX,
136};
137
138/* Context for a connection to Redis */
139typedef struct redisContext {
140    int err; /* Error flags, 0 when there is no error */
141    char errstr[128]; /* String representation of error when applicable */
142    int fd;
143    int flags;
144    char *obuf; /* Write buffer */
145    redisReader *reader; /* Protocol reader */
146
147    enum redisConnectionType connection_type;
148    struct timeval *timeout;
149
150    struct {
151        char *host;
152        char *source_addr;
153        int port;
154    } tcp;
155
156    struct {
157        char *path;
158    } unix_sock;
159
160} redisContext;
161
162redisContext *redisConnect(const char *ip, int port);
163redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
164redisContext *redisConnectNonBlock(const char *ip, int port);
165redisContext *redisConnectBindNonBlock(const char *ip, int port,
166                                       const char *source_addr);
167redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
168                                                const char *source_addr);
169redisContext *redisConnectUnix(const char *path);
170redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
171redisContext *redisConnectUnixNonBlock(const char *path);
172redisContext *redisConnectFd(int fd);
173
174/**
175 * Reconnect the given context using the saved information.
176 *
177 * This re-uses the exact same connect options as in the initial connection.
178 * host, ip (or path), timeout and bind address are reused,
179 * flags are used unmodified from the existing context.
180 *
181 * Returns REDIS_OK on successfull connect or REDIS_ERR otherwise.
182 */
183int redisReconnect(redisContext *c);
184
185int redisSetTimeout(redisContext *c, const struct timeval tv);
186int redisEnableKeepAlive(redisContext *c);
187void redisFree(redisContext *c);
188int redisFreeKeepFd(redisContext *c);
189int redisBufferRead(redisContext *c);
190int redisBufferWrite(redisContext *c, int *done);
191
192/* In a blocking context, this function first checks if there are unconsumed
193 * replies to return and returns one if so. Otherwise, it flushes the output
194 * buffer to the socket and reads until it has a reply. In a non-blocking
195 * context, it will return unconsumed replies until there are no more. */
196int redisGetReply(redisContext *c, void **reply);
197int redisGetReplyFromReader(redisContext *c, void **reply);
198
199/* Write a formatted command to the output buffer. Use these functions in blocking mode
200 * to get a pipeline of commands. */
201int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
202
203/* Write a command to the output buffer. Use these functions in blocking mode
204 * to get a pipeline of commands. */
205int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
206int redisAppendCommand(redisContext *c, const char *format, ...);
207int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
208
209/* Issue a command to Redis. In a blocking context, it is identical to calling
210 * redisAppendCommand, followed by redisGetReply. The function will return
211 * NULL if there was an error in performing the request, otherwise it will
212 * return the reply. In a non-blocking context, it is identical to calling
213 * only redisAppendCommand and will always return NULL. */
214void *redisvCommand(redisContext *c, const char *format, va_list ap);
215void *redisCommand(redisContext *c, const char *format, ...);
216void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif