WingMan – Diff between revs 10 and 12

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 10 Rev 12
Line 1... Line 1...
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;
Line 180... Line 181...
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 var load = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); -  
186   185 {
187 e.ApplicationMessage.Payload = AES.Decrypt(e.ApplicationMessage.Payload, Password); -  
188   -  
Line 189... Line 186...
189 var load2 = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); 186 e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password);
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);
Line 219... Line 216...
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) -  
224 .WithSubscriptionInterceptor(MqttSubscriptionIntercept) -  
225 .WithConnectionValidator(MqttConnectionValidator) 220 .WithDefaultEndpointBoundIPAddress(IpAddress)
Line 226... Line 221...
226 .WithDefaultEndpointPort(Port); 221 .WithDefaultEndpointPort(Port);
Line 227... Line 222...
227   222  
Line 239... Line 234...
239 } 234 }
Line 240... Line 235...
240   235  
241 return Running; 236 return Running;
Line 242... Line -...
242 } -  
243   -  
244 private void MqttConnectionValidator(MqttConnectionValidatorContext context) -  
245 { -  
246 context.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; -  
247 } 237 }
248   238  
249 private async Task StopServer() 239 private async Task StopServer()
Line 250... Line 240...
250 { 240 {
251 UnbindServerHandlers(); 241 UnbindServerHandlers();
Line 252... Line -...
252   -  
253 await Server.StopAsync(); -  
254 } -  
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; 242  
268 context.CloseConnection = false; 243 await Server.StopAsync();
269 } 244 }
270   245  
271 private void BindServerHandlers() 246 private void BindServerHandlers()
Line 292... Line 267...
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 { -  
297 var load = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); -  
298   271 {
299 e.ApplicationMessage.Payload = AES.Decrypt(e.ApplicationMessage.Payload, Password); -  
300   -  
Line 301... Line 272...
301 var load2 = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); 272 e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password);
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);
Line 341... Line 312...
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 }
Line 345... Line 316...
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);
Line 349... Line 321...
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),
Line 353... Line 326...
353 OnServerStarted?.Invoke(sender, e); 326 CancellationToken, TaskContinuationOptions.None, TaskScheduler);
354 } 327 }
355   328  
Line 368... Line 341...
368 Running = false; 341 Running = false;
369 } 342 }
Line 370... Line 343...
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); -  
374   -  
375 var load = Encoding.UTF8.GetString(encryptedPayload); -  
376   -  
377 switch (Type) 346 using (var payloadStream = new MemoryStream(await AES.Encrypt(payload, Password)))
-   347 {
-   348 switch (Type)
378 { 349 {
379 case MqttCommunicationType.Client: 350 case MqttCommunicationType.Client:
380 await Client.PublishAsync(new ManagedMqttApplicationMessage 351 await Client.PublishAsync(new []
381 { 352 {
-   353 new MqttApplicationMessage
-   354 {
-   355 Topic = topic,
-   356 Payload = payloadStream.ToArray(),
-   357 QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
382 ApplicationMessage = new MqttApplicationMessage {Topic = topic, Payload = encryptedPayload } 358 }
383 }); 359 });
384 break; 360 break;
-   361 case MqttCommunicationType.Server:
-   362 await Server.PublishAsync(new[]
-   363 {
-   364 new MqttApplicationMessage
-   365 {
-   366 Topic = topic,
385 case MqttCommunicationType.Server: 367 Payload = payloadStream.ToArray(),
-   368 QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
-   369 }
386 await Server.PublishAsync(new MqttApplicationMessage {Topic = topic, Payload = encryptedPayload }); 370 });
-   371 break;
387 break; 372 }
388 } 373 }
389 } 374 }
390 } 375 }