libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-timeout-timer.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 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
30
31#if defined(STANDALONE)
33#define lws_context lws_context_standalone
34#endif
35
36/*
37 * NOTE: These public enums are part of the abi. If you want to add one,
38 * add it at where specified so existing users are unaffected.
39 */
82
83 /****** add new things just above ---^ ******/
84
86};
87
88#define lws_time_in_microseconds lws_now_usecs
89
90#define LWS_TO_KILL_ASYNC -1
97#define LWS_TO_KILL_SYNC -2
114lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
115
126void
127lws_set_timeout_us(struct lws *wsi, enum pending_timeout reason, lws_usec_t us);
128
129/* helper for clearer LWS_TO_KILL_ASYNC / LWS_TO_KILL_SYNC usage */
130#define lws_wsi_close(w, to_kill) lws_set_timeout(w, 1, to_kill)
131
132
133#define LWS_SET_TIMER_USEC_CANCEL ((lws_usec_t)-1ll)
134#define LWS_USEC_PER_SEC ((lws_usec_t)1000000)
135
164lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs);
165
167
168typedef void (*sul_cb_t)(struct lws_sorted_usec_list *sul);
169
170typedef struct lws_sorted_usec_list {
171 struct lws_dll2 list; /* simplify the code by keeping this at start */
174 uint32_t latency_us; /* us it may safely be delayed */
176
177/*
178 * There are multiple sul owners to allow accounting for, a) events that must
179 * wake from suspend, and b) events that can be missued due to suspend
180 */
181#define LWS_COUNT_PT_SUL_OWNERS 2
182
183#define LWSSULLI_MISS_IF_SUSPENDED 0
184#define LWSSULLI_WAKE_IF_SUSPENDED 1
185
186/*
187 * lws_sul2_schedule() - schedule a callback
188 *
189 * \param context: the lws_context
190 * \param tsi: the thread service index (usually 0)
191 * \param flags: LWSSULLI_...
192 * \param sul: pointer to the sul element
193 *
194 * Generic callback-at-a-later time function. The callback happens on the
195 * event loop thread context.
196 *
197 * Although the api has us resultion, the actual resolution depends on the
198 * platform and may be, eg, 1ms.
199 *
200 * This doesn't allocate and doesn't fail.
201 *
202 * If flags contains LWSSULLI_WAKE_IF_SUSPENDED, the scheduled event is placed
203 * on a sul owner list that, if the system has entered low power suspend mode,
204 * tries to arrange that the system should wake from platform suspend just
205 * before the event is due. Scheduled events without this flag will be missed
206 * in the case the system is in suspend and nothing else happens to have woken
207 * it.
208 *
209 * You can call it again with another us value to change the delay or move the
210 * event to a different owner (ie, wake or miss on suspend).
211 */
213lws_sul2_schedule(struct lws_context *context, int tsi, int flags,
215
216/*
217 * lws_sul_cancel() - cancel scheduled callback
218 *
219 * \param sul: pointer to the sul element
220 *
221 * If it's scheduled, remove the sul from its owning sorted list.
222 * If not scheduled, it's a NOP.
223 *
224 * With LWS_MAX_SMP > 1 it takes the lock of the service thread whose list the
225 * sul is on while it unlinks it, so it may be used from any thread. As for
226 * any lws lock, a thread holding one service thread's lock must also hold the
227 * context lock to cancel a sul on another's list. The sul must have been
228 * scheduled by the lws_sul_schedule() family: a sul you put on a list of your
229 * own with __lws_sul_insert() is removed with lws_dll2_remove() instead.
230 */
233
234/*
235 * lws_sul_cancel_tsi() - cancel a scheduled callback from any thread
236 *
237 * \param ctx: the lws_context
238 * \param tsi: the service thread index the sul was scheduled on
239 * \param sul: pointer to the sul element
240 *
241 * The same as lws_sul_cancel(), which finds and locks the service thread the
242 * sul is on itself; \p ctx and \p tsi are no longer needed. lws_sul_schedule()
243 * likewise takes the lock of the pt it schedules on, so scheduling onto
244 * another service thread's pt is safe, although that thread only sees the new
245 * deadline when it next wakes.
246 */
248lws_sul_cancel_tsi(struct lws_context *ctx, int tsi,
250
251/*
252 * lws_sul_earliest_wakeable_event() - get earliest wake-from-suspend event
253 *
254 * \param ctx: the lws context
255 * \param pearliest: pointer to lws_usec_t to take the result
256 *
257 * Either returns 1 if no pending event, or 0 and sets *pearliest to the
258 * MONOTONIC time of the current earliest next expected event.
259 */
261lws_sul_earliest_wakeable_event(struct lws_context *ctx, lws_usec_t *pearliest);
262
263/*
264 * For backwards compatibility
265 *
266 * If us is LWS_SET_TIMER_USEC_CANCEL, the sul is removed from the scheduler.
267 * New code can use lws_sul_cancel()
268 */
269
271lws_sul_schedule(struct lws_context *ctx, int tsi, lws_sorted_usec_list_t *sul,
272 sul_cb_t _cb, lws_usec_t _us);
274lws_sul_schedule_wakesuspend(struct lws_context *ctx, int tsi,
276 lws_usec_t _us);
277
278#if defined(LWS_WITH_SUL_DEBUGGING)
302lws_sul_debug_zombies(struct lws_context *ctx, void *po, size_t len,
303 const char *destroy_description);
304#else
305#define lws_sul_debug_zombies(_a, _b, _c, _d)
306#endif
307
308/*
309 * lws_validity_confirmed() - reset the validity timer for a network connection
310 *
311 * \param wsi: the connection that saw traffic proving the connection valid
312 *
313 * Network connections are subject to intervals defined by the context, the
314 * vhost if server connections, or the client connect info if a client
315 * connection. If the connection goes longer than the specified time since
316 * last observing traffic that can only happen if traffic is passing in both
317 * directions, then lws will try to create a PING transaction on the network
318 * connection.
319 *
320 * If the connection reaches the specified `.secs_since_valid_hangup` time
321 * still without any proof of validity, the connection will be closed.
322 *
323 * If the PONG comes, or user code observes traffic that satisfies the proof
324 * that both directions are passing traffic to the peer and calls this api,
325 * the connection validity timer is reset and the scheme repeats.
326 *
327 * Raw connections have no PING of their own. One taken from an http CONNECT
328 * (a LWSMPRO_CONNECT mount) is under the vhost's policy anyway: its protocol
329 * must call this when it sees the connection alive, eg, its own inline PONG,
330 * within .secs_since_valid_hangup, or it is hung up. On other raw connections
331 * there is no validity timer, and calling it does nothing.
332 */
334lws_validity_confirmed(struct lws *wsi);
335
336/*
337 * These are not normally needed, they're exported for the case there's code
338 * using lws_sul for which lws is an optional link dependency.
339 */
340
343
346/*
347 * An event loop calls this holding the context lock and then the pt lock, in
348 * that order, so the callbacks it runs may close connections or destroy
349 * vhosts, which take the same locks in the same order.
350 */
351
352#if defined(STANDALONE)
353#undef lws_context
354#endif
355
struct lws_dll2_owner lws_dll2_owner_t
unsigned int uint32_t
#define LWS_EXTERN
int64_t lws_usec_t
#define LWS_VISIBLE
LWS_VISIBLE LWS_EXTERN int lws_sul_earliest_wakeable_event(struct lws_context *ctx, lws_usec_t *pearliest)
LWS_VISIBLE LWS_EXTERN void lws_sul_cancel_tsi(struct lws_context *ctx, int tsi, lws_sorted_usec_list_t *sul)
LWS_VISIBLE LWS_EXTERN void lws_sul_cancel(lws_sorted_usec_list_t *sul)
LWS_VISIBLE LWS_EXTERN lws_usec_t __lws_sul_service_ripe(lws_dll2_owner_t *own, int own_len, lws_usec_t usnow)
LWS_VISIBLE LWS_EXTERN void lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs)
LWS_VISIBLE LWS_EXTERN void lws_validity_confirmed(struct lws *wsi)
pending_timeout
@ PENDING_TIMEOUT_THREADPOOL_TASK
@ PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE
@ PENDING_TIMEOUT_ESTABLISH_WITH_SERVER
@ PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE
@ PENDING_TIMEOUT_SHUTDOWN_FLUSH
@ PENDING_TIMEOUT_CLIENT_CONN_IDLE
@ PENDING_TIMEOUT_KILLED_BY_PARENT
@ PENDING_TIMEOUT_AWAITING_PING
@ PENDING_TIMEOUT_USER_OK
@ PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
@ PENDING_TIMEOUT_HTTP_RESPONSE
@ PENDING_TIMEOUT_THREADPOOL
@ PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE
@ PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE
@ PENDING_TIMEOUT_USER_REASON_BASE
@ PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG
@ NO_PENDING_TIMEOUT
@ PENDING_TIMEOUT_CGI
@ PENDING_TIMEOUT_SSL_ACCEPT
@ PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD
@ PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY
@ PENDING_TIMEOUT_CLOSE_SEND
@ PENDING_TIMEOUT_UDP_IDLE
@ PENDING_TIMEOUT_HOLDING_AH
@ PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY
@ PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING
@ PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND
@ PENDING_TIMEOUT_CLOSE_ACK
@ PENDING_TIMEOUT_UNUSED1
@ PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE
@ PENDING_TIMEOUT_LAGGING
@ PENDING_TIMEOUT_KILLED_BY_SSL_INFO
@ PENDING_TIMEOUT_AWAITING_SOCKS_GREETING_REPLY
@ PENDING_TIMEOUT_HTTP_CONTENT
@ PENDING_TIMEOUT_KILLED_BY_PROXY_CLIENT_CLOSE
void(* sul_cb_t)(struct lws_sorted_usec_list *sul)
#define lws_sul_debug_zombies(_a, _b, _c, _d)
LWS_VISIBLE LWS_EXTERN void lws_sul_schedule(struct lws_context *ctx, int tsi, lws_sorted_usec_list_t *sul, sul_cb_t _cb, lws_usec_t _us)
struct lws_sorted_usec_list lws_sorted_usec_list_t
void lws_set_timeout_us(struct lws *wsi, enum pending_timeout reason, lws_usec_t us)
LWS_VISIBLE LWS_EXTERN int __lws_sul_insert(lws_dll2_owner_t *own, lws_sorted_usec_list_t *sul)
LWS_VISIBLE LWS_EXTERN void lws_sul_schedule_wakesuspend(struct lws_context *ctx, int tsi, lws_sorted_usec_list_t *sul, sul_cb_t _cb, lws_usec_t _us)
LWS_VISIBLE LWS_EXTERN void lws_sul2_schedule(struct lws_context *context, int tsi, int flags, lws_sorted_usec_list_t *sul)
LWS_VISIBLE LWS_EXTERN void lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)