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-23 14:55:27 +00:00
|
|
|
void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
|
|
|
|
#if EV_MULTIPLICITY
|
|
|
|
((void)loop);
|
|
|
|
#endif
|
|
|
|
((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-23 14:55:27 +00:00
|
|
|
void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
|
|
|
|
#if EV_MULTIPLICITY
|
|
|
|
((void)loop);
|
|
|
|
#endif
|
|
|
|
((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-23 14:55:27 +00:00
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
2010-11-01 09:17:28 +00:00
|
|
|
if (!e->reading) {
|
|
|
|
e->reading = 1;
|
2010-11-23 14:55:27 +00:00
|
|
|
ev_io_start(EV_A_ &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-23 14:55:27 +00:00
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
2010-11-01 09:17:28 +00:00
|
|
|
if (e->reading) {
|
|
|
|
e->reading = 0;
|
2010-11-23 14:55:27 +00:00
|
|
|
ev_io_stop(EV_A_ &e->rev);
|
2010-11-01 09:17:28 +00:00
|
|
|
}
|
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-23 14:55:27 +00:00
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
2010-11-01 09:17:28 +00:00
|
|
|
if (!e->writing) {
|
|
|
|
e->writing = 1;
|
2010-11-23 14:55:27 +00:00
|
|
|
ev_io_start(EV_A_ &e->wev);
|
2010-11-01 09:17:28 +00:00
|
|
|
}
|
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-23 14:55:27 +00:00
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
2010-11-01 09:17:28 +00:00
|
|
|
if (e->writing) {
|
|
|
|
e->writing = 0;
|
2010-11-23 14:55:27 +00:00
|
|
|
ev_io_stop(EV_A_ &e->wev);
|
2010-11-01 09:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 14:55:27 +00:00
|
|
|
int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
|
2010-11-01 09:17:28 +00:00
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
redisLibevEvents *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-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-11-23 14:55:27 +00:00
|
|
|
#if EV_MULTIPLICITY
|
2010-10-19 18:13:10 +00:00
|
|
|
e->loop = loop;
|
2010-11-23 14:55:27 +00:00
|
|
|
#else
|
|
|
|
e->loop = NULL;
|
|
|
|
#endif
|
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;
|
2010-12-01 15:43:21 +00:00
|
|
|
ac->_adapter_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
|
|
|
|