redis-check-dump now understands dumps produced by Redis 2.6
[redis.git] / deps / hiredis / adapters / libev.h
CommitLineData
b66e5add 1#ifndef __HIREDIS_LIBEV_H__
2#define __HIREDIS_LIBEV_H__
3#include <stdlib.h>
24f753a8
PN
4#include <sys/types.h>
5#include <ev.h>
6#include "../hiredis.h"
7#include "../async.h"
8
9typedef struct redisLibevEvents {
10 redisAsyncContext *context;
11 struct ev_loop *loop;
12 int reading, writing;
13 ev_io rev, wev;
14} redisLibevEvents;
15
b66e5add 16static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
a1e97d69
PN
17#if EV_MULTIPLICITY
18 ((void)loop);
19#endif
20 ((void)revents);
21
22 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
24f753a8
PN
23 redisAsyncHandleRead(e->context);
24}
25
b66e5add 26static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
a1e97d69
PN
27#if EV_MULTIPLICITY
28 ((void)loop);
29#endif
30 ((void)revents);
31
32 redisLibevEvents *e = (redisLibevEvents*)watcher->data;
24f753a8
PN
33 redisAsyncHandleWrite(e->context);
34}
35
b66e5add 36static void redisLibevAddRead(void *privdata) {
a1e97d69
PN
37 redisLibevEvents *e = (redisLibevEvents*)privdata;
38 struct ev_loop *loop = e->loop;
39 ((void)loop);
24f753a8
PN
40 if (!e->reading) {
41 e->reading = 1;
a1e97d69 42 ev_io_start(EV_A_ &e->rev);
24f753a8
PN
43 }
44}
45
b66e5add 46static void redisLibevDelRead(void *privdata) {
a1e97d69
PN
47 redisLibevEvents *e = (redisLibevEvents*)privdata;
48 struct ev_loop *loop = e->loop;
49 ((void)loop);
24f753a8
PN
50 if (e->reading) {
51 e->reading = 0;
a1e97d69 52 ev_io_stop(EV_A_ &e->rev);
24f753a8
PN
53 }
54}
55
b66e5add 56static void redisLibevAddWrite(void *privdata) {
a1e97d69
PN
57 redisLibevEvents *e = (redisLibevEvents*)privdata;
58 struct ev_loop *loop = e->loop;
59 ((void)loop);
24f753a8
PN
60 if (!e->writing) {
61 e->writing = 1;
a1e97d69 62 ev_io_start(EV_A_ &e->wev);
24f753a8
PN
63 }
64}
65
b66e5add 66static void redisLibevDelWrite(void *privdata) {
a1e97d69
PN
67 redisLibevEvents *e = (redisLibevEvents*)privdata;
68 struct ev_loop *loop = e->loop;
69 ((void)loop);
24f753a8
PN
70 if (e->writing) {
71 e->writing = 0;
a1e97d69 72 ev_io_stop(EV_A_ &e->wev);
24f753a8
PN
73 }
74}
75
b66e5add 76static void redisLibevCleanup(void *privdata) {
a1e97d69 77 redisLibevEvents *e = (redisLibevEvents*)privdata;
24f753a8
PN
78 redisLibevDelRead(privdata);
79 redisLibevDelWrite(privdata);
80 free(e);
81}
82
b66e5add 83static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
24f753a8
PN
84 redisContext *c = &(ac->c);
85 redisLibevEvents *e;
86
87 /* Nothing should be attached when something is already attached */
b66e5add 88 if (ac->ev.data != NULL)
24f753a8
PN
89 return REDIS_ERR;
90
91 /* Create container for context and r/w events */
a1e97d69 92 e = (redisLibevEvents*)malloc(sizeof(*e));
24f753a8 93 e->context = ac;
a1e97d69 94#if EV_MULTIPLICITY
24f753a8 95 e->loop = loop;
a1e97d69
PN
96#else
97 e->loop = NULL;
98#endif
24f753a8
PN
99 e->reading = e->writing = 0;
100 e->rev.data = e;
101 e->wev.data = e;
102
103 /* Register functions to start/stop listening for events */
b66e5add 104 ac->ev.addRead = redisLibevAddRead;
105 ac->ev.delRead = redisLibevDelRead;
106 ac->ev.addWrite = redisLibevAddWrite;
107 ac->ev.delWrite = redisLibevDelWrite;
108 ac->ev.cleanup = redisLibevCleanup;
109 ac->ev.data = e;
24f753a8
PN
110
111 /* Initialize read/write events */
112 ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
113 ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
114 return REDIS_OK;
115}
a1e97d69 116
b66e5add 117#endif