Style
This commit is contained in:
parent
e245ab48ec
commit
c8d72f7c7a
@ -72,9 +72,9 @@ redisContext *libevRedisConnect(struct ev_loop *loop, redisErrorCallback *err, c
|
||||
|
||||
/* Create container for context and r/w events */
|
||||
e = malloc(sizeof(*e));
|
||||
e->loop = loop;
|
||||
e->context = c;
|
||||
e->err = err;
|
||||
e->loop = loop;
|
||||
e->rev.data = e;
|
||||
e->wev.data = e;
|
||||
|
||||
|
@ -5,36 +5,31 @@
|
||||
/* Prototype for the error callback. */
|
||||
typedef void (redisErrorCallback)(const redisContext*);
|
||||
|
||||
/* This struct enables us to pass both the events and the
|
||||
* redisContext to the read and write handlers. */
|
||||
typedef struct redisEvents {
|
||||
typedef struct libeventRedisEvents {
|
||||
redisContext *context;
|
||||
redisErrorCallback *err;
|
||||
struct event rev, wev;
|
||||
} redisEvents;
|
||||
} libeventRedisEvents;
|
||||
|
||||
void redisLibEventRead(int fd, short event, void *arg) {
|
||||
void libeventRedisReadEvent(int fd, short event, void *arg) {
|
||||
((void)fd); ((void)event);
|
||||
redisEvents *e = arg;
|
||||
libeventRedisEvents *e = arg;
|
||||
|
||||
/* Always re-schedule read events */
|
||||
event_add(&e->rev,NULL);
|
||||
|
||||
if (redisBufferRead(e->context) == REDIS_ERR) {
|
||||
/* Handle error. */
|
||||
e->err(e->context);
|
||||
} else {
|
||||
/* If processing the replies/callbacks results in an error,
|
||||
* invoke the error callback and abort. */
|
||||
if (redisProcessCallbacks(e->context) == REDIS_ERR) {
|
||||
e->err(e->context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void redisLibEventWrite(int fd, short event, void *arg) {
|
||||
void libeventRedisWriteEvent(int fd, short event, void *arg) {
|
||||
((void)fd); ((void)event);
|
||||
redisEvents *e = arg;
|
||||
libeventRedisEvents *e = arg;
|
||||
int done = 0;
|
||||
|
||||
if (redisBufferWrite(e->context,&done) == REDIS_ERR) {
|
||||
@ -52,29 +47,29 @@ void redisLibEventWrite(int fd, short event, void *arg) {
|
||||
|
||||
/* Schedule to be notified on a write event, so the outgoing buffer
|
||||
* can be flushed to the socket. */
|
||||
void redisLibEventOnWrite(redisContext *c, void *privdata) {
|
||||
void libeventRedisCommandCallback(redisContext *c, void *privdata) {
|
||||
((void)c);
|
||||
redisEvents *e = privdata;
|
||||
libeventRedisEvents *e = privdata;
|
||||
event_add(&e->wev,NULL);
|
||||
}
|
||||
|
||||
/* Remove event handlers when the context gets disconnected. */
|
||||
void redisLibEventOnDisconnect(redisContext *c, void *privdata) {
|
||||
void libeventRedisDisconnectCallback(redisContext *c, void *privdata) {
|
||||
((void)c);
|
||||
redisEvents *e = privdata;
|
||||
libeventRedisEvents *e = privdata;
|
||||
event_del(&e->rev);
|
||||
event_del(&e->wev);
|
||||
}
|
||||
|
||||
/* Free the redisEvents struct when the context is free'd. */
|
||||
void redisLibEventOnFree(redisContext *c, void *privdata) {
|
||||
/* Free the libeventRedisEvents struct when the context is free'd. */
|
||||
void libeventRedisFreeCallback(redisContext *c, void *privdata) {
|
||||
((void)c);
|
||||
redisEvents *e = privdata;
|
||||
libeventRedisEvents *e = privdata;
|
||||
free(e);
|
||||
}
|
||||
|
||||
redisContext *redisLibEventConnect(struct event_base *base, redisErrorCallback *err, const char *ip, int port) {
|
||||
redisEvents *e;
|
||||
redisContext *libeventRedisConnect(struct event_base *base, redisErrorCallback *err, const char *ip, int port) {
|
||||
libeventRedisEvents *e;
|
||||
redisContext *c = redisConnectNonBlock(ip,port,NULL);
|
||||
if (c->error != NULL) {
|
||||
err(c);
|
||||
@ -87,12 +82,14 @@ redisContext *redisLibEventConnect(struct event_base *base, redisErrorCallback *
|
||||
e->context = c;
|
||||
e->err = err;
|
||||
|
||||
/* 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_set(&e->wev, e->context->fd, EV_WRITE, redisLibEventWrite, e);
|
||||
/* Register callbacks */
|
||||
redisSetDisconnectCallback(c,libeventRedisDisconnectCallback,e);
|
||||
redisSetCommandCallback(c,libeventRedisCommandCallback,e);
|
||||
redisSetFreeCallback(c,libeventRedisFreeCallback,e);
|
||||
|
||||
/* Initialize and install read/write events */
|
||||
event_set(&e->rev,c->fd,EV_READ,libeventRedisReadEvent,e);
|
||||
event_set(&e->wev,c->fd,EV_WRITE,libeventRedisWriteEvent,e);
|
||||
event_base_set(base,&e->rev);
|
||||
event_base_set(base,&e->wev);
|
||||
return c;
|
||||
|
@ -19,7 +19,7 @@ int main (int argc, char **argv) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
struct event_base *base = event_base_new();
|
||||
|
||||
redisContext *c = redisLibEventConnect(base, errorCallback, "127.0.0.1", 6379);
|
||||
redisContext *c = libeventRedisConnect(base, errorCallback, "127.0.0.1", 6379);
|
||||
if (c == NULL) return 1;
|
||||
|
||||
redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
|
||||
|
Loading…
Reference in New Issue
Block a user