1 /*
2  * lws System Message Distribution
3  *
4  * Copyright (C) 2019 - 2020 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 
26 #if defined(LWS_WITH_SECURE_STREAMS)
27 #define LWS_SMD_SS_RX_HEADER_LEN_EFF	(LWS_SMD_SS_RX_HEADER_LEN)
28 #else
29 #define LWS_SMD_SS_RX_HEADER_LEN_EFF	(0)
30 #endif
31 
32 struct lws_smd_peer;
33 
34 typedef struct lws_smd_msg {
35 	lws_dll2_t			list;
36 
37 	struct lws_smd_peer		*exc;
38 
39 	lws_usec_t			timestamp;
40 	lws_smd_class_t			_class;
41 
42 	uint16_t			length;
43 	uint16_t			refcount;
44 
45 	/* message itself is over-allocated after this */
46 } lws_smd_msg_t;
47 
48 typedef struct lws_smd_peer {
49 	lws_dll2_t			list;
50 
51 #if defined(LWS_WITH_SECURE_STREAMS)
52 	lws_ss_handle_t			*ss_handle; /* LSMDT_SECURE_STREAMS */
53 #endif
54 
55 	lws_smd_notification_cb_t	cb;   /* LSMDT_<other> */
56 	void				*opaque;
57 
58 	/* NULL, or next message we will handle */
59 	lws_smd_msg_t			*tail;
60 
61 	lws_smd_class_t			_class_filter;
62 } lws_smd_peer_t;
63 
64 /*
65  * Manages message distribution
66  *
67  * There is one of these in the lws_context, but the distribution action also
68  * gets involved in delivering to pt event loops individually for SMP case
69  */
70 
71 typedef struct lws_smd {
72 	lws_dll2_owner_t		owner_messages; /* lws_smd_msg_t */
73 	lws_mutex_t			lock_messages;
74 	lws_dll2_owner_t		owner_peers;	/* lws_smd_peer_t */
75 	lws_mutex_t			lock_peers;
76 
77 	/* union of peer class filters, suppress creation of msg classes not set */
78 	lws_smd_class_t			_class_filter;
79 
80 	char				delivering;
81 } lws_smd_t;
82 
83 /* check if this tsi has pending messages to deliver */
84 
85 int
86 lws_smd_message_pending(struct lws_context *ctx);
87 
88 int
89 lws_smd_msg_distribute(struct lws_context *ctx);
90 
91 int
92 _lws_smd_destroy(struct lws_context *ctx);
93 
94 int
95 _lws_smd_msg_send(struct lws_context *ctx, void *pay, struct lws_smd_peer *exc);
96