ae.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef __HIREDIS_AE_H__
  2. #define __HIREDIS_AE_H__
  3. #include <sys/types.h>
  4. #include <ae.h>
  5. #include "../hiredis.h"
  6. #include "../async.h"
  7. typedef struct redisAeEvents {
  8. redisAsyncContext *context;
  9. aeEventLoop *loop;
  10. int fd;
  11. int reading, writing;
  12. } redisAeEvents;
  13. static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  14. ((void)el); ((void)fd); ((void)mask);
  15. redisAeEvents *e = (redisAeEvents*)privdata;
  16. redisAsyncHandleRead(e->context);
  17. }
  18. static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {
  19. ((void)el); ((void)fd); ((void)mask);
  20. redisAeEvents *e = (redisAeEvents*)privdata;
  21. redisAsyncHandleWrite(e->context);
  22. }
  23. static void redisAeAddRead(void *privdata) {
  24. redisAeEvents *e = (redisAeEvents*)privdata;
  25. aeEventLoop *loop = e->loop;
  26. if (!e->reading) {
  27. e->reading = 1;
  28. aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);
  29. }
  30. }
  31. static void redisAeDelRead(void *privdata) {
  32. redisAeEvents *e = (redisAeEvents*)privdata;
  33. aeEventLoop *loop = e->loop;
  34. if (e->reading) {
  35. e->reading = 0;
  36. aeDeleteFileEvent(loop,e->fd,AE_READABLE);
  37. }
  38. }
  39. static void redisAeAddWrite(void *privdata) {
  40. redisAeEvents *e = (redisAeEvents*)privdata;
  41. aeEventLoop *loop = e->loop;
  42. if (!e->writing) {
  43. e->writing = 1;
  44. aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);
  45. }
  46. }
  47. static void redisAeDelWrite(void *privdata) {
  48. redisAeEvents *e = (redisAeEvents*)privdata;
  49. aeEventLoop *loop = e->loop;
  50. if (e->writing) {
  51. e->writing = 0;
  52. aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);
  53. }
  54. }
  55. static void redisAeCleanup(void *privdata) {
  56. redisAeEvents *e = (redisAeEvents*)privdata;
  57. redisAeDelRead(privdata);
  58. redisAeDelWrite(privdata);
  59. free(e);
  60. }
  61. static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
  62. redisContext *c = &(ac->c);
  63. redisAeEvents *e;
  64. /* Nothing should be attached when something is already attached */
  65. if (ac->ev.data != NULL)
  66. return REDIS_ERR;
  67. /* Create container for context and r/w events */
  68. e = (redisAeEvents*)malloc(sizeof(*e));
  69. e->context = ac;
  70. e->loop = loop;
  71. e->fd = c->fd;
  72. e->reading = e->writing = 0;
  73. /* Register functions to start/stop listening for events */
  74. ac->ev.addRead = redisAeAddRead;
  75. ac->ev.delRead = redisAeDelRead;
  76. ac->ev.addWrite = redisAeAddWrite;
  77. ac->ev.delWrite = redisAeDelWrite;
  78. ac->ev.cleanup = redisAeCleanup;
  79. ac->ev.data = e;
  80. return REDIS_OK;
  81. }
  82. #endif