2011-06-27 19:13:11 +00:00
|
|
|
#ifndef __HIREDIS_LIBEV_H__
|
|
|
|
#define __HIREDIS_LIBEV_H__
|
2011-05-21 00:08:36 +00:00
|
|
|
#include <stdlib.h>
|
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
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
|
2010-11-23 14:55:27 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
|
2010-11-23 14:55:27 +00:00
|
|
|
#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
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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);
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
static 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-29 14:41:03 +00:00
|
|
|
if (ac->ev.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 */
|
2010-12-29 14:41:03 +00:00
|
|
|
ac->ev.addRead = redisLibevAddRead;
|
|
|
|
ac->ev.delRead = redisLibevDelRead;
|
|
|
|
ac->ev.addWrite = redisLibevAddWrite;
|
|
|
|
ac->ev.delWrite = redisLibevDelWrite;
|
|
|
|
ac->ev.cleanup = redisLibevCleanup;
|
|
|
|
ac->ev.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
|
|
|
|
2011-06-27 19:13:11 +00:00
|
|
|
#endif
|