]> git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libev.h
Add openldap submodule with a symlink for liblmdb.
[redis.git] / deps / hiredis / adapters / libev.h
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_LIBEV_H__
32 #define __HIREDIS_LIBEV_H__
33 #include <stdlib.h>
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
46 static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
47 #if EV_MULTIPLICITY
48 ((void)loop);
49 #endif
50 ((void)revents);
51
52 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
53 redisAsyncHandleRead(e->context);
54 }
55
56 static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
57 #if EV_MULTIPLICITY
58 ((void)loop);
59 #endif
60 ((void)revents);
61
62 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
63 redisAsyncHandleWrite(e->context);
64 }
65
66 static void redisLibevAddRead(void *privdata) {
67 redisLibevEvents *e = (redisLibevEvents*)privdata;
68 struct ev_loop *loop = e->loop;
69 ((void)loop);
70 if (!e->reading) {
71 e->reading = 1;
72 ev_io_start(EV_A_ &e->rev);
73 }
74 }
75
76 static void redisLibevDelRead(void *privdata) {
77 redisLibevEvents *e = (redisLibevEvents*)privdata;
78 struct ev_loop *loop = e->loop;
79 ((void)loop);
80 if (e->reading) {
81 e->reading = 0;
82 ev_io_stop(EV_A_ &e->rev);
83 }
84 }
85
86 static void redisLibevAddWrite(void *privdata) {
87 redisLibevEvents *e = (redisLibevEvents*)privdata;
88 struct ev_loop *loop = e->loop;
89 ((void)loop);
90 if (!e->writing) {
91 e->writing = 1;
92 ev_io_start(EV_A_ &e->wev);
93 }
94 }
95
96 static void redisLibevDelWrite(void *privdata) {
97 redisLibevEvents *e = (redisLibevEvents*)privdata;
98 struct ev_loop *loop = e->loop;
99 ((void)loop);
100 if (e->writing) {
101 e->writing = 0;
102 ev_io_stop(EV_A_ &e->wev);
103 }
104 }
105
106 static void redisLibevCleanup(void *privdata) {
107 redisLibevEvents *e = (redisLibevEvents*)privdata;
108 redisLibevDelRead(privdata);
109 redisLibevDelWrite(privdata);
110 free(e);
111 }
112
113 static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
114 redisContext *c = &(ac->c);
115 redisLibevEvents *e;
116
117 /* Nothing should be attached when something is already attached */
118 if (ac->ev.data != NULL)
119 return REDIS_ERR;
120
121 /* Create container for context and r/w events */
122 e = (redisLibevEvents*)malloc(sizeof(*e));
123 e->context = ac;
124 #if EV_MULTIPLICITY
125 e->loop = loop;
126 #else
127 e->loop = NULL;
128 #endif
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 */
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;
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 }
146
147 #endif