ae.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_AE_H__
  31. #define __HIREDIS_AE_H__
  32. #include <sys/types.h>
  33. #include <ae.h>
  34. #include "../hiredis.h"
  35. #include "../async.h"
  36. #if 1 //shenzheng 2015-11-5 redis cluster
  37. #include "../hircluster.h"
  38. #endif //shenzheng 2015-11-5 redis cluster
  39. typedef struct redisAeEvents {
  40. redisAsyncContext *context;
  41. aeEventLoop *loop;
  42. int fd;
  43. int reading, writing;
  44. } redisAeEvents;
  45. static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  46. ((void)el); ((void)fd); ((void)mask);
  47. redisAeEvents *e = (redisAeEvents*)privdata;
  48. redisAsyncHandleRead(e->context);
  49. }
  50. static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  51. ((void)el); ((void)fd); ((void)mask);
  52. redisAeEvents *e = (redisAeEvents*)privdata;
  53. redisAsyncHandleWrite(e->context);
  54. }
  55. static void redisAeAddRead(void *privdata) {
  56. redisAeEvents *e = (redisAeEvents*)privdata;
  57. aeEventLoop *loop = e->loop;
  58. if (!e->reading) {
  59. e->reading = 1;
  60. aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);
  61. }
  62. }
  63. static void redisAeDelRead(void *privdata) {
  64. redisAeEvents *e = (redisAeEvents*)privdata;
  65. aeEventLoop *loop = e->loop;
  66. if (e->reading) {
  67. e->reading = 0;
  68. aeDeleteFileEvent(loop,e->fd,AE_READABLE);
  69. }
  70. }
  71. static void redisAeAddWrite(void *privdata) {
  72. redisAeEvents *e = (redisAeEvents*)privdata;
  73. aeEventLoop *loop = e->loop;
  74. if (!e->writing) {
  75. e->writing = 1;
  76. aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);
  77. }
  78. }
  79. static void redisAeDelWrite(void *privdata) {
  80. redisAeEvents *e = (redisAeEvents*)privdata;
  81. aeEventLoop *loop = e->loop;
  82. if (e->writing) {
  83. e->writing = 0;
  84. aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);
  85. }
  86. }
  87. static void redisAeCleanup(void *privdata) {
  88. redisAeEvents *e = (redisAeEvents*)privdata;
  89. redisAeDelRead(privdata);
  90. redisAeDelWrite(privdata);
  91. free(e);
  92. }
  93. static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
  94. redisContext *c = &(ac->c);
  95. redisAeEvents *e;
  96. /* Nothing should be attached when something is already attached */
  97. if (ac->ev.data != NULL)
  98. return REDIS_ERR;
  99. /* Create container for context and r/w events */
  100. e = (redisAeEvents*)malloc(sizeof(*e));
  101. e->context = ac;
  102. e->loop = loop;
  103. e->fd = c->fd;
  104. e->reading = e->writing = 0;
  105. /* Register functions to start/stop listening for events */
  106. ac->ev.addRead = redisAeAddRead;
  107. ac->ev.delRead = redisAeDelRead;
  108. ac->ev.addWrite = redisAeAddWrite;
  109. ac->ev.delWrite = redisAeDelWrite;
  110. ac->ev.cleanup = redisAeCleanup;
  111. ac->ev.data = e;
  112. return REDIS_OK;
  113. }
  114. #if 1 //shenzheng 2015-11-5 redis cluster
  115. static int redisAeAttach_link(redisAsyncContext *ac, void *base)
  116. {
  117. redisAeAttach((aeEventLoop *)base, ac);
  118. }
  119. static int redisClusterAeAttach(aeEventLoop *loop, redisClusterAsyncContext *acc) {
  120. if(acc == NULL || loop == NULL)
  121. {
  122. return REDIS_ERR;
  123. }
  124. acc->adapter = loop;
  125. acc->attach_fn = redisAeAttach_link;
  126. return REDIS_OK;
  127. }
  128. #endif //shenzheng 2015-11-5 redis cluster
  129. #endif