]>
git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libev.h
1 #ifndef __HIREDIS_LIBEV_H__
2 #define __HIREDIS_LIBEV_H__
6 #include "../hiredis.h"
9 typedef struct redisLibevEvents
{
10 redisAsyncContext
*context
;
16 static void redisLibevReadEvent(EV_P_ ev_io
*watcher
, int revents
) {
22 redisLibevEvents
*e
= (redisLibevEvents
*)watcher
->data
;
23 redisAsyncHandleRead(e
->context
);
26 static void redisLibevWriteEvent(EV_P_ ev_io
*watcher
, int revents
) {
32 redisLibevEvents
*e
= (redisLibevEvents
*)watcher
->data
;
33 redisAsyncHandleWrite(e
->context
);
36 static void redisLibevAddRead(void *privdata
) {
37 redisLibevEvents
*e
= (redisLibevEvents
*)privdata
;
38 struct ev_loop
*loop
= e
->loop
;
42 ev_io_start(EV_A_
&e
->rev
);
46 static void redisLibevDelRead(void *privdata
) {
47 redisLibevEvents
*e
= (redisLibevEvents
*)privdata
;
48 struct ev_loop
*loop
= e
->loop
;
52 ev_io_stop(EV_A_
&e
->rev
);
56 static void redisLibevAddWrite(void *privdata
) {
57 redisLibevEvents
*e
= (redisLibevEvents
*)privdata
;
58 struct ev_loop
*loop
= e
->loop
;
62 ev_io_start(EV_A_
&e
->wev
);
66 static void redisLibevDelWrite(void *privdata
) {
67 redisLibevEvents
*e
= (redisLibevEvents
*)privdata
;
68 struct ev_loop
*loop
= e
->loop
;
72 ev_io_stop(EV_A_
&e
->wev
);
76 static void redisLibevCleanup(void *privdata
) {
77 redisLibevEvents
*e
= (redisLibevEvents
*)privdata
;
78 redisLibevDelRead(privdata
);
79 redisLibevDelWrite(privdata
);
83 static int redisLibevAttach(EV_P_ redisAsyncContext
*ac
) {
84 redisContext
*c
= &(ac
->c
);
87 /* Nothing should be attached when something is already attached */
88 if (ac
->ev
.data
!= NULL
)
91 /* Create container for context and r/w events */
92 e
= (redisLibevEvents
*)malloc(sizeof(*e
));
99 e
->reading
= e
->writing
= 0;
103 /* Register functions to start/stop listening for events */
104 ac
->ev
.addRead
= redisLibevAddRead
;
105 ac
->ev
.delRead
= redisLibevDelRead
;
106 ac
->ev
.addWrite
= redisLibevAddWrite
;
107 ac
->ev
.delWrite
= redisLibevDelWrite
;
108 ac
->ev
.cleanup
= redisLibevCleanup
;
111 /* Initialize read/write events */
112 ev_io_init(&e
->rev
,redisLibevReadEvent
,c
->fd
,EV_READ
);
113 ev_io_init(&e
->wev
,redisLibevWriteEvent
,c
->fd
,EV_WRITE
);