]> git.saurik.com Git - redis.git/blame - deps/hiredis/adapters/ae.h
Make clear that contributing code to the Redis project means to release it under...
[redis.git] / deps / hiredis / adapters / ae.h
CommitLineData
b66e5add 1#ifndef __HIREDIS_AE_H__
2#define __HIREDIS_AE_H__
a1e97d69
PN
3#include <sys/types.h>
4#include <ae.h>
5#include "../hiredis.h"
6#include "../async.h"
7
8typedef struct redisAeEvents {
9 redisAsyncContext *context;
10 aeEventLoop *loop;
11 int fd;
12 int reading, writing;
13} redisAeEvents;
14
b66e5add 15static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
a1e97d69
PN
16 ((void)el); ((void)fd); ((void)mask);
17
18 redisAeEvents *e = (redisAeEvents*)privdata;
19 redisAsyncHandleRead(e->context);
20}
21
b66e5add 22static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
a1e97d69
PN
23 ((void)el); ((void)fd); ((void)mask);
24
25 redisAeEvents *e = (redisAeEvents*)privdata;
26 redisAsyncHandleWrite(e->context);
27}
28
b66e5add 29static void redisAeAddRead(void *privdata) {
a1e97d69
PN
30 redisAeEvents *e = (redisAeEvents*)privdata;
31 aeEventLoop *loop = e->loop;
32 if (!e->reading) {
33 e->reading = 1;
34 aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);
35 }
36}
37
b66e5add 38static void redisAeDelRead(void *privdata) {
a1e97d69
PN
39 redisAeEvents *e = (redisAeEvents*)privdata;
40 aeEventLoop *loop = e->loop;
41 if (e->reading) {
42 e->reading = 0;
43 aeDeleteFileEvent(loop,e->fd,AE_READABLE);
44 }
45}
46
b66e5add 47static void redisAeAddWrite(void *privdata) {
a1e97d69
PN
48 redisAeEvents *e = (redisAeEvents*)privdata;
49 aeEventLoop *loop = e->loop;
50 if (!e->writing) {
51 e->writing = 1;
52 aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);
53 }
54}
55
b66e5add 56static void redisAeDelWrite(void *privdata) {
a1e97d69
PN
57 redisAeEvents *e = (redisAeEvents*)privdata;
58 aeEventLoop *loop = e->loop;
59 if (e->writing) {
60 e->writing = 0;
61 aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);
62 }
63}
64
b66e5add 65static void redisAeCleanup(void *privdata) {
a1e97d69
PN
66 redisAeEvents *e = (redisAeEvents*)privdata;
67 redisAeDelRead(privdata);
68 redisAeDelWrite(privdata);
69 free(e);
70}
71
b66e5add 72static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
a1e97d69
PN
73 redisContext *c = &(ac->c);
74 redisAeEvents *e;
75
76 /* Nothing should be attached when something is already attached */
b66e5add 77 if (ac->ev.data != NULL)
a1e97d69
PN
78 return REDIS_ERR;
79
80 /* Create container for context and r/w events */
81 e = (redisAeEvents*)malloc(sizeof(*e));
82 e->context = ac;
83 e->loop = loop;
84 e->fd = c->fd;
85 e->reading = e->writing = 0;
86
87 /* Register functions to start/stop listening for events */
b66e5add 88 ac->ev.addRead = redisAeAddRead;
89 ac->ev.delRead = redisAeDelRead;
90 ac->ev.addWrite = redisAeAddWrite;
91 ac->ev.delWrite = redisAeDelWrite;
92 ac->ev.cleanup = redisAeCleanup;
93 ac->ev.data = e;
a1e97d69
PN
94
95 return REDIS_OK;
96}
b66e5add 97#endif