hio.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "hio.h"
  2. #include "io_watcher.h"
  3. void hio_init(hio_t* io) {
  4. hloop_t* loop = io->loop;
  5. int fd = io->fd;
  6. memset(io, 0, sizeof(hio_t));
  7. io->event_index[0] = io->event_index[1] = -1;
  8. io->loop = loop;
  9. io->fd = fd;
  10. }
  11. hio_t* hio_get(hloop_t* loop, int fd) {
  12. auto iter = loop->ios.find(fd);
  13. if (iter == loop->ios.end()) {
  14. return NULL;
  15. }
  16. return iter->second;
  17. }
  18. hio_t* hio_add(hloop_t* loop, int fd) {
  19. // first try get
  20. hio_t* io = hio_get(loop, fd);
  21. if (io == NULL) {
  22. // then add
  23. #ifdef EVENT_SELECT
  24. if (loop->ios.size() >= FD_SETSIZE) return NULL;
  25. #endif
  26. io = (hio_t*)malloc(sizeof(hio_t));
  27. hio_init(io);
  28. io->event_type= HEVENT_TYPE_IO;
  29. io->event_id = ++loop->event_counter;
  30. loop->ios[fd] = io;
  31. }
  32. io->loop = loop;
  33. io->fd = fd;
  34. io->active = 1;
  35. return io;
  36. }
  37. void hio_del(hio_t* io) {
  38. iowatcher_del_event(io, ALL_EVENTS);
  39. io->events = 0;
  40. // no free, just init for reuse
  41. hio_init(io);
  42. }
  43. hio_t* hio_read (hloop_t* loop, int fd, hio_cb revent_cb, void* revent_userdata) {
  44. hio_t* io = hio_add(loop, fd);
  45. if (io == NULL) return NULL;
  46. io->revent_cb = revent_cb;
  47. io->revent_userdata = revent_userdata;
  48. iowatcher_add_event(io, READ_EVENT);
  49. io->events |= READ_EVENT;
  50. return io;
  51. }
  52. hio_t* hio_write (hloop_t* loop, int fd, hio_cb wevent_cb, void* wevent_userdata) {
  53. hio_t* io = hio_add(loop, fd);
  54. if (io == NULL) return NULL;
  55. io->wevent_cb = wevent_cb;
  56. io->wevent_userdata = wevent_userdata;
  57. iowatcher_add_event(io, WRITE_EVENT);
  58. io->events |= WRITE_EVENT;
  59. return io;
  60. }
  61. #include "hsocket.h"
  62. hio_t* hio_accept (hloop_t* loop, int listenfd, hio_cb revent_cb, void* revent_userdata) {
  63. hio_t* io = hio_read(loop, listenfd, revent_cb, revent_userdata);
  64. if (io) {
  65. nonblocking(listenfd);
  66. io->accept = 1;
  67. }
  68. return io;
  69. }
  70. hio_t* hio_connect(hloop_t* loop, int connfd, hio_cb wevent_cb, void* wevent_userdata) {
  71. hio_t* io = hio_write(loop, connfd, wevent_cb, wevent_userdata);
  72. if (io) {
  73. nonblocking(connfd);
  74. io->connect = 1;
  75. }
  76. return io;
  77. }
  78. static int handle_read_event(hio_t* io) {
  79. if (!io->active) return 0;
  80. if (io->revent_cb) {
  81. io->revent_cb(io, io->revent_userdata);
  82. }
  83. return 0;
  84. }
  85. static int handle_write_event(hio_t* io) {
  86. if (!io->active) return 0;
  87. bool connect_event = io->connect;
  88. if (connect_event) {
  89. // ONESHOT
  90. iowatcher_del_event(io, WRITE_EVENT);
  91. io->connect = 0;
  92. }
  93. if (io->wevent_cb) {
  94. io->wevent_cb(io, io->wevent_userdata);
  95. }
  96. //if (!connect_event && io->write_queue.empty()) {
  97. //iowatcher_del_event(io, WRITE_EVENT);
  98. //}
  99. return 0;
  100. }
  101. int hio_handle_events(hio_t* io) {
  102. if (io->revents & READ_EVENT) {
  103. handle_read_event(io);
  104. }
  105. if (io->revents & WRITE_EVENT) {
  106. handle_write_event(io);
  107. }
  108. io->revents = 0;
  109. return 0;
  110. }