• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
26 #define _WINSOCK_DEPRECATED_NO_WARNINGS
27 #endif
28 #include "private-lib-core.h"
29 
30 
31 int
_lws_plat_service_forced_tsi(struct lws_context * context,int tsi)32 _lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
33 {
34 	struct lws_context_per_thread *pt = &context->pt[tsi];
35 	int m, n, r;
36 
37 	r = lws_service_flag_pending(context, tsi);
38 
39 	/* any socket with events to service? */
40 	for (n = 0; n < (int)pt->fds_count; n++) {
41 		if (!pt->fds[n].revents)
42 			continue;
43 
44                unsigned int fds_count = pt->fds_count;
45 		m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
46 		if (m < 0)
47 			return -1;
48                /* if something closed, fds_count will change, retry this slot */
49                if (pt->fds_count != fds_count)
50 			n--;
51 	}
52 
53 	lws_service_do_ripe_rxflow(pt);
54 
55 	return r;
56 }
57 
58 extern void lws_client_conn_wait_timeout(lws_sorted_usec_list_t *sul);
59 
60 int
_lws_plat_service_tsi(struct lws_context * context,int timeout_ms,int tsi)61 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
62 {
63 	struct lws_context_per_thread *pt;
64 	struct lws_pollfd *pfd;
65 	lws_usec_t timeout_us;
66 	struct lws *wsi;
67 	unsigned int i;
68 	int n;
69 
70 	/* stay dead once we are dead */
71 	if (context == NULL)
72 		return 1;
73 
74 	pt = &context->pt[tsi];
75 
76 	if (!pt->service_tid_detected && context->vhost_list) {
77 		lws_fakewsi_def_plwsa(pt);
78 
79 		lws_fakewsi_prep_plwsa_ctx(context);
80 
81 		pt->service_tid = context->vhost_list->
82 			protocols[0].callback((struct lws *)plwsa,
83 					LWS_CALLBACK_GET_THREAD_ID,
84 						  NULL, NULL, 0);
85 		pt->service_tid_detected = 1;
86 	}
87 
88 	if (timeout_ms < 0)
89 		timeout_ms = 0;
90 	else
91 		/* force a default timeout of 23 days */
92 		timeout_ms = 2000000000;
93 	timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
94 
95 	if (context->event_loop_ops->run_pt)
96 		context->event_loop_ops->run_pt(context, tsi);
97 
98 	for (i = 0; i < pt->fds_count; ++i) {
99 		pfd = &pt->fds[i];
100 
101 		if (!(pfd->events & LWS_POLLOUT))
102 			continue;
103 
104 		wsi = wsi_from_fd(context, pfd->fd);
105 		if (!wsi || wsi->listener)
106 			continue;
107 		if (wsi->sock_send_blocking)
108 			continue;
109 		pfd->revents = LWS_POLLOUT;
110 		n = lws_service_fd(context, pfd);
111 		if (n < 0)
112 			return -1;
113 
114 		/*
115 		 * Force WSAWaitForMultipleEvents() to check events
116 		 * and then return immediately.
117 		 */
118 		timeout_us = 0;
119 
120 		/* if something closed, retry this slot */
121 		if (n)
122 			i--;
123 	}
124 
125 	/*
126 	 * service pending callbacks and get maximum wait time
127 	 */
128 	{
129 		lws_usec_t us;
130 
131 		lws_pt_lock(pt, __func__);
132 		/* don't stay in poll wait longer than next hr timeout */
133 		us = __lws_sul_service_ripe(pt->pt_sul_owner,
134 					    LWS_COUNT_PT_SUL_OWNERS,
135 					    lws_now_usecs());
136 		if (us && us < timeout_us)
137 			/*
138 			 * If something wants zero wait, that's OK, but if the next sul
139 			 * coming ripe is an interval less than our wait resolution,
140 			 * bump it to be the wait resolution.
141 			 */
142 			timeout_us = us < context->us_wait_resolution ?
143 					context->us_wait_resolution : us;
144 
145 		lws_pt_unlock(pt);
146 	}
147 
148 	if (_lws_plat_service_forced_tsi(context, tsi))
149 		timeout_us = 0;
150 
151 	/*
152 	 * is there anybody with pending stuff that needs service forcing?
153 	 */
154 
155 	if (!lws_service_adjust_timeout(context, 1, tsi))
156 		timeout_us = 0;
157 
158 //	lwsl_notice("%s: in %dms, count %d\n", __func__, (int)(timeout_us / 1000), pt->fds_count);
159 //	for (n = 0; n < (int)pt->fds_count; n++)
160 //		lwsl_notice("%s: fd %d ev 0x%x POLLIN %d, POLLOUT %d\n", __func__, (int)pt->fds[n].fd, (int)pt->fds[n].events, POLLIN, POLLOUT);
161 	int d = WSAPoll((WSAPOLLFD *)&pt->fds[0], pt->fds_count, (int)(timeout_us / LWS_US_PER_MS));
162 	if (d < 0) {
163 		lwsl_err("%s: WSAPoll failed: count %d, err %d: %d\n", __func__, pt->fds_count, d, WSAGetLastError());
164 		return 0;
165 	}
166 //	lwsl_notice("%s: out\n", __func__);
167 
168 #if defined(LWS_WITH_TLS)
169 	if (pt->context->tls_ops &&
170 	    pt->context->tls_ops->fake_POLLIN_for_buffered)
171 		pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
172 #endif
173 
174 	for (n = 0; n < (int)pt->fds_count; n++)
175 		if (pt->fds[n].fd != LWS_SOCK_INVALID && pt->fds[n].revents) {
176 //			lwsl_notice("%s: idx %d, revents 0x%x\n", __func__, n, pt->fds[n].revents);
177 			lws_service_fd_tsi(context, &pt->fds[n], tsi);
178 		}
179 
180 	return 0;
181 }
182 
183 int
lws_plat_service(struct lws_context * context,int timeout_ms)184 lws_plat_service(struct lws_context *context, int timeout_ms)
185 {
186 	return _lws_plat_service_tsi(context, timeout_ms, 0);
187 }
188