Move event_base code out of libevent.h

This removes the need to wrap an event_base pointer in the redisContext
struct and allows multiple connections to be used in a single
event_base. Also, use the *privdata argument to the provided callbacks
to curry the redisEvent struct around.
This commit is contained in:
Pieter Noordhuis 2010-10-11 18:26:38 +02:00
parent 135e9265ac
commit a7acd97ad1
3 changed files with 6 additions and 22 deletions

View File

@ -11,7 +11,6 @@ typedef struct redisEvents {
redisContext *context;
redisErrorCallback *err;
struct event rev, wev;
struct event_base *base;
} redisEvents;
void redisLibEventRead(int fd, short event, void *arg) {
@ -74,15 +73,7 @@ void redisLibEventOnFree(redisContext *c, void *privdata) {
free(e);
}
/* Dispatch libevent loop. */
int redisLibEventDispatch(redisContext *c) {
if (!c->events) {
return -1;
}
return event_base_dispatch(c->events->base);
}
redisContext *redisLibEventConnect(const char *ip, int port, redisErrorCallback *err) {
redisContext *redisLibEventConnect(const char *ip, int port, redisErrorCallback *err, struct event_base *base) {
redisEvents *e;
redisContext *c = redisConnectNonBlock(ip, port, NULL);
if (c->error != NULL) {
@ -94,16 +85,14 @@ redisContext *redisLibEventConnect(const char *ip, int port, redisErrorCallback
e = malloc(sizeof(*e));
e->context = c;
e->err = err;
e->base = event_base_new();
c->events = e;
/* Register callbacks and events */
redisSetDisconnectCallback(e->context, redisLibEventOnDisconnect, e);
redisSetCommandCallback(e->context, redisLibEventOnWrite, e);
redisSetFreeCallback(e->context, redisLibEventOnFree, e);
event_set(&e->rev, e->context->fd, EV_READ, redisLibEventRead, e);
event_base_set(e->base, &e->rev);
event_set(&e->wev, e->context->fd, EV_WRITE, redisLibEventWrite, e);
event_base_set(e->base, &e->wev);
event_base_set(base, &e->rev);
event_base_set(base, &e->wev);
return c;
}

View File

@ -75,7 +75,6 @@ typedef struct redisReplyObjectFunctions {
} redisReplyFunctions;
struct redisContext; /* need forward declaration of redisContext */
struct redisEvents; /* need forward declaration of redisEvents */
/* Callbacks triggered on non-reply events. */
typedef void (redisContextCallback)(struct redisContext*, void*);
@ -110,10 +109,6 @@ typedef struct redisContext {
redisCallback *callbacks;
int cpos;
int clen;
/* Libevent context */
struct redisEvents *events;
} redisContext;
void freeReplyObject(void *reply);

View File

@ -20,14 +20,14 @@ void errorCallback(redisContext *c) {
int main (int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
event_init();
struct event_base *base = event_base_new();
redisContext *c = redisLibEventConnect("127.0.0.1", 6379, errorCallback);
redisContext *c = redisLibEventConnect("127.0.0.1", 6379, errorCallback, base);
if (c == NULL) return 1;
redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
redisCommandWithCallback(c, getCallback, "end-1", "GET key");
redisLibEventDispatch(c);
event_base_dispatch(base);
redisFree(c);
return 0;
}