Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

189 lines
5.5 KiB

  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)EV_A);
  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)EV_A);
  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. #if EV_MULTIPLICITY
  63. struct ev_loop *loop = e->loop;
  64. #endif
  65. if (!e->reading) {
  66. e->reading = 1;
  67. ev_io_start(EV_A_ &e->rev);
  68. }
  69. }
  70. static void redisLibevDelRead(void *privdata) {
  71. redisLibevEvents *e = (redisLibevEvents*)privdata;
  72. #if EV_MULTIPLICITY
  73. struct ev_loop *loop = e->loop;
  74. #endif
  75. if (e->reading) {
  76. e->reading = 0;
  77. ev_io_stop(EV_A_ &e->rev);
  78. }
  79. }
  80. static void redisLibevAddWrite(void *privdata) {
  81. redisLibevEvents *e = (redisLibevEvents*)privdata;
  82. #if EV_MULTIPLICITY
  83. struct ev_loop *loop = e->loop;
  84. #endif
  85. if (!e->writing) {
  86. e->writing = 1;
  87. ev_io_start(EV_A_ &e->wev);
  88. }
  89. }
  90. static void redisLibevDelWrite(void *privdata) {
  91. redisLibevEvents *e = (redisLibevEvents*)privdata;
  92. #if EV_MULTIPLICITY
  93. struct ev_loop *loop = e->loop;
  94. #endif
  95. if (e->writing) {
  96. e->writing = 0;
  97. ev_io_stop(EV_A_ &e->wev);
  98. }
  99. }
  100. static void redisLibevStopTimer(void *privdata) {
  101. redisLibevEvents *e = (redisLibevEvents*)privdata;
  102. #if EV_MULTIPLICITY
  103. struct ev_loop *loop = e->loop;
  104. #endif
  105. ev_timer_stop(EV_A_ &e->timer);
  106. }
  107. static void redisLibevCleanup(void *privdata) {
  108. redisLibevEvents *e = (redisLibevEvents*)privdata;
  109. redisLibevDelRead(privdata);
  110. redisLibevDelWrite(privdata);
  111. redisLibevStopTimer(privdata);
  112. hi_free(e);
  113. }
  114. static void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) {
  115. #if EV_MULTIPLICITY
  116. ((void)EV_A);
  117. #endif
  118. ((void)revents);
  119. redisLibevEvents *e = (redisLibevEvents*)timer->data;
  120. redisAsyncHandleTimeout(e->context);
  121. }
  122. static void redisLibevSetTimeout(void *privdata, struct timeval tv) {
  123. redisLibevEvents *e = (redisLibevEvents*)privdata;
  124. #if EV_MULTIPLICITY
  125. struct ev_loop *loop = e->loop;
  126. #endif
  127. if (!ev_is_active(&e->timer)) {
  128. ev_init(&e->timer, redisLibevTimeout);
  129. e->timer.data = e;
  130. }
  131. e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00;
  132. ev_timer_again(EV_A_ &e->timer);
  133. }
  134. static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
  135. redisContext *c = &(ac->c);
  136. redisLibevEvents *e;
  137. /* Nothing should be attached when something is already attached */
  138. if (ac->ev.data != NULL)
  139. return REDIS_ERR;
  140. /* Create container for context and r/w events */
  141. e = (redisLibevEvents*)hi_calloc(1, sizeof(*e));
  142. if (e == NULL)
  143. return REDIS_ERR;
  144. e->context = ac;
  145. #if EV_MULTIPLICITY
  146. e->loop = EV_A;
  147. #else
  148. e->loop = NULL;
  149. #endif
  150. e->rev.data = e;
  151. e->wev.data = e;
  152. /* Register functions to start/stop listening for events */
  153. ac->ev.addRead = redisLibevAddRead;
  154. ac->ev.delRead = redisLibevDelRead;
  155. ac->ev.addWrite = redisLibevAddWrite;
  156. ac->ev.delWrite = redisLibevDelWrite;
  157. ac->ev.cleanup = redisLibevCleanup;
  158. ac->ev.scheduleTimer = redisLibevSetTimeout;
  159. ac->ev.data = e;
  160. /* Initialize read/write events */
  161. ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
  162. ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
  163. return REDIS_OK;
  164. }
  165. #endif