2010-10-19 15:10:01 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ev.h>
|
2010-11-01 09:42:32 +00:00
|
|
|
#include "../hiredis.h"
|
|
|
|
#include "../async.h"
|
2010-10-19 15:10:01 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
typedef struct redisLibevEvents {
|
|
|
|
redisAsyncContext *context;
|
2010-10-19 15:10:01 +00:00
|
|
|
struct ev_loop *loop;
|
2010-11-01 09:17:28 +00:00
|
|
|
int reading, writing;
|
2010-10-19 15:10:01 +00:00
|
|
|
ev_io rev, wev;
|
2010-11-01 09:17:28 +00:00
|
|
|
} redisLibevEvents;
|
2010-10-19 15:10:01 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevReadEvent(struct ev_loop *loop, ev_io *watcher, int revents) {
|
2010-10-19 15:10:01 +00:00
|
|
|
((void)loop); ((void)revents);
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)watcher->data;
|
2010-11-01 09:17:28 +00:00
|
|
|
redisAsyncHandleRead(e->context);
|
2010-10-19 15:10:01 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevWriteEvent(struct ev_loop *loop, ev_io *watcher, int revents) {
|
2010-10-19 15:10:01 +00:00
|
|
|
((void)loop); ((void)revents);
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)watcher->data;
|
2010-11-01 09:17:28 +00:00
|
|
|
redisAsyncHandleWrite(e->context);
|
|
|
|
}
|
2010-10-19 15:10:01 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevAddRead(void *privdata) {
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
if (!e->reading) {
|
|
|
|
e->reading = 1;
|
|
|
|
ev_io_start(e->loop,&e->rev);
|
2010-10-19 15:10:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevDelRead(void *privdata) {
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
if (e->reading) {
|
|
|
|
e->reading = 0;
|
|
|
|
ev_io_stop(e->loop,&e->rev);
|
|
|
|
}
|
2010-10-19 15:10:01 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevAddWrite(void *privdata) {
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
if (!e->writing) {
|
|
|
|
e->writing = 1;
|
|
|
|
ev_io_start(e->loop,&e->wev);
|
|
|
|
}
|
2010-10-19 15:10:01 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void redisLibevDelWrite(void *privdata) {
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
if (e->writing) {
|
|
|
|
e->writing = 0;
|
|
|
|
ev_io_stop(e->loop,&e->wev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisLibevCleanup(void *privdata) {
|
2010-11-22 08:45:55 +00:00
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
2010-11-01 09:17:28 +00:00
|
|
|
redisLibevDelRead(privdata);
|
|
|
|
redisLibevDelWrite(privdata);
|
2010-10-19 15:10:01 +00:00
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
int redisLibevAttach(redisAsyncContext *ac, struct ev_loop *loop) {
|
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
redisLibevEvents *e;
|
|
|
|
|
|
|
|
/* Nothing should be attached when something is already attached */
|
|
|
|
if (ac->data != NULL)
|
|
|
|
return REDIS_ERR;
|
2010-10-19 15:10:01 +00:00
|
|
|
|
|
|
|
/* Create container for context and r/w events */
|
2010-11-22 08:45:55 +00:00
|
|
|
e = (redisLibevEvents*)malloc(sizeof(*e));
|
2010-11-01 09:17:28 +00:00
|
|
|
e->context = ac;
|
2010-10-19 18:13:10 +00:00
|
|
|
e->loop = loop;
|
2010-11-01 09:17:28 +00:00
|
|
|
e->reading = e->writing = 0;
|
2010-10-19 15:10:01 +00:00
|
|
|
e->rev.data = e;
|
|
|
|
e->wev.data = e;
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
/* Register functions to start/stop listening for events */
|
|
|
|
ac->evAddRead = redisLibevAddRead;
|
|
|
|
ac->evDelRead = redisLibevDelRead;
|
|
|
|
ac->evAddWrite = redisLibevAddWrite;
|
|
|
|
ac->evDelWrite = redisLibevDelWrite;
|
|
|
|
ac->evCleanup = redisLibevCleanup;
|
|
|
|
ac->data = e;
|
2010-10-19 15:10:01 +00:00
|
|
|
|
|
|
|
/* Initialize read/write events */
|
2010-11-01 09:17:28 +00:00
|
|
|
ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
|
|
|
|
ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
|
|
|
|
return REDIS_OK;
|
2010-10-19 15:10:01 +00:00
|
|
|
}
|
2010-11-22 08:45:55 +00:00
|
|
|
|