]>
Commit | Line | Data |
---|---|---|
24f753a8 PN |
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 | ||
a1e97d69 PN |
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; | |
24f753a8 PN |
20 | redisAsyncHandleRead(e->context); |
21 | } | |
22 | ||
a1e97d69 PN |
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; | |
24f753a8 PN |
30 | redisAsyncHandleWrite(e->context); |
31 | } | |
32 | ||
33 | void redisLibevAddRead(void *privdata) { | |
a1e97d69 PN |
34 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
35 | struct ev_loop *loop = e->loop; | |
36 | ((void)loop); | |
24f753a8 PN |
37 | if (!e->reading) { |
38 | e->reading = 1; | |
a1e97d69 | 39 | ev_io_start(EV_A_ &e->rev); |
24f753a8 PN |
40 | } |
41 | } | |
42 | ||
43 | void redisLibevDelRead(void *privdata) { | |
a1e97d69 PN |
44 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
45 | struct ev_loop *loop = e->loop; | |
46 | ((void)loop); | |
24f753a8 PN |
47 | if (e->reading) { |
48 | e->reading = 0; | |
a1e97d69 | 49 | ev_io_stop(EV_A_ &e->rev); |
24f753a8 PN |
50 | } |
51 | } | |
52 | ||
53 | void redisLibevAddWrite(void *privdata) { | |
a1e97d69 PN |
54 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
55 | struct ev_loop *loop = e->loop; | |
56 | ((void)loop); | |
24f753a8 PN |
57 | if (!e->writing) { |
58 | e->writing = 1; | |
a1e97d69 | 59 | ev_io_start(EV_A_ &e->wev); |
24f753a8 PN |
60 | } |
61 | } | |
62 | ||
63 | void redisLibevDelWrite(void *privdata) { | |
a1e97d69 PN |
64 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
65 | struct ev_loop *loop = e->loop; | |
66 | ((void)loop); | |
24f753a8 PN |
67 | if (e->writing) { |
68 | e->writing = 0; | |
a1e97d69 | 69 | ev_io_stop(EV_A_ &e->wev); |
24f753a8 PN |
70 | } |
71 | } | |
72 | ||
73 | void redisLibevCleanup(void *privdata) { | |
a1e97d69 | 74 | redisLibevEvents *e = (redisLibevEvents*)privdata; |
24f753a8 PN |
75 | redisLibevDelRead(privdata); |
76 | redisLibevDelWrite(privdata); | |
77 | free(e); | |
78 | } | |
79 | ||
a1e97d69 | 80 | int redisLibevAttach(EV_P_ redisAsyncContext *ac) { |
24f753a8 PN |
81 | redisContext *c = &(ac->c); |
82 | redisLibevEvents *e; | |
83 | ||
84 | /* Nothing should be attached when something is already attached */ | |
a1e97d69 | 85 | if (ac->_adapter_data != NULL) |
24f753a8 PN |
86 | return REDIS_ERR; |
87 | ||
88 | /* Create container for context and r/w events */ | |
a1e97d69 | 89 | e = (redisLibevEvents*)malloc(sizeof(*e)); |
24f753a8 | 90 | e->context = ac; |
a1e97d69 | 91 | #if EV_MULTIPLICITY |
24f753a8 | 92 | e->loop = loop; |
a1e97d69 PN |
93 | #else |
94 | e->loop = NULL; | |
95 | #endif | |
24f753a8 PN |
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; | |
a1e97d69 | 106 | ac->_adapter_data = e; |
24f753a8 PN |
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 | } | |
a1e97d69 | 113 |