2
0

libuv.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef __HIREDIS_LIBUV_H__
  2. #define __HIREDIS_LIBUV_H__
  3. #include <uv.h>
  4. #include "../hiredis.h"
  5. #include "../async.h"
  6. #include <string.h>
  7. typedef struct redisLibuvEvents {
  8. redisAsyncContext* context;
  9. uv_poll_t handle;
  10. int events;
  11. } redisLibuvEvents;
  12. int redisLibuvAttach(redisAsyncContext*, uv_loop_t*);
  13. static void redisLibuvPoll(uv_poll_t* handle, int status, int events) {
  14. redisLibuvEvents* p = (redisLibuvEvents*)handle->data;
  15. if (status != 0) {
  16. return;
  17. }
  18. if (events & UV_READABLE) {
  19. redisAsyncHandleRead(p->context);
  20. }
  21. if (events & UV_WRITABLE) {
  22. redisAsyncHandleWrite(p->context);
  23. }
  24. }
  25. static void redisLibuvAddRead(void *privdata) {
  26. redisLibuvEvents* p = (redisLibuvEvents*)privdata;
  27. p->events |= UV_READABLE;
  28. uv_poll_start(&p->handle, p->events, redisLibuvPoll);
  29. }
  30. static void redisLibuvDelRead(void *privdata) {
  31. redisLibuvEvents* p = (redisLibuvEvents*)privdata;
  32. p->events &= ~UV_READABLE;
  33. if (p->events) {
  34. uv_poll_start(&p->handle, p->events, redisLibuvPoll);
  35. } else {
  36. uv_poll_stop(&p->handle);
  37. }
  38. }
  39. static void redisLibuvAddWrite(void *privdata) {
  40. redisLibuvEvents* p = (redisLibuvEvents*)privdata;
  41. p->events |= UV_WRITABLE;
  42. uv_poll_start(&p->handle, p->events, redisLibuvPoll);
  43. }
  44. static void redisLibuvDelWrite(void *privdata) {
  45. redisLibuvEvents* p = (redisLibuvEvents*)privdata;
  46. p->events &= ~UV_WRITABLE;
  47. if (p->events) {
  48. uv_poll_start(&p->handle, p->events, redisLibuvPoll);
  49. } else {
  50. uv_poll_stop(&p->handle);
  51. }
  52. }
  53. static void on_close(uv_handle_t* handle) {
  54. redisLibuvEvents* p = (redisLibuvEvents*)handle->data;
  55. free(p);
  56. }
  57. static void redisLibuvCleanup(void *privdata) {
  58. redisLibuvEvents* p = (redisLibuvEvents*)privdata;
  59. uv_close((uv_handle_t*)&p->handle, on_close);
  60. }
  61. static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) {
  62. redisContext *c = &(ac->c);
  63. if (ac->ev.data != NULL) {
  64. return REDIS_ERR;
  65. }
  66. ac->ev.addRead = redisLibuvAddRead;
  67. ac->ev.delRead = redisLibuvDelRead;
  68. ac->ev.addWrite = redisLibuvAddWrite;
  69. ac->ev.delWrite = redisLibuvDelWrite;
  70. ac->ev.cleanup = redisLibuvCleanup;
  71. redisLibuvEvents* p = (redisLibuvEvents*)malloc(sizeof(*p));
  72. if (!p) {
  73. return REDIS_ERR;
  74. }
  75. memset(p, 0, sizeof(*p));
  76. if (uv_poll_init(loop, &p->handle, c->fd) != 0) {
  77. return REDIS_ERR;
  78. }
  79. ac->ev.data = p;
  80. p->handle.data = p;
  81. p->context = ac;
  82. return REDIS_OK;
  83. }
  84. #endif