2010-10-11 16:34:32 +00:00
|
|
|
#include <sys/types.h>
|
2010-10-07 15:48:03 +00:00
|
|
|
#include <event.h>
|
2010-11-01 09:42:32 +00:00
|
|
|
#include "../hiredis.h"
|
|
|
|
#include "../async.h"
|
2010-10-07 15:48:03 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
typedef struct redisLibeventEvents {
|
|
|
|
redisAsyncContext *context;
|
2010-10-07 15:48:03 +00:00
|
|
|
struct event rev, wev;
|
2010-11-01 09:17:28 +00:00
|
|
|
} redisLibeventEvents;
|
2010-10-07 15:48:03 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventReadEvent(int fd, short event, void *arg) {
|
2010-10-07 15:48:03 +00:00
|
|
|
((void)fd); ((void)event);
|
2010-11-01 09:17:28 +00:00
|
|
|
redisLibeventEvents *e = arg;
|
|
|
|
redisAsyncHandleRead(e->context);
|
2010-10-07 15:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventWriteEvent(int fd, short event, void *arg) {
|
2010-10-07 15:48:03 +00:00
|
|
|
((void)fd); ((void)event);
|
2010-11-01 09:17:28 +00:00
|
|
|
redisLibeventEvents *e = arg;
|
|
|
|
redisAsyncHandleWrite(e->context);
|
|
|
|
}
|
2010-10-07 15:48:03 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventAddRead(void *privdata) {
|
|
|
|
redisLibeventEvents *e = privdata;
|
|
|
|
event_add(&e->rev,NULL);
|
2010-10-07 15:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventDelRead(void *privdata) {
|
|
|
|
redisLibeventEvents *e = privdata;
|
|
|
|
event_del(&e->rev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisLibeventAddWrite(void *privdata) {
|
|
|
|
redisLibeventEvents *e = privdata;
|
2010-10-07 15:48:03 +00:00
|
|
|
event_add(&e->wev,NULL);
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventDelWrite(void *privdata) {
|
|
|
|
redisLibeventEvents *e = privdata;
|
2010-10-07 15:48:03 +00:00
|
|
|
event_del(&e->wev);
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventCleanup(void *privdata) {
|
|
|
|
redisLibeventEvents *e = privdata;
|
|
|
|
event_del(&e->rev);
|
|
|
|
event_del(&e->wev);
|
2010-10-07 15:48:03 +00:00
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
redisLibeventEvents *e;
|
|
|
|
|
|
|
|
/* Nothing should be attached when something is already attached */
|
|
|
|
if (ac->data != NULL)
|
|
|
|
return REDIS_ERR;
|
2010-10-07 15:48:03 +00:00
|
|
|
|
|
|
|
/* Create container for context and r/w events */
|
|
|
|
e = malloc(sizeof(*e));
|
2010-11-01 09:17:28 +00:00
|
|
|
e->context = ac;
|
2010-10-07 15:48:03 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
/* Register functions to start/stop listening for events */
|
|
|
|
ac->evAddRead = redisLibeventAddRead;
|
|
|
|
ac->evDelRead = redisLibeventDelRead;
|
|
|
|
ac->evAddWrite = redisLibeventAddWrite;
|
|
|
|
ac->evDelWrite = redisLibeventDelWrite;
|
|
|
|
ac->evCleanup = redisLibeventCleanup;
|
|
|
|
ac->data = e;
|
2010-10-19 18:13:10 +00:00
|
|
|
|
|
|
|
/* Initialize and install read/write events */
|
2010-11-01 09:17:28 +00:00
|
|
|
event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
|
|
|
|
event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e);
|
2010-10-19 18:13:10 +00:00
|
|
|
event_base_set(base,&e->rev);
|
|
|
|
event_base_set(base,&e->wev);
|
2010-11-01 09:17:28 +00:00
|
|
|
return REDIS_OK;
|
2010-10-07 15:48:03 +00:00
|
|
|
}
|