]>
Commit | Line | Data |
---|---|---|
021321e0 PN |
1 | #include <sys/types.h> |
2 | #include <ae.h> | |
3 | #include "../hiredis.h" | |
4 | #include "../async.h" | |
5 | ||
6 | typedef struct redisAeEvents { | |
7 | redisAsyncContext *context; | |
8 | aeEventLoop *loop; | |
9 | int fd; | |
10 | int reading, writing; | |
11 | } redisAeEvents; | |
12 | ||
13 | void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) { | |
14 | ((void)el); ((void)fd); ((void)mask); | |
15 | ||
16 | redisAeEvents *e = (redisAeEvents*)privdata; | |
17 | redisAsyncHandleRead(e->context); | |
18 | } | |
19 | ||
20 | void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) { | |
21 | ((void)el); ((void)fd); ((void)mask); | |
22 | ||
23 | redisAeEvents *e = (redisAeEvents*)privdata; | |
24 | redisAsyncHandleWrite(e->context); | |
25 | } | |
26 | ||
27 | void redisAeAddRead(void *privdata) { | |
28 | redisAeEvents *e = (redisAeEvents*)privdata; | |
29 | aeEventLoop *loop = e->loop; | |
30 | if (!e->reading) { | |
31 | e->reading = 1; | |
32 | aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e); | |
33 | } | |
34 | } | |
35 | ||
36 | void redisAeDelRead(void *privdata) { | |
37 | redisAeEvents *e = (redisAeEvents*)privdata; | |
38 | aeEventLoop *loop = e->loop; | |
39 | if (e->reading) { | |
40 | e->reading = 0; | |
41 | aeDeleteFileEvent(loop,e->fd,AE_READABLE); | |
42 | } | |
43 | } | |
44 | ||
45 | void redisAeAddWrite(void *privdata) { | |
46 | redisAeEvents *e = (redisAeEvents*)privdata; | |
47 | aeEventLoop *loop = e->loop; | |
48 | if (!e->writing) { | |
49 | e->writing = 1; | |
50 | aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e); | |
51 | } | |
52 | } | |
53 | ||
54 | void redisAeDelWrite(void *privdata) { | |
55 | redisAeEvents *e = (redisAeEvents*)privdata; | |
56 | aeEventLoop *loop = e->loop; | |
57 | if (e->writing) { | |
58 | e->writing = 0; | |
59 | aeDeleteFileEvent(loop,e->fd,AE_WRITABLE); | |
60 | } | |
61 | } | |
62 | ||
63 | void redisAeCleanup(void *privdata) { | |
64 | redisAeEvents *e = (redisAeEvents*)privdata; | |
65 | redisAeDelRead(privdata); | |
66 | redisAeDelWrite(privdata); | |
67 | free(e); | |
68 | } | |
69 | ||
70 | int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) { | |
71 | redisContext *c = &(ac->c); | |
72 | redisAeEvents *e; | |
73 | ||
74 | /* Nothing should be attached when something is already attached */ | |
75 | if (ac->_adapter_data != NULL) | |
76 | return REDIS_ERR; | |
77 | ||
78 | /* Create container for context and r/w events */ | |
79 | e = (redisAeEvents*)malloc(sizeof(*e)); | |
80 | e->context = ac; | |
81 | e->loop = loop; | |
82 | e->fd = c->fd; | |
83 | e->reading = e->writing = 0; | |
84 | ||
85 | /* Register functions to start/stop listening for events */ | |
86 | ac->evAddRead = redisAeAddRead; | |
87 | ac->evDelRead = redisAeDelRead; | |
88 | ac->evAddWrite = redisAeAddWrite; | |
89 | ac->evDelWrite = redisAeDelWrite; | |
90 | ac->evCleanup = redisAeCleanup; | |
91 | ac->_adapter_data = e; | |
92 | ||
93 | return REDIS_OK; | |
94 | } | |
95 |