ivykis.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef __HIREDIS_IVYKIS_H__
  2. #define __HIREDIS_IVYKIS_H__
  3. #include <iv.h>
  4. #include "../hiredis.h"
  5. #include "../async.h"
  6. typedef struct redisIvykisEvents {
  7. redisAsyncContext *context;
  8. struct iv_fd fd;
  9. } redisIvykisEvents;
  10. static void redisIvykisReadEvent(void *arg) {
  11. redisAsyncContext *context = (redisAsyncContext *)arg;
  12. redisAsyncHandleRead(context);
  13. }
  14. static void redisIvykisWriteEvent(void *arg) {
  15. redisAsyncContext *context = (redisAsyncContext *)arg;
  16. redisAsyncHandleWrite(context);
  17. }
  18. static void redisIvykisAddRead(void *privdata) {
  19. redisIvykisEvents *e = (redisIvykisEvents*)privdata;
  20. iv_fd_set_handler_in(&e->fd, redisIvykisReadEvent);
  21. }
  22. static void redisIvykisDelRead(void *privdata) {
  23. redisIvykisEvents *e = (redisIvykisEvents*)privdata;
  24. iv_fd_set_handler_in(&e->fd, NULL);
  25. }
  26. static void redisIvykisAddWrite(void *privdata) {
  27. redisIvykisEvents *e = (redisIvykisEvents*)privdata;
  28. iv_fd_set_handler_out(&e->fd, redisIvykisWriteEvent);
  29. }
  30. static void redisIvykisDelWrite(void *privdata) {
  31. redisIvykisEvents *e = (redisIvykisEvents*)privdata;
  32. iv_fd_set_handler_out(&e->fd, NULL);
  33. }
  34. static void redisIvykisCleanup(void *privdata) {
  35. redisIvykisEvents *e = (redisIvykisEvents*)privdata;
  36. iv_fd_unregister(&e->fd);
  37. free(e);
  38. }
  39. static int redisIvykisAttach(redisAsyncContext *ac) {
  40. redisContext *c = &(ac->c);
  41. redisIvykisEvents *e;
  42. /* Nothing should be attached when something is already attached */
  43. if (ac->ev.data != NULL)
  44. return REDIS_ERR;
  45. /* Create container for context and r/w events */
  46. e = (redisIvykisEvents*)malloc(sizeof(*e));
  47. e->context = ac;
  48. /* Register functions to start/stop listening for events */
  49. ac->ev.addRead = redisIvykisAddRead;
  50. ac->ev.delRead = redisIvykisDelRead;
  51. ac->ev.addWrite = redisIvykisAddWrite;
  52. ac->ev.delWrite = redisIvykisDelWrite;
  53. ac->ev.cleanup = redisIvykisCleanup;
  54. ac->ev.data = e;
  55. /* Initialize and install read/write events */
  56. IV_FD_INIT(&e->fd);
  57. e->fd.fd = c->fd;
  58. e->fd.handler_in = redisIvykisReadEvent;
  59. e->fd.handler_out = redisIvykisWriteEvent;
  60. e->fd.handler_err = NULL;
  61. e->fd.cookie = e->context;
  62. iv_fd_register(&e->fd);
  63. return REDIS_OK;
  64. }
  65. #endif