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-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)arg;
|
2010-11-01 09:17:28 +00:00
|
|
|
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-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)arg;
|
2010-11-01 09:17:28 +00:00
|
|
|
redisAsyncHandleWrite(e->context);
|
|
|
|
}
|
2010-10-07 15:48:03 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibeventAddRead(void *privdata) {
|
2010-11-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
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) {
|
2010-11-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
event_del(&e->rev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisLibeventAddWrite(void *privdata) {
|
2010-11-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)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) {
|
2010-11-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)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) {
|
2010-11-22 00:26:55 +00:00
|
|
|
redisLibeventEvents *e = (redisLibeventEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
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 */
|
2010-12-01 15:43:21 +00:00
|
|
|
if (ac->_adapter_data != NULL)
|
2010-11-01 09:17:28 +00:00
|
|
|
return REDIS_ERR;
|
2010-10-07 15:48:03 +00:00
|
|
|
|
|
|
|
/* Create container for context and r/w events */
|
2010-11-22 00:26:55 +00:00
|
|
|
e = (redisLibeventEvents*)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;
|
2010-12-01 15:43:21 +00:00
|
|
|
ac->_adapter_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
|
|
|
}
|