libevent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_LIBEVENT_H__
  31. #define __HIREDIS_LIBEVENT_H__
  32. #include <event.h>
  33. #include "../hiredis.h"
  34. #include "../async.h"
  35. #if 1 //shenzheng 2015-9-21 redis cluster
  36. #include "../hircluster.h"
  37. #endif //shenzheng 2015-9-21 redis cluster
  38. typedef struct redisLibeventEvents {
  39. redisAsyncContext *context;
  40. struct event rev, wev;
  41. } redisLibeventEvents;
  42. static void redisLibeventReadEvent(int fd, short event, void *arg) {
  43. ((void)fd); ((void)event);
  44. redisLibeventEvents *e = (redisLibeventEvents*)arg;
  45. redisAsyncHandleRead(e->context);
  46. }
  47. static void redisLibeventWriteEvent(int fd, short event, void *arg) {
  48. ((void)fd); ((void)event);
  49. redisLibeventEvents *e = (redisLibeventEvents*)arg;
  50. redisAsyncHandleWrite(e->context);
  51. }
  52. static void redisLibeventAddRead(void *privdata) {
  53. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  54. event_add(&e->rev,NULL);
  55. }
  56. static void redisLibeventDelRead(void *privdata) {
  57. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  58. event_del(&e->rev);
  59. }
  60. static void redisLibeventAddWrite(void *privdata) {
  61. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  62. event_add(&e->wev,NULL);
  63. }
  64. static void redisLibeventDelWrite(void *privdata) {
  65. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  66. event_del(&e->wev);
  67. }
  68. static void redisLibeventCleanup(void *privdata) {
  69. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  70. event_del(&e->rev);
  71. event_del(&e->wev);
  72. free(e);
  73. }
  74. static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
  75. redisContext *c = &(ac->c);
  76. redisLibeventEvents *e;
  77. /* Nothing should be attached when something is already attached */
  78. if (ac->ev.data != NULL)
  79. return REDIS_ERR;
  80. /* Create container for context and r/w events */
  81. e = (redisLibeventEvents*)malloc(sizeof(*e));
  82. e->context = ac;
  83. /* Register functions to start/stop listening for events */
  84. ac->ev.addRead = redisLibeventAddRead;
  85. ac->ev.delRead = redisLibeventDelRead;
  86. ac->ev.addWrite = redisLibeventAddWrite;
  87. ac->ev.delWrite = redisLibeventDelWrite;
  88. ac->ev.cleanup = redisLibeventCleanup;
  89. ac->ev.data = e;
  90. /* Initialize and install read/write events */
  91. event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
  92. event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e);
  93. event_base_set(base,&e->rev);
  94. event_base_set(base,&e->wev);
  95. return REDIS_OK;
  96. }
  97. #if 1 //shenzheng 2015-9-21 redis cluster
  98. static int redisLibeventAttach_link(redisAsyncContext *ac, void *base)
  99. {
  100. redisLibeventAttach(ac, (struct event_base *)base);
  101. }
  102. static int redisClusterLibeventAttach(redisClusterAsyncContext *acc, struct event_base *base) {
  103. if(acc == NULL || base == NULL)
  104. {
  105. return REDIS_ERR;
  106. }
  107. acc->adapter = base;
  108. acc->attach_fn = redisLibeventAttach_link;
  109. return REDIS_OK;
  110. }
  111. #endif //shenzheng 2015-9-21 redis cluster
  112. #endif