summaryrefslogtreecommitdiff
path: root/extra/hiredis/libevent.h
blob: 6f7f9adfe6d3b0926ddf6e15bad7787be6c8918b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <sys/types.h>
#include <event.h>
#include <hiredis.h>

/* Prototype for the error callback. */
typedef void (redisErrorCallback)(const redisContext*);

typedef struct libeventRedisEvents {
    redisContext *context;
    redisErrorCallback *err;
    struct event rev, wev;
} libeventRedisEvents;

void libeventRedisReadEvent(int fd, short event, void *arg) {
    ((void)fd); ((void)event);
    libeventRedisEvents *e = arg;

    /* Always re-schedule read events */
    event_add(&e->rev,NULL);

    if (redisBufferRead(e->context) == REDIS_ERR) {
        e->err(e->context);
    } else {
        if (redisProcessCallbacks(e->context) == REDIS_ERR) {
            e->err(e->context);
        }
    }
}

void libeventRedisWriteEvent(int fd, short event, void *arg) {
    ((void)fd); ((void)event);
    libeventRedisEvents *e = arg;
    int done = 0;

    if (redisBufferWrite(e->context,&done) == REDIS_ERR) {
        /* Handle error */
        e->err(e->context);
    } else {
        /* Schedule write event again when writing is not done. */
        if (!done) {
            event_add(&e->wev,NULL);
        } else {
            event_add(&e->rev,NULL);
        }
    }
}

/* Schedule to be notified on a write event, so the outgoing buffer
 * can be flushed to the socket. */
void libeventRedisCommandCallback(redisContext *c, void *privdata) {
    ((void)c);
    libeventRedisEvents *e = privdata;
    event_add(&e->wev,NULL);
}

/* Remove event handlers when the context gets disconnected. */
void libeventRedisDisconnectCallback(redisContext *c, void *privdata) {
    ((void)c);
    libeventRedisEvents *e = privdata;
    event_del(&e->rev);
    event_del(&e->wev);
}

/* Free the libeventRedisEvents struct when the context is free'd. */
void libeventRedisFreeCallback(redisContext *c, void *privdata) {
    ((void)c);
    libeventRedisEvents *e = privdata;
    free(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);
        redisFree(c);
        return NULL;
    }

    /* Create container for context and r/w events */
    e = malloc(sizeof(*e));
    e->context = c;
    e->err = err;

    /* 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;
}