]>
Commit | Line | Data |
---|---|---|
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_LIBEV_H__ |
32 | #define __HIREDIS_LIBEV_H__ | |
33 | #include <stdlib.h> | |
24f753a8 PN |
34 | #include <sys/types.h> |
35 | #include <ev.h> | |
36 | #include "../hiredis.h" | |
37 | #include "../async.h" | |
38 | ||
39 | typedef struct redisLibevEvents { | |
40 | redisAsyncContext *context; | |
41 | struct ev_loop *loop; | |
42 | int reading, writing; | |
43 | ev_io rev, wev; | |
44 | } redisLibevEvents; | |
45 | ||
b66e5add | 46 | static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) { |
a1e97d69 PN |
47 | #if EV_MULTIPLICITY |
48 | ((void)loop); | |
49 | #endif | |
50 | ((void)revents); | |
51 | ||
52 | redisLibevEvents *e = (redisLibevEvents*)watcher->data; | |
24f753a8 PN |
53 | redisAsyncHandleRead(e->context); |
54 | } | |
55 | ||
b66e5add | 56 | static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) { |
a1e97d69 PN |
57 | #if EV_MULTIPLICITY |
58 | ((void)loop); | |
59 | #endif | |
60 | ((void)revents); | |
61 | ||
62 | redisLibevEvents *e = (redisLibevEvents*)watcher->data; | |
24f753a8 PN |
63 | redisAsyncHandleWrite(e->context); |
64 | } | |
65 | ||
b66e5add | 66 | static void redisLibevAddRead(void *privdata) { |
a1e97d69 PN |
67 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
68 | struct ev_loop *loop = e->loop; | |
69 | ((void)loop); | |
24f753a8 PN |
70 | if (!e->reading) { |
71 | e->reading = 1; | |
a1e97d69 | 72 | ev_io_start(EV_A_ &e->rev); |
24f753a8 PN |
73 | } |
74 | } | |
75 | ||
b66e5add | 76 | static void redisLibevDelRead(void *privdata) { |
a1e97d69 PN |
77 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
78 | struct ev_loop *loop = e->loop; | |
79 | ((void)loop); | |
24f753a8 PN |
80 | if (e->reading) { |
81 | e->reading = 0; | |
a1e97d69 | 82 | ev_io_stop(EV_A_ &e->rev); |
24f753a8 PN |
83 | } |
84 | } | |
85 | ||
b66e5add | 86 | static void redisLibevAddWrite(void *privdata) { |
a1e97d69 PN |
87 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
88 | struct ev_loop *loop = e->loop; | |
89 | ((void)loop); | |
24f753a8 PN |
90 | if (!e->writing) { |
91 | e->writing = 1; | |
a1e97d69 | 92 | ev_io_start(EV_A_ &e->wev); |
24f753a8 PN |
93 | } |
94 | } | |
95 | ||
b66e5add | 96 | static void redisLibevDelWrite(void *privdata) { |
a1e97d69 PN |
97 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
98 | struct ev_loop *loop = e->loop; | |
99 | ((void)loop); | |
24f753a8 PN |
100 | if (e->writing) { |
101 | e->writing = 0; | |
a1e97d69 | 102 | ev_io_stop(EV_A_ &e->wev); |
24f753a8 PN |
103 | } |
104 | } | |
105 | ||
b66e5add | 106 | static void redisLibevCleanup(void *privdata) { |
a1e97d69 | 107 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
24f753a8 PN |
108 | redisLibevDelRead(privdata); |
109 | redisLibevDelWrite(privdata); | |
110 | free(e); | |
111 | } | |
112 | ||
b66e5add | 113 | static int redisLibevAttach(EV_P_ redisAsyncContext *ac) { |
24f753a8 PN |
114 | redisContext *c = &(ac->c); |
115 | redisLibevEvents *e; | |
116 | ||
117 | /* Nothing should be attached when something is already attached */ | |
b66e5add | 118 | if (ac->ev.data != NULL) |
24f753a8 PN |
119 | return REDIS_ERR; |
120 | ||
121 | /* Create container for context and r/w events */ | |
a1e97d69 | 122 | e = (redisLibevEvents*)malloc(sizeof(*e)); |
24f753a8 | 123 | e->context = ac; |
a1e97d69 | 124 | #if EV_MULTIPLICITY |
24f753a8 | 125 | e->loop = loop; |
a1e97d69 PN |
126 | #else |
127 | e->loop = NULL; | |
128 | #endif | |
24f753a8 PN |
129 | e->reading = e->writing = 0; |
130 | e->rev.data = e; | |
131 | e->wev.data = e; | |
132 | ||
133 | /* Register functions to start/stop listening for events */ | |
b66e5add | 134 | ac->ev.addRead = redisLibevAddRead; |
135 | ac->ev.delRead = redisLibevDelRead; | |
136 | ac->ev.addWrite = redisLibevAddWrite; | |
137 | ac->ev.delWrite = redisLibevDelWrite; | |
138 | ac->ev.cleanup = redisLibevCleanup; | |
139 | ac->ev.data = e; | |
24f753a8 PN |
140 | |
141 | /* Initialize read/write events */ | |
142 | ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); | |
143 | ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); | |
144 | return REDIS_OK; | |
145 | } | |
a1e97d69 | 146 | |
b66e5add | 147 | #endif |