select.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Wez Furlong <wez@thebrainroom.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_network.h"
  20. #ifdef PHP_WIN32
  21. /* $Id$ */
  22. /* Win32 select() will only work with sockets, so we roll our own implementation here.
  23. * - If you supply only sockets, this simply passes through to winsock select().
  24. * - If you supply file handles, there is no way to distinguish between
  25. * ready for read/write or OOB, so any set in which the handle is found will
  26. * be marked as ready.
  27. * - If you supply a mixture of handles and sockets, the system will interleave
  28. * calls between select() and WaitForMultipleObjects(). The time slicing may
  29. * cause this function call to take up to 100 ms longer than you specified.
  30. * - Calling this with NULL sets as a portable way to sleep with sub-second
  31. * accuracy is not supported.
  32. * */
  33. PHPAPI int php_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
  34. {
  35. ULONGLONG ms_total, limit;
  36. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  37. int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
  38. int n_handles = 0, i;
  39. fd_set sock_read, sock_write, sock_except;
  40. fd_set aread, awrite, aexcept;
  41. int sock_max_fd = -1;
  42. struct timeval tvslice;
  43. int retcode;
  44. #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
  45. /* calculate how long we need to wait in milliseconds */
  46. if (tv == NULL) {
  47. ms_total = INFINITE;
  48. } else {
  49. ms_total = tv->tv_sec * 1000;
  50. ms_total += tv->tv_usec / 1000;
  51. }
  52. FD_ZERO(&sock_read);
  53. FD_ZERO(&sock_write);
  54. FD_ZERO(&sock_except);
  55. /* build an array of handles for non-sockets */
  56. for (i = 0; i < max_fd; i++) {
  57. if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
  58. handles[n_handles] = (HANDLE)(zend_uintptr_t)_get_osfhandle(i);
  59. if (handles[n_handles] == INVALID_HANDLE_VALUE) {
  60. /* socket */
  61. if (SAFE_FD_ISSET(i, rfds)) {
  62. FD_SET((uint)i, &sock_read);
  63. }
  64. if (SAFE_FD_ISSET(i, wfds)) {
  65. FD_SET((uint)i, &sock_write);
  66. }
  67. if (SAFE_FD_ISSET(i, efds)) {
  68. FD_SET((uint)i, &sock_except);
  69. }
  70. if (i > sock_max_fd) {
  71. sock_max_fd = i;
  72. }
  73. } else {
  74. handle_slot_to_fd[n_handles] = i;
  75. n_handles++;
  76. }
  77. }
  78. }
  79. if (n_handles == 0) {
  80. /* plain sockets only - let winsock handle the whole thing */
  81. return select(max_fd, rfds, wfds, efds, tv);
  82. }
  83. /* mixture of handles and sockets; lets multiplex between
  84. * winsock and waiting on the handles */
  85. FD_ZERO(&aread);
  86. FD_ZERO(&awrite);
  87. FD_ZERO(&aexcept);
  88. limit = GetTickCount64() + ms_total;
  89. do {
  90. retcode = 0;
  91. if (sock_max_fd >= 0) {
  92. /* overwrite the zero'd sets here; the select call
  93. * will clear those that are not active */
  94. aread = sock_read;
  95. awrite = sock_write;
  96. aexcept = sock_except;
  97. tvslice.tv_sec = 0;
  98. tvslice.tv_usec = 100000;
  99. retcode = select(sock_max_fd+1, &aread, &awrite, &aexcept, &tvslice);
  100. }
  101. if (n_handles > 0) {
  102. /* check handles */
  103. DWORD wret;
  104. wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
  105. if (wret == WAIT_TIMEOUT) {
  106. /* set retcode to 0; this is the default.
  107. * select() may have set it to something else,
  108. * in which case we leave it alone, so this branch
  109. * does nothing */
  110. ;
  111. } else if (wret == WAIT_FAILED) {
  112. if (retcode == 0) {
  113. retcode = -1;
  114. }
  115. } else {
  116. if (retcode < 0) {
  117. retcode = 0;
  118. }
  119. for (i = 0; i < n_handles; i++) {
  120. if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
  121. if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
  122. FD_SET((uint)handle_slot_to_fd[i], &aread);
  123. }
  124. if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
  125. FD_SET((uint)handle_slot_to_fd[i], &awrite);
  126. }
  127. if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
  128. FD_SET((uint)handle_slot_to_fd[i], &aexcept);
  129. }
  130. retcode++;
  131. }
  132. }
  133. }
  134. }
  135. } while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit));
  136. if (rfds) {
  137. *rfds = aread;
  138. }
  139. if (wfds) {
  140. *wfds = awrite;
  141. }
  142. if (efds) {
  143. *efds = aexcept;
  144. }
  145. return retcode;
  146. }
  147. #endif
  148. /*
  149. * Local variables:
  150. * tab-width: 4
  151. * c-basic-offset: 4
  152. * End:
  153. * vim600: noet sw=4 ts=4 fdm=marker
  154. * vim<600: noet sw=4 ts=4
  155. */