]>
git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libevent.h
1 #ifndef __HIREDIS_LIBEVENT_H__
2 #define __HIREDIS_LIBEVENT_H__
4 #include "../hiredis.h"
7 typedef struct redisLibeventEvents
{
8 redisAsyncContext
*context
;
10 } redisLibeventEvents
;
12 static void redisLibeventReadEvent(int fd
, short event
, void *arg
) {
13 ((void)fd
); ((void)event
);
14 redisLibeventEvents
*e
= (redisLibeventEvents
*)arg
;
15 redisAsyncHandleRead(e
->context
);
18 static void redisLibeventWriteEvent(int fd
, short event
, void *arg
) {
19 ((void)fd
); ((void)event
);
20 redisLibeventEvents
*e
= (redisLibeventEvents
*)arg
;
21 redisAsyncHandleWrite(e
->context
);
24 static void redisLibeventAddRead(void *privdata
) {
25 redisLibeventEvents
*e
= (redisLibeventEvents
*)privdata
;
26 event_add(&e
->rev
,NULL
);
29 static void redisLibeventDelRead(void *privdata
) {
30 redisLibeventEvents
*e
= (redisLibeventEvents
*)privdata
;
34 static void redisLibeventAddWrite(void *privdata
) {
35 redisLibeventEvents
*e
= (redisLibeventEvents
*)privdata
;
36 event_add(&e
->wev
,NULL
);
39 static void redisLibeventDelWrite(void *privdata
) {
40 redisLibeventEvents
*e
= (redisLibeventEvents
*)privdata
;
44 static void redisLibeventCleanup(void *privdata
) {
45 redisLibeventEvents
*e
= (redisLibeventEvents
*)privdata
;
51 static int redisLibeventAttach(redisAsyncContext
*ac
, struct event_base
*base
) {
52 redisContext
*c
= &(ac
->c
);
53 redisLibeventEvents
*e
;
55 /* Nothing should be attached when something is already attached */
56 if (ac
->ev
.data
!= NULL
)
59 /* Create container for context and r/w events */
60 e
= (redisLibeventEvents
*)malloc(sizeof(*e
));
63 /* Register functions to start/stop listening for events */
64 ac
->ev
.addRead
= redisLibeventAddRead
;
65 ac
->ev
.delRead
= redisLibeventDelRead
;
66 ac
->ev
.addWrite
= redisLibeventAddWrite
;
67 ac
->ev
.delWrite
= redisLibeventDelWrite
;
68 ac
->ev
.cleanup
= redisLibeventCleanup
;
71 /* Initialize and install read/write events */
72 event_set(&e
->rev
,c
->fd
,EV_READ
,redisLibeventReadEvent
,e
);
73 event_set(&e
->wev
,c
->fd
,EV_WRITE
,redisLibeventWriteEvent
,e
);
74 event_base_set(base
,&e
->rev
);
75 event_base_set(base
,&e
->wev
);