Simulation Core
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. hi_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*)hi_malloc(sizeof(*e));
  47. if (e == NULL)
  48. return REDIS_ERR;
  49. e->context = ac;
  50. /* Register functions to start/stop listening for events */
  51. ac->ev.addRead = redisIvykisAddRead;
  52. ac->ev.delRead = redisIvykisDelRead;
  53. ac->ev.addWrite = redisIvykisAddWrite;
  54. ac->ev.delWrite = redisIvykisDelWrite;
  55. ac->ev.cleanup = redisIvykisCleanup;
  56. ac->ev.data = e;
  57. /* Initialize and install read/write events */
  58. IV_FD_INIT(&e->fd);
  59. e->fd.fd = c->fd;
  60. e->fd.handler_in = redisIvykisReadEvent;
  61. e->fd.handler_out = redisIvykisWriteEvent;
  62. e->fd.handler_err = NULL;
  63. e->fd.cookie = e->context;
  64. iv_fd_register(&e->fd);
  65. return REDIS_OK;
  66. }
  67. #endif