select.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "iowatcher.h"
  2. #ifdef EVENT_SELECT
  3. #include "hplatform.h"
  4. #ifdef OS_LINUX
  5. #include <sys/select.h>
  6. #endif
  7. #include "hdef.h"
  8. #include "hevent.h"
  9. #include "hsocket.h"
  10. typedef struct select_ctx_s {
  11. int max_fd;
  12. fd_set readfds;
  13. fd_set writefds;
  14. int nread;
  15. int nwrite;
  16. } select_ctx_t;
  17. int iowatcher_init(hloop_t* loop) {
  18. if (loop->iowatcher) return 0;
  19. select_ctx_t* select_ctx = (select_ctx_t*)malloc(sizeof(select_ctx_t));
  20. select_ctx->max_fd = -1;
  21. FD_ZERO(&select_ctx->readfds);
  22. FD_ZERO(&select_ctx->writefds);
  23. select_ctx->nread = 0;
  24. select_ctx->nwrite = 0;
  25. loop->iowatcher = select_ctx;
  26. return 0;
  27. }
  28. int iowatcher_cleanup(hloop_t* loop) {
  29. SAFE_FREE(loop->iowatcher);
  30. return 0;
  31. }
  32. int iowatcher_add_event(hloop_t* loop, int fd, int events) {
  33. if (loop->iowatcher == NULL) {
  34. iowatcher_init(loop);
  35. }
  36. select_ctx_t* select_ctx = (select_ctx_t*)loop->iowatcher;
  37. if (fd > select_ctx->max_fd) {
  38. select_ctx->max_fd = fd;
  39. }
  40. if (events & READ_EVENT) {
  41. if (!FD_ISSET(fd, &select_ctx->readfds)) {
  42. FD_SET(fd, &select_ctx->readfds);
  43. select_ctx->nread++;
  44. }
  45. }
  46. if (events & WRITE_EVENT) {
  47. if (!FD_ISSET(fd, &select_ctx->writefds)) {
  48. FD_SET(fd, &select_ctx->writefds);
  49. select_ctx->nwrite++;
  50. }
  51. }
  52. return 0;
  53. }
  54. int iowatcher_del_event(hloop_t* loop, int fd, int events) {
  55. select_ctx_t* select_ctx = (select_ctx_t*)loop->iowatcher;
  56. if (select_ctx == NULL) return 0;
  57. if (fd == select_ctx->max_fd) {
  58. select_ctx->max_fd = -1;
  59. }
  60. if (events & READ_EVENT) {
  61. if (FD_ISSET(fd, &select_ctx->readfds)) {
  62. FD_CLR(fd, &select_ctx->readfds);
  63. select_ctx->nread--;
  64. }
  65. }
  66. if (events & WRITE_EVENT) {
  67. if (FD_ISSET(fd, &select_ctx->writefds)) {
  68. FD_CLR(fd, &select_ctx->writefds);
  69. select_ctx->nwrite--;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int find_max_active_fd(hloop_t* loop) {
  75. hio_t* io = NULL;
  76. for (int i = loop->ios.maxsize-1; i >= 0; --i) {
  77. io = loop->ios.ptr[i];
  78. if (io && io->active && io->events) return i;
  79. }
  80. return -1;
  81. }
  82. static int remove_bad_fds(hloop_t* loop) {
  83. select_ctx_t* select_ctx = (select_ctx_t*)loop->iowatcher;
  84. if (select_ctx == NULL) return 0;
  85. int badfds = 0;
  86. int error = 0;
  87. socklen_t optlen = sizeof(error);
  88. for (int fd = 0; fd <= select_ctx->max_fd; ++fd) {
  89. if (FD_ISSET(fd, &select_ctx->readfds) ||
  90. FD_ISSET(fd, &select_ctx->writefds)) {
  91. error = 0;
  92. optlen = sizeof(int);
  93. if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&error, &optlen) < 0 || error != 0) {
  94. ++badfds;
  95. hio_t* io = loop->ios.ptr[fd];
  96. if (io) {
  97. hio_del(io, ALL_EVENTS);
  98. }
  99. }
  100. }
  101. }
  102. return badfds;
  103. }
  104. int iowatcher_poll_events(hloop_t* loop, int timeout) {
  105. select_ctx_t* select_ctx = (select_ctx_t*)loop->iowatcher;
  106. if (select_ctx == NULL) return 0;
  107. if (select_ctx->nread == 0 && select_ctx->nwrite == 0) {
  108. return 0;
  109. }
  110. int max_fd = select_ctx->max_fd;
  111. fd_set readfds = select_ctx->readfds;
  112. fd_set writefds = select_ctx->writefds;
  113. if (max_fd == -1) {
  114. select_ctx->max_fd = max_fd = find_max_active_fd(loop);
  115. }
  116. struct timeval tv, *tp;
  117. if (timeout == INFINITE) {
  118. tp = NULL;
  119. }
  120. else {
  121. tv.tv_sec = timeout / 1000;
  122. tv.tv_usec = (timeout % 1000) * 1000;
  123. tp = &tv;
  124. }
  125. int nselect = select(max_fd+1, &readfds, &writefds, NULL, tp);
  126. if (nselect < 0) {
  127. #ifdef OS_WIN
  128. if (WSAGetLastError() == WSAENOTSOCK) {
  129. #else
  130. if (errno == EBADF) {
  131. perror("select");
  132. #endif
  133. remove_bad_fds(loop);
  134. return -EBADF;
  135. }
  136. return nselect;
  137. }
  138. if (nselect == 0) return 0;
  139. int nevents = 0;
  140. int revents = 0;
  141. for (int fd = 0; fd <= max_fd; ++fd) {
  142. revents = 0;
  143. if (FD_ISSET(fd, &readfds)) {
  144. ++nevents;
  145. revents |= READ_EVENT;
  146. }
  147. if (FD_ISSET(fd, &writefds)) {
  148. ++nevents;
  149. revents |= WRITE_EVENT;
  150. }
  151. if (revents) {
  152. hio_t* io = loop->ios.ptr[fd];
  153. if (io) {
  154. io->revents = revents;
  155. EVENT_PENDING(io);
  156. }
  157. }
  158. if (nevents == nselect) break;
  159. }
  160. return nevents;
  161. }
  162. #endif