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:40:12 +00:00
|
|
|
#ifndef __HIREDIS_AE_H__
|
|
|
|
#define __HIREDIS_AE_H__
|
2010-12-01 12:47:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ae.h>
|
|
|
|
#include "../hiredis.h"
|
|
|
|
#include "../async.h"
|
|
|
|
|
|
|
|
typedef struct redisAeEvents {
|
|
|
|
redisAsyncContext *context;
|
|
|
|
aeEventLoop *loop;
|
|
|
|
int fd;
|
|
|
|
int reading, writing;
|
|
|
|
} redisAeEvents;
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
|
2010-12-01 12:47:58 +00:00
|
|
|
((void)el); ((void)fd); ((void)mask);
|
|
|
|
|
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
redisAsyncHandleRead(e->context);
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
|
2010-12-01 12:47:58 +00:00
|
|
|
((void)el); ((void)fd); ((void)mask);
|
|
|
|
|
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
redisAsyncHandleWrite(e->context);
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeAddRead(void *privdata) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
aeEventLoop *loop = e->loop;
|
|
|
|
if (!e->reading) {
|
|
|
|
e->reading = 1;
|
|
|
|
aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeDelRead(void *privdata) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
aeEventLoop *loop = e->loop;
|
|
|
|
if (e->reading) {
|
|
|
|
e->reading = 0;
|
|
|
|
aeDeleteFileEvent(loop,e->fd,AE_READABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeAddWrite(void *privdata) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
aeEventLoop *loop = e->loop;
|
|
|
|
if (!e->writing) {
|
|
|
|
e->writing = 1;
|
|
|
|
aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeDelWrite(void *privdata) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
aeEventLoop *loop = e->loop;
|
|
|
|
if (e->writing) {
|
|
|
|
e->writing = 0;
|
|
|
|
aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static void redisAeCleanup(void *privdata) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisAeEvents *e = (redisAeEvents*)privdata;
|
|
|
|
redisAeDelRead(privdata);
|
|
|
|
redisAeDelWrite(privdata);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:40:12 +00:00
|
|
|
static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
|
2010-12-01 12:47:58 +00:00
|
|
|
redisContext *c = &(ac->c);
|
|
|
|
redisAeEvents *e;
|
|
|
|
|
|
|
|
/* Nothing should be attached when something is already attached */
|
2010-12-29 14:41:03 +00:00
|
|
|
if (ac->ev.data != NULL)
|
2010-12-01 12:47:58 +00:00
|
|
|
return REDIS_ERR;
|
|
|
|
|
|
|
|
/* Create container for context and r/w events */
|
|
|
|
e = (redisAeEvents*)malloc(sizeof(*e));
|
|
|
|
e->context = ac;
|
|
|
|
e->loop = loop;
|
|
|
|
e->fd = c->fd;
|
|
|
|
e->reading = e->writing = 0;
|
|
|
|
|
|
|
|
/* Register functions to start/stop listening for events */
|
2010-12-29 14:41:03 +00:00
|
|
|
ac->ev.addRead = redisAeAddRead;
|
|
|
|
ac->ev.delRead = redisAeDelRead;
|
|
|
|
ac->ev.addWrite = redisAeAddWrite;
|
|
|
|
ac->ev.delWrite = redisAeDelWrite;
|
|
|
|
ac->ev.cleanup = redisAeCleanup;
|
|
|
|
ac->ev.data = e;
|
2010-12-01 12:47:58 +00:00
|
|
|
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
2011-06-27 19:40:12 +00:00
|
|
|
#endif
|