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 
31 #ifndef __HIREDIS_LIBEVENT_H__
32 #define __HIREDIS_LIBEVENT_H__
33 #include <event2/event.h>
34 #include "../hiredis.h"
35 #include "../async.h"
36 
37 #define REDIS_LIBEVENT_DELETED 0x01
38 #define REDIS_LIBEVENT_ENTERED 0x02
39 
40 typedef struct redisLibeventEvents {
41     redisAsyncContext *context;
42     struct event *ev;
43     struct event_base *base;
44     struct timeval tv;
45     short flags;
46     short state;
47 } redisLibeventEvents;
48 
redisLibeventDestroy(redisLibeventEvents * e)49 static void redisLibeventDestroy(redisLibeventEvents *e) {
50     hi_free(e);
51 }
52 
redisLibeventHandler(int fd,short event,void * arg)53 static void redisLibeventHandler(int fd, short event, void *arg) {
54     ((void)fd);
55     redisLibeventEvents *e = (redisLibeventEvents*)arg;
56     e->state |= REDIS_LIBEVENT_ENTERED;
57 
58     #define CHECK_DELETED() if (e->state & REDIS_LIBEVENT_DELETED) {\
59         redisLibeventDestroy(e);\
60         return; \
61     }
62 
63     if ((event & EV_TIMEOUT) && (e->state & REDIS_LIBEVENT_DELETED) == 0) {
64         redisAsyncHandleTimeout(e->context);
65         CHECK_DELETED();
66     }
67 
68     if ((event & EV_READ) && e->context && (e->state & REDIS_LIBEVENT_DELETED) == 0) {
69         redisAsyncHandleRead(e->context);
70         CHECK_DELETED();
71     }
72 
73     if ((event & EV_WRITE) && e->context && (e->state & REDIS_LIBEVENT_DELETED) == 0) {
74         redisAsyncHandleWrite(e->context);
75         CHECK_DELETED();
76     }
77 
78     e->state &= ~REDIS_LIBEVENT_ENTERED;
79     #undef CHECK_DELETED
80 }
81 
redisLibeventUpdate(void * privdata,short flag,int isRemove)82 static void redisLibeventUpdate(void *privdata, short flag, int isRemove) {
83     redisLibeventEvents *e = (redisLibeventEvents *)privdata;
84     const struct timeval *tv = e->tv.tv_sec || e->tv.tv_usec ? &e->tv : NULL;
85 
86     if (isRemove) {
87         if ((e->flags & flag) == 0) {
88             return;
89         } else {
90             e->flags &= ~flag;
91         }
92     } else {
93         if (e->flags & flag) {
94             return;
95         } else {
96             e->flags |= flag;
97         }
98     }
99 
100     event_del(e->ev);
101     event_assign(e->ev, e->base, e->context->c.fd, e->flags | EV_PERSIST,
102                  redisLibeventHandler, privdata);
103     event_add(e->ev, tv);
104 }
105 
redisLibeventAddRead(void * privdata)106 static void redisLibeventAddRead(void *privdata) {
107     redisLibeventUpdate(privdata, EV_READ, 0);
108 }
109 
redisLibeventDelRead(void * privdata)110 static void redisLibeventDelRead(void *privdata) {
111     redisLibeventUpdate(privdata, EV_READ, 1);
112 }
113 
redisLibeventAddWrite(void * privdata)114 static void redisLibeventAddWrite(void *privdata) {
115     redisLibeventUpdate(privdata, EV_WRITE, 0);
116 }
117 
redisLibeventDelWrite(void * privdata)118 static void redisLibeventDelWrite(void *privdata) {
119     redisLibeventUpdate(privdata, EV_WRITE, 1);
120 }
121 
redisLibeventCleanup(void * privdata)122 static void redisLibeventCleanup(void *privdata) {
123     redisLibeventEvents *e = (redisLibeventEvents*)privdata;
124     if (!e) {
125         return;
126     }
127     event_del(e->ev);
128     event_free(e->ev);
129     e->ev = NULL;
130 
131     if (e->state & REDIS_LIBEVENT_ENTERED) {
132         e->state |= REDIS_LIBEVENT_DELETED;
133     } else {
134         redisLibeventDestroy(e);
135     }
136 }
137 
redisLibeventSetTimeout(void * privdata,struct timeval tv)138 static void redisLibeventSetTimeout(void *privdata, struct timeval tv) {
139     redisLibeventEvents *e = (redisLibeventEvents *)privdata;
140     short flags = e->flags;
141     e->flags = 0;
142     e->tv = tv;
143     redisLibeventUpdate(e, flags, 0);
144 }
145 
redisLibeventAttach(redisAsyncContext * ac,struct event_base * base)146 static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
147     redisContext *c = &(ac->c);
148     redisLibeventEvents *e;
149 
150     /* Nothing should be attached when something is already attached */
151     if (ac->ev.data != NULL)
152         return REDIS_ERR;
153 
154     /* Create container for context and r/w events */
155     e = (redisLibeventEvents*)hi_calloc(1, sizeof(*e));
156     if (e == NULL)
157         return REDIS_ERR;
158 
159     e->context = ac;
160 
161     /* Register functions to start/stop listening for events */
162     ac->ev.addRead = redisLibeventAddRead;
163     ac->ev.delRead = redisLibeventDelRead;
164     ac->ev.addWrite = redisLibeventAddWrite;
165     ac->ev.delWrite = redisLibeventDelWrite;
166     ac->ev.cleanup = redisLibeventCleanup;
167     ac->ev.scheduleTimer = redisLibeventSetTimeout;
168     ac->ev.data = e;
169 
170     /* Initialize and install read/write events */
171     e->ev = event_new(base, c->fd, EV_READ | EV_WRITE, redisLibeventHandler, e);
172     e->base = base;
173     return REDIS_OK;
174 }
175 #endif
176