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