2
0

libevent.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. typedef struct redisLibeventEvents {
  36. redisAsyncContext *context;
  37. struct event rev, wev;
  38. } redisLibeventEvents;
  39. static void redisLibeventReadEvent(int fd, short event, void *arg) {
  40. ((void)fd); ((void)event);
  41. redisLibeventEvents *e = (redisLibeventEvents*)arg;
  42. redisAsyncHandleRead(e->context);
  43. }
  44. static void redisLibeventWriteEvent(int fd, short event, void *arg) {
  45. ((void)fd); ((void)event);
  46. redisLibeventEvents *e = (redisLibeventEvents*)arg;
  47. redisAsyncHandleWrite(e->context);
  48. }
  49. static void redisLibeventAddRead(void *privdata) {
  50. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  51. event_add(&e->rev,NULL);
  52. }
  53. static void redisLibeventDelRead(void *privdata) {
  54. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  55. event_del(&e->rev);
  56. }
  57. static void redisLibeventAddWrite(void *privdata) {
  58. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  59. event_add(&e->wev,NULL);
  60. }
  61. static void redisLibeventDelWrite(void *privdata) {
  62. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  63. event_del(&e->wev);
  64. }
  65. static void redisLibeventCleanup(void *privdata) {
  66. redisLibeventEvents *e = (redisLibeventEvents*)privdata;
  67. event_del(&e->rev);
  68. event_del(&e->wev);
  69. free(e);
  70. }
  71. static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
  72. redisContext *c = &(ac->c);
  73. redisLibeventEvents *e;
  74. /* Nothing should be attached when something is already attached */
  75. if (ac->ev.data != NULL)
  76. return REDIS_ERR;
  77. /* Create container for context and r/w events */
  78. e = (redisLibeventEvents*)malloc(sizeof(*e));
  79. e->context = ac;
  80. /* Register functions to start/stop listening for events */
  81. ac->ev.addRead = redisLibeventAddRead;
  82. ac->ev.delRead = redisLibeventDelRead;
  83. ac->ev.addWrite = redisLibeventAddWrite;
  84. ac->ev.delWrite = redisLibeventDelWrite;
  85. ac->ev.cleanup = redisLibeventCleanup;
  86. ac->ev.data = e;
  87. /* Initialize and install read/write events */
  88. event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
  89. event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e);
  90. event_base_set(base,&e->rev);
  91. event_base_set(base,&e->wev);
  92. return REDIS_OK;
  93. }
  94. #endif