2
0

windows-service.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  22. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  23. #endif
  24. #include "core/private.h"
  25. LWS_EXTERN int
  26. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
  27. {
  28. struct lws_context_per_thread *pt;
  29. WSANETWORKEVENTS networkevents;
  30. struct lws_pollfd *pfd;
  31. struct lws *wsi;
  32. unsigned int i;
  33. DWORD ev;
  34. int n;
  35. /* stay dead once we are dead */
  36. if (context == NULL || !context->vhost_list)
  37. return 1;
  38. pt = &context->pt[tsi];
  39. if (!pt->service_tid_detected) {
  40. struct lws _lws;
  41. memset(&_lws, 0, sizeof(_lws));
  42. _lws.context = context;
  43. pt->service_tid = context->vhost_list->
  44. protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
  45. NULL, NULL, 0);
  46. pt->service_tid_detected = 1;
  47. }
  48. if (timeout_ms < 0) {
  49. if (lws_service_flag_pending(context, tsi)) {
  50. /* any socket with events to service? */
  51. for (n = 0; n < (int)pt->fds_count; n++) {
  52. int m;
  53. if (!pt->fds[n].revents)
  54. continue;
  55. m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
  56. if (m < 0)
  57. return -1;
  58. /* if something closed, retry this slot */
  59. if (m)
  60. n--;
  61. }
  62. }
  63. return 0;
  64. }
  65. if (context->event_loop_ops->run_pt)
  66. context->event_loop_ops->run_pt(context, tsi);
  67. for (i = 0; i < pt->fds_count; ++i) {
  68. pfd = &pt->fds[i];
  69. if (!(pfd->events & LWS_POLLOUT))
  70. continue;
  71. wsi = wsi_from_fd(context, pfd->fd);
  72. if (!wsi || wsi->listener)
  73. continue;
  74. if (wsi->sock_send_blocking)
  75. continue;
  76. pfd->revents = LWS_POLLOUT;
  77. n = lws_service_fd(context, pfd);
  78. if (n < 0)
  79. return -1;
  80. /* Force WSAWaitForMultipleEvents() to check events and then return immediately. */
  81. timeout_ms = 0;
  82. /* if something closed, retry this slot */
  83. if (n)
  84. i--;
  85. }
  86. /*
  87. * is there anybody with pending stuff that needs service forcing?
  88. */
  89. if (!lws_service_adjust_timeout(context, 1, tsi)) {
  90. /* -1 timeout means just do forced service */
  91. _lws_plat_service_tsi(context, -1, pt->tid);
  92. /* still somebody left who wants forced service? */
  93. if (!lws_service_adjust_timeout(context, 1, pt->tid))
  94. /* yes... come back again quickly */
  95. timeout_ms = 0;
  96. }
  97. if (timeout_ms) {
  98. lws_usec_t t;
  99. lws_pt_lock(pt, __func__);
  100. /* don't stay in poll wait longer than next hr timeout */
  101. t = __lws_hrtimer_service(pt);
  102. if ((lws_usec_t)timeout_ms * 1000 > t)
  103. timeout_ms = (int)(t / 1000);
  104. lws_pt_unlock(pt);
  105. }
  106. ev = WSAWaitForMultipleEvents(1, &pt->events, FALSE, timeout_ms, FALSE);
  107. if (ev == WSA_WAIT_EVENT_0) {
  108. unsigned int eIdx;
  109. WSAResetEvent(pt->events);
  110. if (pt->context->tls_ops &&
  111. pt->context->tls_ops->fake_POLLIN_for_buffered)
  112. pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
  113. for (eIdx = 0; eIdx < pt->fds_count; ++eIdx) {
  114. unsigned int err;
  115. if (WSAEnumNetworkEvents(pt->fds[eIdx].fd, 0,
  116. &networkevents) == SOCKET_ERROR) {
  117. lwsl_err("WSAEnumNetworkEvents() failed "
  118. "with error %d\n", LWS_ERRNO);
  119. return -1;
  120. }
  121. pfd = &pt->fds[eIdx];
  122. pfd->revents = (short)networkevents.lNetworkEvents;
  123. err = networkevents.iErrorCode[FD_CONNECT_BIT];
  124. if ((networkevents.lNetworkEvents & FD_CONNECT) &&
  125. err && err != LWS_EALREADY &&
  126. err != LWS_EINPROGRESS && err != LWS_EWOULDBLOCK &&
  127. err != WSAEINVAL) {
  128. lwsl_debug("Unable to connect errno=%d\n", err);
  129. pfd->revents |= LWS_POLLHUP;
  130. }
  131. if (pfd->revents & LWS_POLLOUT) {
  132. wsi = wsi_from_fd(context, pfd->fd);
  133. if (wsi)
  134. wsi->sock_send_blocking = 0;
  135. }
  136. /* if something closed, retry this slot */
  137. if (pfd->revents & LWS_POLLHUP)
  138. --eIdx;
  139. if (pfd->revents) {
  140. recv(pfd->fd, NULL, 0, 0);
  141. lws_service_fd_tsi(context, pfd, tsi);
  142. }
  143. }
  144. }
  145. if (ev == WSA_WAIT_TIMEOUT)
  146. lws_service_fd(context, NULL);
  147. return 0;
  148. }
  149. int
  150. lws_plat_service(struct lws_context *context, int timeout_ms)
  151. {
  152. return _lws_plat_service_tsi(context, timeout_ms, 0);
  153. }
  154. void
  155. lws_plat_service_periodic(struct lws_context *context)
  156. {
  157. }