2014-04-09 01:21:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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;
|
2020-05-04 17:35:30 +00:00
|
|
|
ev_timer timer;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:35:30 +00:00
|
|
|
static void redisLibevStopTimer(void *privdata) {
|
|
|
|
redisLibevEvents *e = (redisLibevEvents*)privdata;
|
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
|
|
|
ev_timer_stop(EV_A_ &e->timer);
|
|
|
|
}
|
|
|
|
|
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);
|
2020-05-04 17:35:30 +00:00
|
|
|
redisLibevStopTimer(privdata);
|
2010-10-19 15:10:01 +00:00
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:35:30 +00:00
|
|
|
static void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) {
|
|
|
|
((void)revents);
|
|
|
|
redisLibevEvents *e = timer->data;
|
|
|
|
redisAsyncHandleTimeout(e->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void redisLibevSetTimeout(void *privdata, struct timeval tv) {
|
|
|
|
redisLibevEvents *e = privdata;
|
|
|
|
struct ev_loop *loop = e->loop;
|
|
|
|
((void)loop);
|
|
|
|
|
|
|
|
if (!ev_is_active(&e->timer)) {
|
|
|
|
ev_init(&e->timer, redisLibevTimeout);
|
|
|
|
e->timer.data = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00;
|
|
|
|
ev_timer_again(EV_A_ &e->timer);
|
|
|
|
}
|
|
|
|
|
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 */
|
2020-05-04 17:35:30 +00:00
|
|
|
e = (redisLibevEvents*)hi_calloc(1, 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-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;
|
2020-05-04 17:35:30 +00:00
|
|
|
ac->ev.scheduleTimer = redisLibevSetTimeout;
|
2010-12-29 14:41:03 +00:00
|
|
|
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
|