]>
Commit | Line | Data |
---|---|---|
24f753a8 PN |
1 | #include <sys/types.h> |
2 | #include <event.h> | |
3 | #include "../hiredis.h" | |
4 | #include "../async.h" | |
5 | ||
6 | typedef struct redisLibeventEvents { | |
7 | redisAsyncContext *context; | |
8 | struct event rev, wev; | |
9 | } redisLibeventEvents; | |
10 | ||
11 | void redisLibeventReadEvent(int fd, short event, void *arg) { | |
12 | ((void)fd); ((void)event); | |
13 | redisLibeventEvents *e = arg; | |
14 | redisAsyncHandleRead(e->context); | |
15 | } | |
16 | ||
17 | void redisLibeventWriteEvent(int fd, short event, void *arg) { | |
18 | ((void)fd); ((void)event); | |
19 | redisLibeventEvents *e = arg; | |
20 | redisAsyncHandleWrite(e->context); | |
21 | } | |
22 | ||
23 | void redisLibeventAddRead(void *privdata) { | |
24 | redisLibeventEvents *e = privdata; | |
25 | event_add(&e->rev,NULL); | |
26 | } | |
27 | ||
28 | void redisLibeventDelRead(void *privdata) { | |
29 | redisLibeventEvents *e = privdata; | |
30 | event_del(&e->rev); | |
31 | } | |
32 | ||
33 | void redisLibeventAddWrite(void *privdata) { | |
34 | redisLibeventEvents *e = privdata; | |
35 | event_add(&e->wev,NULL); | |
36 | } | |
37 | ||
38 | void redisLibeventDelWrite(void *privdata) { | |
39 | redisLibeventEvents *e = privdata; | |
40 | event_del(&e->wev); | |
41 | } | |
42 | ||
43 | void redisLibeventCleanup(void *privdata) { | |
44 | redisLibeventEvents *e = privdata; | |
45 | event_del(&e->rev); | |
46 | event_del(&e->wev); | |
47 | free(e); | |
48 | } | |
49 | ||
50 | int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) { | |
51 | redisContext *c = &(ac->c); | |
52 | redisLibeventEvents *e; | |
53 | ||
54 | /* Nothing should be attached when something is already attached */ | |
55 | if (ac->data != NULL) | |
56 | return REDIS_ERR; | |
57 | ||
58 | /* Create container for context and r/w events */ | |
59 | e = malloc(sizeof(*e)); | |
60 | e->context = ac; | |
61 | ||
62 | /* Register functions to start/stop listening for events */ | |
63 | ac->evAddRead = redisLibeventAddRead; | |
64 | ac->evDelRead = redisLibeventDelRead; | |
65 | ac->evAddWrite = redisLibeventAddWrite; | |
66 | ac->evDelWrite = redisLibeventDelWrite; | |
67 | ac->evCleanup = redisLibeventCleanup; | |
68 | ac->data = e; | |
69 | ||
70 | /* Initialize and install read/write events */ | |
71 | event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); | |
72 | event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e); | |
73 | event_base_set(base,&e->rev); | |
74 | event_base_set(base,&e->wev); | |
75 | return REDIS_OK; | |
76 | } |