]> git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libevent.h
Sentinel: SENTINEL FAILOVER command implemented.
[redis.git] / deps / hiredis / adapters / libevent.h
1 #ifndef __HIREDIS_LIBEVENT_H__
2 #define __HIREDIS_LIBEVENT_H__
3 #include <event.h>
4 #include "../hiredis.h"
5 #include "../async.h"
6
7 typedef struct redisLibeventEvents {
8 redisAsyncContext *context;
9 struct event rev, wev;
10 } redisLibeventEvents;
11
12 static void redisLibeventReadEvent(int fd, short event, void *arg) {
13 ((void)fd); ((void)event);
14 redisLibeventEvents *e = (redisLibeventEvents*)arg;
15 redisAsyncHandleRead(e->context);
16 }
17
18 static void redisLibeventWriteEvent(int fd, short event, void *arg) {
19 ((void)fd); ((void)event);
20 redisLibeventEvents *e = (redisLibeventEvents*)arg;
21 redisAsyncHandleWrite(e->context);
22 }
23
24 static void redisLibeventAddRead(void *privdata) {
25 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
26 event_add(&e->rev,NULL);
27 }
28
29 static void redisLibeventDelRead(void *privdata) {
30 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
31 event_del(&e->rev);
32 }
33
34 static void redisLibeventAddWrite(void *privdata) {
35 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
36 event_add(&e->wev,NULL);
37 }
38
39 static void redisLibeventDelWrite(void *privdata) {
40 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
41 event_del(&e->wev);
42 }
43
44 static void redisLibeventCleanup(void *privdata) {
45 redisLibeventEvents *e = (redisLibeventEvents*)privdata;
46 event_del(&e->rev);
47 event_del(&e->wev);
48 free(e);
49 }
50
51 static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
52 redisContext *c = &(ac->c);
53 redisLibeventEvents *e;
54
55 /* Nothing should be attached when something is already attached */
56 if (ac->ev.data != NULL)
57 return REDIS_ERR;
58
59 /* Create container for context and r/w events */
60 e = (redisLibeventEvents*)malloc(sizeof(*e));
61 e->context = ac;
62
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;
69 ac->ev.data = e;
70
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);
76 return REDIS_OK;
77 }
78 #endif