diff options
Diffstat (limited to 'examples/redis-unstable/deps/hiredis/adapters/libhv.h')
| -rw-r--r-- | examples/redis-unstable/deps/hiredis/adapters/libhv.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/examples/redis-unstable/deps/hiredis/adapters/libhv.h b/examples/redis-unstable/deps/hiredis/adapters/libhv.h new file mode 100644 index 0000000..3b54c70 --- /dev/null +++ b/examples/redis-unstable/deps/hiredis/adapters/libhv.h | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | #ifndef __HIREDIS_LIBHV_H__ | ||
| 2 | #define __HIREDIS_LIBHV_H__ | ||
| 3 | |||
| 4 | #include <hv/hloop.h> | ||
| 5 | #include "../hiredis.h" | ||
| 6 | #include "../async.h" | ||
| 7 | |||
| 8 | typedef struct redisLibhvEvents { | ||
| 9 | hio_t *io; | ||
| 10 | htimer_t *timer; | ||
| 11 | } redisLibhvEvents; | ||
| 12 | |||
| 13 | static void redisLibhvHandleEvents(hio_t* io) { | ||
| 14 | redisAsyncContext* context = (redisAsyncContext*)hevent_userdata(io); | ||
| 15 | int events = hio_events(io); | ||
| 16 | int revents = hio_revents(io); | ||
| 17 | if (context && (events & HV_READ) && (revents & HV_READ)) { | ||
| 18 | redisAsyncHandleRead(context); | ||
| 19 | } | ||
| 20 | if (context && (events & HV_WRITE) && (revents & HV_WRITE)) { | ||
| 21 | redisAsyncHandleWrite(context); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | static void redisLibhvAddRead(void *privdata) { | ||
| 26 | redisLibhvEvents* events = (redisLibhvEvents*)privdata; | ||
| 27 | hio_add(events->io, redisLibhvHandleEvents, HV_READ); | ||
| 28 | } | ||
| 29 | |||
| 30 | static void redisLibhvDelRead(void *privdata) { | ||
| 31 | redisLibhvEvents* events = (redisLibhvEvents*)privdata; | ||
| 32 | hio_del(events->io, HV_READ); | ||
| 33 | } | ||
| 34 | |||
| 35 | static void redisLibhvAddWrite(void *privdata) { | ||
| 36 | redisLibhvEvents* events = (redisLibhvEvents*)privdata; | ||
| 37 | hio_add(events->io, redisLibhvHandleEvents, HV_WRITE); | ||
| 38 | } | ||
| 39 | |||
| 40 | static void redisLibhvDelWrite(void *privdata) { | ||
| 41 | redisLibhvEvents* events = (redisLibhvEvents*)privdata; | ||
| 42 | hio_del(events->io, HV_WRITE); | ||
| 43 | } | ||
| 44 | |||
| 45 | static void redisLibhvCleanup(void *privdata) { | ||
| 46 | redisLibhvEvents* events = (redisLibhvEvents*)privdata; | ||
| 47 | |||
| 48 | if (events->timer) | ||
| 49 | htimer_del(events->timer); | ||
| 50 | |||
| 51 | hio_close(events->io); | ||
| 52 | hevent_set_userdata(events->io, NULL); | ||
| 53 | |||
| 54 | hi_free(events); | ||
| 55 | } | ||
| 56 | |||
| 57 | static void redisLibhvTimeout(htimer_t* timer) { | ||
| 58 | hio_t* io = (hio_t*)hevent_userdata(timer); | ||
| 59 | redisAsyncHandleTimeout((redisAsyncContext*)hevent_userdata(io)); | ||
| 60 | } | ||
| 61 | |||
| 62 | static void redisLibhvSetTimeout(void *privdata, struct timeval tv) { | ||
| 63 | redisLibhvEvents* events; | ||
| 64 | uint32_t millis; | ||
| 65 | hloop_t* loop; | ||
| 66 | |||
| 67 | events = (redisLibhvEvents*)privdata; | ||
| 68 | millis = tv.tv_sec * 1000 + tv.tv_usec / 1000; | ||
| 69 | |||
| 70 | if (millis == 0) { | ||
| 71 | /* Libhv disallows zero'd timers so treat this as a delete or NO OP */ | ||
| 72 | if (events->timer) { | ||
| 73 | htimer_del(events->timer); | ||
| 74 | events->timer = NULL; | ||
| 75 | } | ||
| 76 | } else if (events->timer == NULL) { | ||
| 77 | /* Add new timer */ | ||
| 78 | loop = hevent_loop(events->io); | ||
| 79 | events->timer = htimer_add(loop, redisLibhvTimeout, millis, 1); | ||
| 80 | hevent_set_userdata(events->timer, events->io); | ||
| 81 | } else { | ||
| 82 | /* Update existing timer */ | ||
| 83 | htimer_reset(events->timer, millis); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | static int redisLibhvAttach(redisAsyncContext* ac, hloop_t* loop) { | ||
| 88 | redisContext *c = &(ac->c); | ||
| 89 | redisLibhvEvents *events; | ||
| 90 | hio_t* io = NULL; | ||
| 91 | |||
| 92 | if (ac->ev.data != NULL) { | ||
| 93 | return REDIS_ERR; | ||
| 94 | } | ||
| 95 | |||
| 96 | /* Create container struct to keep track of our io and any timer */ | ||
| 97 | events = (redisLibhvEvents*)hi_malloc(sizeof(*events)); | ||
| 98 | if (events == NULL) { | ||
| 99 | return REDIS_ERR; | ||
| 100 | } | ||
| 101 | |||
| 102 | io = hio_get(loop, c->fd); | ||
| 103 | if (io == NULL) { | ||
| 104 | hi_free(events); | ||
| 105 | return REDIS_ERR; | ||
| 106 | } | ||
| 107 | |||
| 108 | hevent_set_userdata(io, ac); | ||
| 109 | |||
| 110 | events->io = io; | ||
| 111 | events->timer = NULL; | ||
| 112 | |||
| 113 | ac->ev.addRead = redisLibhvAddRead; | ||
| 114 | ac->ev.delRead = redisLibhvDelRead; | ||
| 115 | ac->ev.addWrite = redisLibhvAddWrite; | ||
| 116 | ac->ev.delWrite = redisLibhvDelWrite; | ||
| 117 | ac->ev.cleanup = redisLibhvCleanup; | ||
| 118 | ac->ev.scheduleTimer = redisLibhvSetTimeout; | ||
| 119 | ac->ev.data = events; | ||
| 120 | |||
| 121 | return REDIS_OK; | ||
| 122 | } | ||
| 123 | #endif | ||
