2
0

ae.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. typedef struct redisAeEvents {
  37. redisAsyncContext *context;
  38. aeEventLoop *loop;
  39. int fd;
  40. int reading, writing;
  41. } redisAeEvents;
  42. static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  43. ((void)el); ((void)fd); ((void)mask);
  44. redisAeEvents *e = (redisAeEvents*)privdata;
  45. redisAsyncHandleRead(e->context);
  46. }
  47. static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  48. ((void)el); ((void)fd); ((void)mask);
  49. redisAeEvents *e = (redisAeEvents*)privdata;
  50. redisAsyncHandleWrite(e->context);
  51. }
  52. static void redisAeAddRead(void *privdata) {
  53. redisAeEvents *e = (redisAeEvents*)privdata;
  54. aeEventLoop *loop = e->loop;
  55. if (!e->reading) {
  56. e->reading = 1;
  57. aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);
  58. }
  59. }
  60. static void redisAeDelRead(void *privdata) {
  61. redisAeEvents *e = (redisAeEvents*)privdata;
  62. aeEventLoop *loop = e->loop;
  63. if (e->reading) {
  64. e->reading = 0;
  65. aeDeleteFileEvent(loop,e->fd,AE_READABLE);
  66. }
  67. }
  68. static void redisAeAddWrite(void *privdata) {
  69. redisAeEvents *e = (redisAeEvents*)privdata;
  70. aeEventLoop *loop = e->loop;
  71. if (!e->writing) {
  72. e->writing = 1;
  73. aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);
  74. }
  75. }
  76. static void redisAeDelWrite(void *privdata) {
  77. redisAeEvents *e = (redisAeEvents*)privdata;
  78. aeEventLoop *loop = e->loop;
  79. if (e->writing) {
  80. e->writing = 0;
  81. aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);
  82. }
  83. }
  84. static void redisAeCleanup(void *privdata) {
  85. redisAeEvents *e = (redisAeEvents*)privdata;
  86. redisAeDelRead(privdata);
  87. redisAeDelWrite(privdata);
  88. hi_free(e);
  89. }
  90. static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
  91. redisContext *c = &(ac->c);
  92. redisAeEvents *e;
  93. /* Nothing should be attached when something is already attached */
  94. if (ac->ev.data != NULL)
  95. return REDIS_ERR;
  96. /* Create container for context and r/w events */
  97. e = (redisAeEvents*)hi_malloc(sizeof(*e));
  98. if (e == NULL)
  99. return REDIS_ERR;
  100. e->context = ac;
  101. e->loop = loop;
  102. e->fd = c->fd;
  103. e->reading = e->writing = 0;
  104. /* Register functions to start/stop listening for events */
  105. ac->ev.addRead = redisAeAddRead;
  106. ac->ev.delRead = redisAeDelRead;
  107. ac->ev.addWrite = redisAeAddWrite;
  108. ac->ev.delWrite = redisAeDelWrite;
  109. ac->ev.cleanup = redisAeCleanup;
  110. ac->ev.data = e;
  111. return REDIS_OK;
  112. }
  113. #endif