]> git.saurik.com Git - redis.git/blame - deps/hiredis/async.h
Add hiredis dependency for redis-cli, redis-benchmark, etc
[redis.git] / deps / hiredis / async.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_ASYNC_H
31#define __HIREDIS_ASYNC_H
32#include "hiredis.h"
33
34struct redisAsyncContext; /* need forward declaration of redisAsyncContext */
35
36/* Reply callback prototype and container */
37typedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*);
38typedef struct redisCallback {
39 struct redisCallback *next; /* simple singly linked list */
40 redisCallbackFn *fn;
41 void *privdata;
42} redisCallback;
43
44/* List of callbacks for either regular replies or pub/sub */
45typedef struct redisCallbackList {
46 redisCallback *head, *tail;
47} redisCallbackList;
48
49/* Disconnect callback prototype */
50typedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status);
51
52/* Context for an async connection to Redis */
53typedef struct redisAsyncContext {
54 /* Hold the regular context, so it can be realloc'ed. */
55 redisContext c;
56
57 /* Setup error flags so they can be used directly. */
58 int err;
59 char *errstr;
60
61 /* Called when the library expects to start reading/writing.
62 * The supplied functions should be idempotent. */
63 void (*evAddRead)(void *privdata);
64 void (*evDelRead)(void *privdata);
65 void (*evAddWrite)(void *privdata);
66 void (*evDelWrite)(void *privdata);
67 void (*evCleanup)(void *privdata);
68 void *data;
69
70 /* Called when either the connection is terminated due to an error or per
71 * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
72 redisDisconnectCallback *onDisconnect;
73
74 /* Reply callbacks */
75 redisCallbackList replies;
76} redisAsyncContext;
77
78/* Functions that proxy to hiredis */
79redisAsyncContext *redisAsyncConnect(const char *ip, int port);
80int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFunctions *fn);
81int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);
82void redisAsyncDisconnect(redisAsyncContext *ac);
83
84/* Handle read/write events */
85void redisAsyncHandleRead(redisAsyncContext *ac);
86void redisAsyncHandleWrite(redisAsyncContext *ac);
87
88/* Command functions for an async context. Write the command to the
89 * output buffer and register the provided callback. */
90int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap);
91int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...);
92int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);
93
94#endif