]> git.saurik.com Git - redis.git/blob - deps/hiredis/adapters/libev.h
handle end of space on device in a better way
[redis.git] / deps / hiredis / adapters / libev.h
1 #include <sys/types.h>
2 #include <ev.h>
3 #include "../hiredis.h"
4 #include "../async.h"
5
6 typedef struct redisLibevEvents {
7 redisAsyncContext *context;
8 struct ev_loop *loop;
9 int reading, writing;
10 ev_io rev, wev;
11 } redisLibevEvents;
12
13 void redisLibevReadEvent(struct ev_loop *loop, ev_io *watcher, int revents) {
14 ((void)loop); ((void)revents);
15 redisLibevEvents *e = watcher->data;
16 redisAsyncHandleRead(e->context);
17 }
18
19 void redisLibevWriteEvent(struct ev_loop *loop, ev_io *watcher, int revents) {
20 ((void)loop); ((void)revents);
21 redisLibevEvents *e = watcher->data;
22 redisAsyncHandleWrite(e->context);
23 }
24
25 void redisLibevAddRead(void *privdata) {
26 redisLibevEvents *e = privdata;
27 if (!e->reading) {
28 e->reading = 1;
29 ev_io_start(e->loop,&e->rev);
30 }
31 }
32
33 void redisLibevDelRead(void *privdata) {
34 redisLibevEvents *e = privdata;
35 if (e->reading) {
36 e->reading = 0;
37 ev_io_stop(e->loop,&e->rev);
38 }
39 }
40
41 void redisLibevAddWrite(void *privdata) {
42 redisLibevEvents *e = privdata;
43 if (!e->writing) {
44 e->writing = 1;
45 ev_io_start(e->loop,&e->wev);
46 }
47 }
48
49 void redisLibevDelWrite(void *privdata) {
50 redisLibevEvents *e = privdata;
51 if (e->writing) {
52 e->writing = 0;
53 ev_io_stop(e->loop,&e->wev);
54 }
55 }
56
57 void redisLibevCleanup(void *privdata) {
58 redisLibevEvents *e = privdata;
59 redisLibevDelRead(privdata);
60 redisLibevDelWrite(privdata);
61 free(e);
62 }
63
64 int redisLibevAttach(redisAsyncContext *ac, struct ev_loop *loop) {
65 redisContext *c = &(ac->c);
66 redisLibevEvents *e;
67
68 /* Nothing should be attached when something is already attached */
69 if (ac->data != NULL)
70 return REDIS_ERR;
71
72 /* Create container for context and r/w events */
73 e = malloc(sizeof(*e));
74 e->context = ac;
75 e->loop = loop;
76 e->reading = e->writing = 0;
77 e->rev.data = e;
78 e->wev.data = e;
79
80 /* Register functions to start/stop listening for events */
81 ac->evAddRead = redisLibevAddRead;
82 ac->evDelRead = redisLibevDelRead;
83 ac->evAddWrite = redisLibevAddWrite;
84 ac->evDelWrite = redisLibevDelWrite;
85 ac->evCleanup = redisLibevCleanup;
86 ac->data = e;
87
88 /* Initialize read/write events */
89 ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
90 ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
91 return REDIS_OK;
92 }