libev.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HIREDIS_LIBEV_H__
  31. #define __HIREDIS_LIBEV_H__
  32. #include <stdlib.h>
  33. #include <sys/types.h>
  34. #include <ev.h>
  35. #include "../hiredis.h"
  36. #include "../async.h"
  37. typedef struct redisLibevEvents {
  38. redisAsyncContext *context;
  39. struct ev_loop *loop;
  40. int reading, writing;
  41. ev_io rev, wev;
  42. ev_timer timer;
  43. } redisLibevEvents;
  44. static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
  45. #if EV_MULTIPLICITY
  46. ((void)loop);
  47. #endif
  48. ((void)revents);
  49. redisLibevEvents *e = (redisLibevEvents*)watcher->data;
  50. redisAsyncHandleRead(e->context);
  51. }
  52. static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
  53. #if EV_MULTIPLICITY
  54. ((void)loop);
  55. #endif
  56. ((void)revents);
  57. redisLibevEvents *e = (redisLibevEvents*)watcher->data;
  58. redisAsyncHandleWrite(e->context);
  59. }
  60. static void redisLibevAddRead(void *privdata) {
  61. redisLibevEvents *e = (redisLibevEvents*)privdata;
  62. struct ev_loop *loop = e->loop;
  63. ((void)loop);
  64. if (!e->reading) {
  65. e->reading = 1;
  66. ev_io_start(EV_A_ &e->rev);
  67. }
  68. }
  69. static void redisLibevDelRead(void *privdata) {
  70. redisLibevEvents *e = (redisLibevEvents*)privdata;
  71. struct ev_loop *loop = e->loop;
  72. ((void)loop);
  73. if (e->reading) {
  74. e->reading = 0;
  75. ev_io_stop(EV_A_ &e->rev);
  76. }
  77. }
  78. static void redisLibevAddWrite(void *privdata) {
  79. redisLibevEvents *e = (redisLibevEvents*)privdata;
  80. struct ev_loop *loop = e->loop;
  81. ((void)loop);
  82. if (!e->writing) {
  83. e->writing = 1;
  84. ev_io_start(EV_A_ &e->wev);
  85. }
  86. }
  87. static void redisLibevDelWrite(void *privdata) {
  88. redisLibevEvents *e = (redisLibevEvents*)privdata;
  89. struct ev_loop *loop = e->loop;
  90. ((void)loop);
  91. if (e->writing) {
  92. e->writing = 0;
  93. ev_io_stop(EV_A_ &e->wev);
  94. }
  95. }
  96. static void redisLibevStopTimer(void *privdata) {
  97. redisLibevEvents *e = (redisLibevEvents*)privdata;
  98. struct ev_loop *loop = e->loop;
  99. ((void)loop);
  100. ev_timer_stop(EV_A_ &e->timer);
  101. }
  102. static void redisLibevCleanup(void *privdata) {
  103. redisLibevEvents *e = (redisLibevEvents*)privdata;
  104. redisLibevDelRead(privdata);
  105. redisLibevDelWrite(privdata);
  106. redisLibevStopTimer(privdata);
  107. hi_free(e);
  108. }
  109. static void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) {
  110. ((void)revents);
  111. redisLibevEvents *e = (redisLibevEvents*)timer->data;
  112. redisAsyncHandleTimeout(e->context);
  113. }
  114. static void redisLibevSetTimeout(void *privdata, struct timeval tv) {
  115. redisLibevEvents *e = (redisLibevEvents*)privdata;
  116. struct ev_loop *loop = e->loop;
  117. ((void)loop);
  118. if (!ev_is_active(&e->timer)) {
  119. ev_init(&e->timer, redisLibevTimeout);
  120. e->timer.data = e;
  121. }
  122. e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00;
  123. ev_timer_again(EV_A_ &e->timer);
  124. }
  125. static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
  126. redisContext *c = &(ac->c);
  127. redisLibevEvents *e;
  128. /* Nothing should be attached when something is already attached */
  129. if (ac->ev.data != NULL)
  130. return REDIS_ERR;
  131. /* Create container for context and r/w events */
  132. e = (redisLibevEvents*)hi_calloc(1, sizeof(*e));
  133. if (e == NULL)
  134. return REDIS_ERR;
  135. e->context = ac;
  136. #if EV_MULTIPLICITY
  137. e->loop = loop;
  138. #else
  139. e->loop = NULL;
  140. #endif
  141. e->rev.data = e;
  142. e->wev.data = e;
  143. /* Register functions to start/stop listening for events */
  144. ac->ev.addRead = redisLibevAddRead;
  145. ac->ev.delRead = redisLibevDelRead;
  146. ac->ev.addWrite = redisLibevAddWrite;
  147. ac->ev.delWrite = redisLibevDelWrite;
  148. ac->ev.cleanup = redisLibevCleanup;
  149. ac->ev.scheduleTimer = redisLibevSetTimeout;
  150. ac->ev.data = e;
  151. /* Initialize read/write events */
  152. ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
  153. ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
  154. return REDIS_OK;
  155. }
  156. #endif