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.

libev.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "contrib/libev/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. } redisLibevEvents;
  43. static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
  44. #if EV_MULTIPLICITY
  45. ((void)loop);
  46. #endif
  47. ((void)revents);
  48. redisLibevEvents *e = (redisLibevEvents*)watcher->data;
  49. redisAsyncHandleRead(e->context);
  50. }
  51. static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
  52. #if EV_MULTIPLICITY
  53. ((void)loop);
  54. #endif
  55. ((void)revents);
  56. redisLibevEvents *e = (redisLibevEvents*)watcher->data;
  57. redisAsyncHandleWrite(e->context);
  58. }
  59. static void redisLibevAddRead(void *privdata) {
  60. redisLibevEvents *e = (redisLibevEvents*)privdata;
  61. struct ev_loop *loop = e->loop;
  62. ((void)loop);
  63. if (!e->reading) {
  64. e->reading = 1;
  65. ev_io_start(EV_A_ &e->rev);
  66. }
  67. }
  68. static void redisLibevDelRead(void *privdata) {
  69. redisLibevEvents *e = (redisLibevEvents*)privdata;
  70. struct ev_loop *loop = e->loop;
  71. ((void)loop);
  72. if (e->reading) {
  73. e->reading = 0;
  74. ev_io_stop(EV_A_ &e->rev);
  75. }
  76. }
  77. static void redisLibevAddWrite(void *privdata) {
  78. redisLibevEvents *e = (redisLibevEvents*)privdata;
  79. struct ev_loop *loop = e->loop;
  80. ((void)loop);
  81. if (!e->writing) {
  82. e->writing = 1;
  83. ev_io_start(EV_A_ &e->wev);
  84. }
  85. }
  86. static void redisLibevDelWrite(void *privdata) {
  87. redisLibevEvents *e = (redisLibevEvents*)privdata;
  88. struct ev_loop *loop = e->loop;
  89. ((void)loop);
  90. if (e->writing) {
  91. e->writing = 0;
  92. ev_io_stop(EV_A_ &e->wev);
  93. }
  94. }
  95. static void redisLibevCleanup(void *privdata) {
  96. redisLibevEvents *e = (redisLibevEvents*)privdata;
  97. redisLibevDelRead(privdata);
  98. redisLibevDelWrite(privdata);
  99. free(e);
  100. }
  101. static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
  102. redisContext *c = &(ac->c);
  103. redisLibevEvents *e;
  104. /* Nothing should be attached when something is already attached */
  105. if (ac->ev.data != NULL)
  106. return REDIS_ERR;
  107. /* Create container for context and r/w events */
  108. e = (redisLibevEvents*)malloc(sizeof(*e));
  109. e->context = ac;
  110. #if EV_MULTIPLICITY
  111. e->loop = loop;
  112. #else
  113. e->loop = NULL;
  114. #endif
  115. e->reading = e->writing = 0;
  116. e->rev.data = e;
  117. e->wev.data = e;
  118. /* Register functions to start/stop listening for events */
  119. ac->ev.addRead = redisLibevAddRead;
  120. ac->ev.delRead = redisLibevDelRead;
  121. ac->ev.addWrite = redisLibevAddWrite;
  122. ac->ev.delWrite = redisLibevDelWrite;
  123. ac->ev.cleanup = redisLibevCleanup;
  124. ac->ev.data = e;
  125. /* Initialize read/write events */
  126. ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
  127. ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
  128. return REDIS_OK;
  129. }
  130. #endif