WingMan – Diff between revs 10 and 12
?pathlinks?
Rev 10 | Rev 12 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
- | 2 | using System.IO; |
||
2 | using System.Net; |
3 | using System.Net; |
|
3 | using System.Text; |
4 | using System.Text; |
|
4 | using System.Threading; |
5 | using System.Threading; |
|
5 | using System.Threading.Tasks; |
6 | using System.Threading.Tasks; |
|
6 | using MQTTnet; |
7 | using MQTTnet; |
|
7 | using MQTTnet.Client; |
8 | using MQTTnet.Client; |
|
8 | using MQTTnet.Extensions.ManagedClient; |
9 | using MQTTnet.Extensions.ManagedClient; |
|
9 | using MQTTnet.Protocol; |
10 | using MQTTnet.Protocol; |
|
10 | using MQTTnet.Server; |
11 | using MQTTnet.Server; |
|
11 | using WingMan.Utilities; |
12 | using WingMan.Utilities; |
|
12 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
13 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
|
13 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
14 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
|
14 | |
15 | |
|
15 | namespace WingMan.Communication |
16 | namespace WingMan.Communication |
|
16 | { |
17 | { |
|
17 | public class MqttCommunication : IDisposable |
18 | public class MqttCommunication : IDisposable |
|
18 | { |
19 | { |
|
19 | public delegate void ClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
20 | public delegate void ClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
|
20 | |
21 | |
|
21 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
22 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
|
22 | |
23 | |
|
23 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
24 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
|
24 | |
25 | |
|
25 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
26 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
|
26 | |
27 | |
|
27 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
28 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
|
28 | |
29 | |
|
29 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
30 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
|
30 | |
31 | |
|
31 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
32 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
|
32 | |
33 | |
|
33 | public delegate void ServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
34 | public delegate void ServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
|
34 | |
35 | |
|
35 | public delegate void ServerClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e); |
36 | public delegate void ServerClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e); |
|
36 | |
37 | |
|
37 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
38 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
|
38 | |
39 | |
|
39 | public delegate void ServerStarted(object sender, EventArgs e); |
40 | public delegate void ServerStarted(object sender, EventArgs e); |
|
40 | |
41 | |
|
41 | public delegate void ServerStopped(object sender, EventArgs e); |
42 | public delegate void ServerStopped(object sender, EventArgs e); |
|
42 | |
43 | |
|
43 | public MqttCommunication(TaskScheduler taskScheduler, CancellationToken cancellationToken) |
44 | public MqttCommunication(TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
44 | { |
45 | { |
|
45 | TaskScheduler = taskScheduler; |
46 | TaskScheduler = taskScheduler; |
|
46 | CancellationToken = cancellationToken; |
47 | CancellationToken = cancellationToken; |
|
47 | |
48 | |
|
48 | Client = new MqttFactory().CreateManagedMqttClient(); |
49 | Client = new MqttFactory().CreateManagedMqttClient(); |
|
49 | Server = new MqttFactory().CreateMqttServer(); |
50 | Server = new MqttFactory().CreateMqttServer(); |
|
50 | } |
51 | } |
|
51 | |
52 | |
|
52 | private TaskScheduler TaskScheduler { get; } |
53 | private TaskScheduler TaskScheduler { get; } |
|
53 | |
54 | |
|
54 | private IManagedMqttClient Client { get; } |
55 | private IManagedMqttClient Client { get; } |
|
55 | |
56 | |
|
56 | private IMqttServer Server { get; } |
57 | private IMqttServer Server { get; } |
|
57 | |
58 | |
|
58 | public bool Running { get; set; } |
59 | public bool Running { get; set; } |
|
59 | |
60 | |
|
60 | public string Nick { get; set; } |
61 | public string Nick { get; set; } |
|
61 | |
62 | |
|
62 | private IPAddress IpAddress { get; set; } |
63 | private IPAddress IpAddress { get; set; } |
|
63 | |
64 | |
|
64 | private int Port { get; set; } |
65 | private int Port { get; set; } |
|
65 | |
66 | |
|
66 | private string Password { get; set; } |
67 | private string Password { get; set; } |
|
67 | |
68 | |
|
68 | private CancellationToken CancellationToken { get; } |
69 | private CancellationToken CancellationToken { get; } |
|
69 | |
70 | |
|
70 | public MqttCommunicationType Type { get; set; } |
71 | public MqttCommunicationType Type { get; set; } |
|
71 | |
72 | |
|
72 | public async void Dispose() |
73 | public async void Dispose() |
|
73 | { |
74 | { |
|
74 | await Stop(); |
75 | await Stop(); |
|
75 | } |
76 | } |
|
76 | |
77 | |
|
77 | public event ClientAuthenticationFailed OnClientAuthenticationFailed; |
78 | public event ClientAuthenticationFailed OnClientAuthenticationFailed; |
|
78 | |
79 | |
|
79 | public event ServerAuthenticationFailed OnServerAuthenticationFailed; |
80 | public event ServerAuthenticationFailed OnServerAuthenticationFailed; |
|
80 | |
81 | |
|
81 | public event MessageReceived OnMessageReceived; |
82 | public event MessageReceived OnMessageReceived; |
|
82 | |
83 | |
|
83 | public event ClientConnected OnClientConnected; |
84 | public event ClientConnected OnClientConnected; |
|
84 | |
85 | |
|
85 | public event ClientDisconnected OnClientDisconnected; |
86 | public event ClientDisconnected OnClientDisconnected; |
|
86 | |
87 | |
|
87 | public event ClientConnectionFailed OnClientConnectionFailed; |
88 | public event ClientConnectionFailed OnClientConnectionFailed; |
|
88 | |
89 | |
|
89 | public event ClientUnsubscribed OnClientUnsubscribed; |
90 | public event ClientUnsubscribed OnClientUnsubscribed; |
|
90 | |
91 | |
|
91 | public event ClientSubscribed OnClientSubscribed; |
92 | public event ClientSubscribed OnClientSubscribed; |
|
92 | |
93 | |
|
93 | public event ServerClientDisconnected OnServerClientDisconnected; |
94 | public event ServerClientDisconnected OnServerClientDisconnected; |
|
94 | |
95 | |
|
95 | public event ServerClientConnected OnServerClientConnected; |
96 | public event ServerClientConnected OnServerClientConnected; |
|
96 | |
97 | |
|
97 | public event ServerStarted OnServerStarted; |
98 | public event ServerStarted OnServerStarted; |
|
98 | |
99 | |
|
99 | public event ServerStopped OnServerStopped; |
100 | public event ServerStopped OnServerStopped; |
|
100 | |
101 | |
|
101 | public async Task<bool> Start(MqttCommunicationType type, IPAddress ipAddress, int port, string nick, |
102 | public async Task<bool> Start(MqttCommunicationType type, IPAddress ipAddress, int port, string nick, |
|
102 | string password) |
103 | string password) |
|
103 | { |
104 | { |
|
104 | Type = type; |
105 | Type = type; |
|
105 | IpAddress = ipAddress; |
106 | IpAddress = ipAddress; |
|
106 | Port = port; |
107 | Port = port; |
|
107 | Nick = nick; |
108 | Nick = nick; |
|
108 | Password = password; |
109 | Password = password; |
|
109 | |
110 | |
|
110 | switch (type) |
111 | switch (type) |
|
111 | { |
112 | { |
|
112 | case MqttCommunicationType.Client: |
113 | case MqttCommunicationType.Client: |
|
113 | return await StartClient(); |
114 | return await StartClient(); |
|
114 | case MqttCommunicationType.Server: |
115 | case MqttCommunicationType.Server: |
|
115 | return await StartServer(); |
116 | return await StartServer(); |
|
116 | } |
117 | } |
|
117 | |
118 | |
|
118 | return false; |
119 | return false; |
|
119 | } |
120 | } |
|
120 | |
121 | |
|
121 | private async Task<bool> StartClient() |
122 | private async Task<bool> StartClient() |
|
122 | { |
123 | { |
|
123 | var clientOptions = new MqttClientOptionsBuilder() |
124 | var clientOptions = new MqttClientOptionsBuilder() |
|
124 | .WithTcpServer(IpAddress.ToString(), Port); |
125 | .WithTcpServer(IpAddress.ToString(), Port); |
|
125 | |
126 | |
|
126 | // Setup and start a managed MQTT client. |
127 | // Setup and start a managed MQTT client. |
|
127 | var options = new ManagedMqttClientOptionsBuilder() |
128 | var options = new ManagedMqttClientOptionsBuilder() |
|
128 | .WithClientOptions(clientOptions.Build()) |
129 | .WithClientOptions(clientOptions.Build()) |
|
129 | .Build(); |
130 | .Build(); |
|
130 | |
131 | |
|
131 | BindClientHandlers(); |
132 | BindClientHandlers(); |
|
132 | |
133 | |
|
133 | await Client.SubscribeAsync( |
134 | await Client.SubscribeAsync( |
|
134 | new TopicFilterBuilder() |
135 | new TopicFilterBuilder() |
|
135 | .WithTopic("lobby") |
136 | .WithTopic("lobby") |
|
136 | .Build() |
137 | .Build() |
|
137 | ); |
138 | ); |
|
138 | |
139 | |
|
139 | await Client.SubscribeAsync( |
140 | await Client.SubscribeAsync( |
|
140 | new TopicFilterBuilder() |
141 | new TopicFilterBuilder() |
|
141 | .WithTopic("exchange") |
142 | .WithTopic("exchange") |
|
142 | .Build() |
143 | .Build() |
|
143 | ); |
144 | ); |
|
144 | |
145 | |
|
145 | await Client.SubscribeAsync( |
146 | await Client.SubscribeAsync( |
|
146 | new TopicFilterBuilder() |
147 | new TopicFilterBuilder() |
|
147 | .WithTopic("execute") |
148 | .WithTopic("execute") |
|
148 | .Build() |
149 | .Build() |
|
149 | ); |
150 | ); |
|
150 | |
151 | |
|
151 | await Client.StartAsync(options); |
152 | await Client.StartAsync(options); |
|
152 | |
153 | |
|
153 | Running = true; |
154 | Running = true; |
|
154 | |
155 | |
|
155 | return Running; |
156 | return Running; |
|
156 | } |
157 | } |
|
157 | |
158 | |
|
158 | private async Task StopClient() |
159 | private async Task StopClient() |
|
159 | { |
160 | { |
|
160 | UnbindClientHandlers(); |
161 | UnbindClientHandlers(); |
|
161 | |
162 | |
|
162 | await Client.StopAsync(); |
163 | await Client.StopAsync(); |
|
163 | } |
164 | } |
|
164 | |
165 | |
|
165 | public void BindClientHandlers() |
166 | public void BindClientHandlers() |
|
166 | { |
167 | { |
|
167 | Client.Connected += ClientOnConnected; |
168 | Client.Connected += ClientOnConnected; |
|
168 | Client.Disconnected += ClientOnDisconnected; |
169 | Client.Disconnected += ClientOnDisconnected; |
|
169 | Client.ConnectingFailed += ClientOnConnectingFailed; |
170 | Client.ConnectingFailed += ClientOnConnectingFailed; |
|
170 | Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; |
171 | Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; |
|
171 | } |
172 | } |
|
172 | |
173 | |
|
173 | public void UnbindClientHandlers() |
174 | public void UnbindClientHandlers() |
|
174 | { |
175 | { |
|
175 | Client.Connected -= ClientOnConnected; |
176 | Client.Connected -= ClientOnConnected; |
|
176 | Client.Disconnected -= ClientOnDisconnected; |
177 | Client.Disconnected -= ClientOnDisconnected; |
|
177 | Client.ConnectingFailed -= ClientOnConnectingFailed; |
178 | Client.ConnectingFailed -= ClientOnConnectingFailed; |
|
178 | Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
179 | Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
|
179 | } |
180 | } |
|
180 | |
181 | |
|
181 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
182 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
182 | { |
183 | { |
|
183 | try |
184 | try |
|
184 | { |
185 | { |
|
185 | var load = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
- | ||
186 | |
- | ||
187 | e.ApplicationMessage.Payload = AES.Decrypt(e.ApplicationMessage.Payload, Password); |
186 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
|
188 | |
- | ||
189 | var load2 = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
- | ||
190 | |
187 | |
|
191 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
188 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
192 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
189 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
193 | } |
190 | } |
|
194 | catch (Exception ex) |
191 | catch (Exception ex) |
|
195 | { |
192 | { |
|
196 | await Task.Delay(0, CancellationToken).ContinueWith( |
193 | await Task.Delay(0, CancellationToken).ContinueWith( |
|
197 | _ => OnClientAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
194 | _ => OnClientAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
|
198 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
195 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
199 | } |
196 | } |
|
200 | } |
197 | } |
|
201 | |
198 | |
|
202 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
199 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
|
203 | { |
200 | { |
|
204 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
201 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
|
205 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
202 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
206 | } |
203 | } |
|
207 | |
204 | |
|
208 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
205 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
|
209 | { |
206 | { |
|
210 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
207 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
|
211 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
208 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
212 | } |
209 | } |
|
213 | |
210 | |
|
214 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
211 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
|
215 | { |
212 | { |
|
216 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
213 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
|
217 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
214 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
218 | } |
215 | } |
|
219 | |
216 | |
|
220 | private async Task<bool> StartServer() |
217 | private async Task<bool> StartServer() |
|
221 | { |
218 | { |
|
222 | var optionsBuilder = new MqttServerOptionsBuilder() |
219 | var optionsBuilder = new MqttServerOptionsBuilder() |
|
223 | .WithDefaultEndpointBoundIPAddress(IpAddress) |
220 | .WithDefaultEndpointBoundIPAddress(IpAddress) |
|
224 | .WithSubscriptionInterceptor(MqttSubscriptionIntercept) |
- | ||
225 | .WithConnectionValidator(MqttConnectionValidator) |
- | ||
226 | .WithDefaultEndpointPort(Port); |
221 | .WithDefaultEndpointPort(Port); |
|
227 | |
222 | |
|
228 | BindServerHandlers(); |
223 | BindServerHandlers(); |
|
229 | |
224 | |
|
230 | try |
225 | try |
|
231 | { |
226 | { |
|
232 | await Server.StartAsync(optionsBuilder.Build()); |
227 | await Server.StartAsync(optionsBuilder.Build()); |
|
233 | |
228 | |
|
234 | Running = true; |
229 | Running = true; |
|
235 | } |
230 | } |
|
236 | catch (Exception) |
231 | catch (Exception) |
|
237 | { |
232 | { |
|
238 | Running = false; |
233 | Running = false; |
|
239 | } |
234 | } |
|
240 | |
235 | |
|
241 | return Running; |
236 | return Running; |
|
242 | } |
237 | } |
|
243 | |
- | ||
244 | private void MqttConnectionValidator(MqttConnectionValidatorContext context) |
- | ||
245 | { |
- | ||
246 | context.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; |
- | ||
247 | } |
- | ||
248 | |
238 | |
|
249 | private async Task StopServer() |
239 | private async Task StopServer() |
|
250 | { |
240 | { |
|
251 | UnbindServerHandlers(); |
241 | UnbindServerHandlers(); |
|
252 | |
242 | |
|
253 | await Server.StopAsync(); |
243 | await Server.StopAsync(); |
|
254 | } |
244 | } |
|
255 | |
- | ||
256 | private void MqttSubscriptionIntercept(MqttSubscriptionInterceptorContext context) |
- | ||
257 | { |
- | ||
258 | if (context.TopicFilter.Topic != "lobby" && |
- | ||
259 | context.TopicFilter.Topic != "exchange" && |
- | ||
260 | context.TopicFilter.Topic != "execute") |
- | ||
261 | { |
- | ||
262 | context.AcceptSubscription = false; |
- | ||
263 | context.CloseConnection = true; |
- | ||
264 | return; |
- | ||
265 | } |
- | ||
266 | |
- | ||
267 | context.AcceptSubscription = true; |
- | ||
268 | context.CloseConnection = false; |
- | ||
269 | } |
- | ||
270 | |
245 | |
|
271 | private void BindServerHandlers() |
246 | private void BindServerHandlers() |
|
272 | { |
247 | { |
|
273 | Server.Started += ServerOnStarted; |
248 | Server.Started += ServerOnStarted; |
|
274 | Server.Stopped += ServerOnStopped; |
249 | Server.Stopped += ServerOnStopped; |
|
275 | Server.ClientConnected += ServerOnClientConnected; |
250 | Server.ClientConnected += ServerOnClientConnected; |
|
276 | Server.ClientDisconnected += ServerOnClientDisconnected; |
251 | Server.ClientDisconnected += ServerOnClientDisconnected; |
|
277 | Server.ClientSubscribedTopic += ServerOnClientSubscribedTopic; |
252 | Server.ClientSubscribedTopic += ServerOnClientSubscribedTopic; |
|
278 | Server.ClientUnsubscribedTopic += ServerOnClientUnsubscribedTopic; |
253 | Server.ClientUnsubscribedTopic += ServerOnClientUnsubscribedTopic; |
|
279 | Server.ApplicationMessageReceived += ServerOnApplicationMessageReceived; |
254 | Server.ApplicationMessageReceived += ServerOnApplicationMessageReceived; |
|
280 | } |
255 | } |
|
281 | |
256 | |
|
282 | private void UnbindServerHandlers() |
257 | private void UnbindServerHandlers() |
|
283 | { |
258 | { |
|
284 | Server.Started -= ServerOnStarted; |
259 | Server.Started -= ServerOnStarted; |
|
285 | Server.Stopped -= ServerOnStopped; |
260 | Server.Stopped -= ServerOnStopped; |
|
286 | Server.ClientConnected -= ServerOnClientConnected; |
261 | Server.ClientConnected -= ServerOnClientConnected; |
|
287 | Server.ClientDisconnected -= ServerOnClientDisconnected; |
262 | Server.ClientDisconnected -= ServerOnClientDisconnected; |
|
288 | Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; |
263 | Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; |
|
289 | Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; |
264 | Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; |
|
290 | Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
265 | Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
|
291 | } |
266 | } |
|
292 | |
267 | |
|
293 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
268 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
294 | { |
269 | { |
|
295 | try |
270 | try |
|
296 | { |
271 | { |
|
297 | var load = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
- | ||
298 | |
- | ||
299 | e.ApplicationMessage.Payload = AES.Decrypt(e.ApplicationMessage.Payload, Password); |
272 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
|
300 | |
- | ||
301 | var load2 = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
- | ||
302 | |
273 | |
|
303 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
274 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
304 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
275 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
305 | } |
276 | } |
|
306 | catch (Exception ex) |
277 | catch (Exception ex) |
|
307 | { |
278 | { |
|
308 | foreach (var clientSessionStatus in await Server.GetClientSessionsStatusAsync()) |
279 | foreach (var clientSessionStatus in await Server.GetClientSessionsStatusAsync()) |
|
309 | { |
280 | { |
|
310 | if (!string.Equals(clientSessionStatus.ClientId, e.ClientId, StringComparison.Ordinal)) |
281 | if (!string.Equals(clientSessionStatus.ClientId, e.ClientId, StringComparison.Ordinal)) |
|
311 | continue; |
282 | continue; |
|
312 | |
283 | |
|
313 | await clientSessionStatus.DisconnectAsync(); |
284 | await clientSessionStatus.DisconnectAsync(); |
|
314 | } |
285 | } |
|
315 | |
286 | |
|
316 | await Task.Delay(0, CancellationToken).ContinueWith( |
287 | await Task.Delay(0, CancellationToken).ContinueWith( |
|
317 | _ => OnServerAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
288 | _ => OnServerAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
|
318 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
289 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
319 | } |
290 | } |
|
320 | } |
291 | } |
|
321 | |
292 | |
|
322 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
293 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
|
323 | { |
294 | { |
|
324 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
295 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
|
325 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
296 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
326 | } |
297 | } |
|
327 | |
298 | |
|
328 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
299 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
|
329 | { |
300 | { |
|
330 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
301 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
|
331 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
302 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
332 | } |
303 | } |
|
333 | |
304 | |
|
334 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
305 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
|
335 | { |
306 | { |
|
336 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
307 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
|
337 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
308 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
338 | } |
309 | } |
|
339 | |
310 | |
|
340 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
311 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
|
341 | { |
312 | { |
|
342 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
313 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
|
343 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
314 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
344 | } |
315 | } |
|
345 | |
316 | |
|
346 | private void ServerOnStopped(object sender, EventArgs e) |
317 | private async void ServerOnStopped(object sender, EventArgs e) |
|
347 | { |
318 | { |
|
- | 319 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStopped?.Invoke(sender, e), |
||
348 | OnServerStopped?.Invoke(sender, e); |
320 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
349 | } |
321 | } |
|
350 | |
322 | |
|
351 | private void ServerOnStarted(object sender, EventArgs e) |
323 | private async void ServerOnStarted(object sender, EventArgs e) |
|
- | 324 | { |
||
352 | { |
325 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStarted?.Invoke(sender, e), |
|
353 | OnServerStarted?.Invoke(sender, e); |
326 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
354 | } |
327 | } |
|
355 | |
328 | |
|
356 | public async Task Stop() |
329 | public async Task Stop() |
|
357 | { |
330 | { |
|
358 | switch (Type) |
331 | switch (Type) |
|
359 | { |
332 | { |
|
360 | case MqttCommunicationType.Server: |
333 | case MqttCommunicationType.Server: |
|
361 | await StopServer(); |
334 | await StopServer(); |
|
362 | break; |
335 | break; |
|
363 | case MqttCommunicationType.Client: |
336 | case MqttCommunicationType.Client: |
|
364 | await StopClient(); |
337 | await StopClient(); |
|
365 | break; |
338 | break; |
|
366 | } |
339 | } |
|
367 | |
340 | |
|
368 | Running = false; |
341 | Running = false; |
|
369 | } |
342 | } |
|
370 | |
343 | |
|
371 | public async Task Broadcast(string topic, byte[] payload) |
344 | public async Task Broadcast(string topic, byte[] payload) |
|
372 | { |
345 | { |
|
373 | var encryptedPayload = AES.Encrypt(payload, Password); |
346 | using (var payloadStream = new MemoryStream(await AES.Encrypt(payload, Password))) |
|
374 | |
- | ||
375 | var load = Encoding.UTF8.GetString(encryptedPayload); |
- | ||
376 | |
- | ||
377 | switch (Type) |
- | ||
378 | { |
347 | { |
|
- | 348 | switch (Type) |
||
- | 349 | { |
||
379 | case MqttCommunicationType.Client: |
350 | case MqttCommunicationType.Client: |
|
380 | await Client.PublishAsync(new ManagedMqttApplicationMessage |
351 | await Client.PublishAsync(new [] |
|
381 | { |
352 | { |
|
382 | ApplicationMessage = new MqttApplicationMessage {Topic = topic, Payload = encryptedPayload } |
353 | new MqttApplicationMessage |
|
- | 354 | { |
||
- | 355 | Topic = topic, |
||
- | 356 | Payload = payloadStream.ToArray(), |
||
- | 357 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
||
- | 358 | } |
||
383 | }); |
359 | }); |
|
384 | break; |
360 | break; |
|
385 | case MqttCommunicationType.Server: |
361 | case MqttCommunicationType.Server: |
|
- | 362 | await Server.PublishAsync(new[] |
||
- | 363 | { |
||
- | 364 | new MqttApplicationMessage |
||
- | 365 | { |
||
- | 366 | Topic = topic, |
||
- | 367 | Payload = payloadStream.ToArray(), |
||
386 | await Server.PublishAsync(new MqttApplicationMessage {Topic = topic, Payload = encryptedPayload }); |
368 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
|
- | 369 | } |
||
- | 370 | }); |
||
387 | break; |
371 | break; |
|
- | 372 | } |
||
388 | } |
373 | } |
|
389 | } |
374 | } |
|
390 | } |
375 | } |
|
391 | } |
376 | } |
|
392 | |
377 | |