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