]> git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libev.h
Update hiredis to 0.9.2
[redis.git] / deps / hiredis / adapters / libev.h
1 #include <sys/types.h>
2 #include <ev.h>
3 #include "../hiredis.h"
4 #include "../async.h"
5
6 typedef struct redisLibevEvents {
7 redisAsyncContext *context;
8 struct ev_loop *loop;
9 int reading, writing;
10 ev_io rev, wev;
11 } redisLibevEvents;
12
13 void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
14 #if EV_MULTIPLICITY
15 ((void)loop);
16 #endif
17 ((void)revents);
18
19 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
20 redisAsyncHandleRead(e->context);
21 }
22
23 void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
24 #if EV_MULTIPLICITY
25 ((void)loop);
26 #endif
27 ((void)revents);
28
29 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
30 redisAsyncHandleWrite(e->context);
31 }
32
33 void redisLibevAddRead(void *privdata) {
34 redisLibevEvents *e = (redisLibevEvents*)privdata;
35 struct ev_loop *loop = e->loop;
36 ((void)loop);
37 if (!e->reading) {
38 e->reading = 1;
39 ev_io_start(EV_A_ &e->rev);
40 }
41 }
42
43 void redisLibevDelRead(void *privdata) {
44 redisLibevEvents *e = (redisLibevEvents*)privdata;
45 struct ev_loop *loop = e->loop;
46 ((void)loop);
47 if (e->reading) {
48 e->reading = 0;
49 ev_io_stop(EV_A_ &e->rev);
50 }
51 }
52
53 void redisLibevAddWrite(void *privdata) {
54 redisLibevEvents *e = (redisLibevEvents*)privdata;
55 struct ev_loop *loop = e->loop;
56 ((void)loop);
57 if (!e->writing) {
58 e->writing = 1;
59 ev_io_start(EV_A_ &e->wev);
60 }
61 }
62
63 void redisLibevDelWrite(void *privdata) {
64 redisLibevEvents *e = (redisLibevEvents*)privdata;
65 struct ev_loop *loop = e->loop;
66 ((void)loop);
67 if (e->writing) {
68 e->writing = 0;
69 ev_io_stop(EV_A_ &e->wev);
70 }
71 }
72
73 void redisLibevCleanup(void *privdata) {
74 redisLibevEvents *e = (redisLibevEvents*)privdata;
75 redisLibevDelRead(privdata);
76 redisLibevDelWrite(privdata);
77 free(e);
78 }
79
80 int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
81 redisContext *c = &(ac->c);
82 redisLibevEvents *e;
83
84 /* Nothing should be attached when something is already attached */
85 if (ac->_adapter_data != NULL)
86 return REDIS_ERR;
87
88 /* Create container for context and r/w events */
89 e = (redisLibevEvents*)malloc(sizeof(*e));
90 e->context = ac;
91 #if EV_MULTIPLICITY
92 e->loop = loop;
93 #else
94 e->loop = NULL;
95 #endif
96 e->reading = e->writing = 0;
97 e->rev.data = e;
98 e->wev.data = e;
99
100 /* Register functions to start/stop listening for events */
101 ac->evAddRead = redisLibevAddRead;
102 ac->evDelRead = redisLibevDelRead;
103 ac->evAddWrite = redisLibevAddWrite;
104 ac->evDelWrite = redisLibevDelWrite;
105 ac->evCleanup = redisLibevCleanup;
106 ac->_adapter_data = e;
107
108 /* Initialize read/write events */
109 ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
110 ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
111 return REDIS_OK;
112 }
113