Added event_base to libevent code.
This commit is contained in:
parent
96510ce86a
commit
135e9265ac
@ -1,3 +1,4 @@
|
||||
typedef unsigned char u_char; /* libevents needs u_char */
|
||||
#include <event.h>
|
||||
#include <hiredis.h>
|
||||
|
||||
@ -10,6 +11,7 @@ typedef struct redisEvents {
|
||||
redisContext *context;
|
||||
redisErrorCallback *err;
|
||||
struct event rev, wev;
|
||||
struct event_base *base;
|
||||
} redisEvents;
|
||||
|
||||
void redisLibEventRead(int fd, short event, void *arg) {
|
||||
@ -72,6 +74,14 @@ 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) {
|
||||
redisEvents *e;
|
||||
redisContext *c = redisConnectNonBlock(ip, port, NULL);
|
||||
@ -84,12 +94,16 @@ 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);
|
||||
return e->context;
|
||||
event_base_set(e->base, &e->wev);
|
||||
return c;
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ 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*);
|
||||
@ -109,6 +110,10 @@ typedef struct redisContext {
|
||||
redisCallback *callbacks;
|
||||
int cpos;
|
||||
int clen;
|
||||
|
||||
/* Libevent context */
|
||||
struct redisEvents *events;
|
||||
|
||||
} redisContext;
|
||||
|
||||
void freeReplyObject(void *reply);
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <hiredis/libevent.h>
|
||||
#include <signal.h>
|
||||
|
||||
void getCallback(redisContext *c, redisReply *reply, const void *privdata) {
|
||||
printf("argv[%s]: %s\n", (const char*)privdata, reply->reply);
|
||||
@ -26,7 +27,7 @@ int main (int argc, char **argv) {
|
||||
|
||||
redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
|
||||
redisCommandWithCallback(c, getCallback, "end-1", "GET key");
|
||||
event_dispatch();
|
||||
redisLibEventDispatch(c);
|
||||
redisFree(c);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user