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