]> git.saurik.com Git - redis.git/blame - deps/hiredis/adapters/libevent.h
Query the archive to provide a complete KEYS list.
[redis.git] / deps / hiredis / adapters / libevent.h
CommitLineData
886c9ecb 1/*
2 * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
b66e5add 31#ifndef __HIREDIS_LIBEVENT_H__
32#define __HIREDIS_LIBEVENT_H__
24f753a8
PN
33#include <event.h>
34#include "../hiredis.h"
35#include "../async.h"
36
37typedef struct redisLibeventEvents {
38 redisAsyncContext *context;
39 struct event rev, wev;
40} redisLibeventEvents;
41
b66e5add 42static void redisLibeventReadEvent(int fd, short event, void *arg) {
24f753a8 43 ((void)fd); ((void)event);
a1e97d69 44 redisLibeventEvents *e = (redisLibeventEvents*)arg;
24f753a8
PN
45 redisAsyncHandleRead(e->context);
46}
47
b66e5add 48static void redisLibeventWriteEvent(int fd, short event, void *arg) {
24f753a8 49 ((void)fd); ((void)event);
a1e97d69 50 redisLibeventEvents *e = (redisLibeventEvents*)arg;
24f753a8
PN
51 redisAsyncHandleWrite(e->context);
52}
53
b66e5add 54static void redisLibeventAddRead(void *privdata) {
a1e97d69 55 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
24f753a8
PN
56 event_add(&e->rev,NULL);
57}
58
b66e5add 59static void redisLibeventDelRead(void *privdata) {
a1e97d69 60 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
24f753a8
PN
61 event_del(&e->rev);
62}
63
b66e5add 64static void redisLibeventAddWrite(void *privdata) {
a1e97d69 65 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
24f753a8
PN
66 event_add(&e->wev,NULL);
67}
68
b66e5add 69static void redisLibeventDelWrite(void *privdata) {
a1e97d69 70 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
24f753a8
PN
71 event_del(&e->wev);
72}
73
b66e5add 74static void redisLibeventCleanup(void *privdata) {
a1e97d69 75 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
24f753a8
PN
76 event_del(&e->rev);
77 event_del(&e->wev);
78 free(e);
79}
80
b66e5add 81static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
24f753a8
PN
82 redisContext *c = &(ac->c);
83 redisLibeventEvents *e;
84
85 /* Nothing should be attached when something is already attached */
b66e5add 86 if (ac->ev.data != NULL)
24f753a8
PN
87 return REDIS_ERR;
88
89 /* Create container for context and r/w events */
a1e97d69 90 e = (redisLibeventEvents*)malloc(sizeof(*e));
24f753a8
PN
91 e->context = ac;
92
93 /* Register functions to start/stop listening for events */
b66e5add 94 ac->ev.addRead = redisLibeventAddRead;
95 ac->ev.delRead = redisLibeventDelRead;
96 ac->ev.addWrite = redisLibeventAddWrite;
97 ac->ev.delWrite = redisLibeventDelWrite;
98 ac->ev.cleanup = redisLibeventCleanup;
99 ac->ev.data = e;
24f753a8
PN
100
101 /* Initialize and install read/write events */
102 event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
103 event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e);
104 event_base_set(base,&e->rev);
105 event_base_set(base,&e->wev);
106 return REDIS_OK;
107}
b66e5add 108#endif