WingMan – Diff between revs 5 and 7

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 5 Rev 7
1 using System; -  
2 using System.Collections.Concurrent; 1 using System.Collections.Concurrent;
3 using System.Collections.Generic; 2 using System.Collections.Generic;
4 using System.IO; 3 using System.IO;
5 using System.Linq; 4 using System.Linq;
6 using System.Text; -  
7 using System.Threading; 5 using System.Threading;
8 using System.Threading.Tasks; 6 using System.Threading.Tasks;
9 using MQTTnet; 7 using MQTTnet;
10 using MQTTnet.Extensions.ManagedClient; -  
11 using WingMan.Communication; 8 using WingMan.Communication;
12   9  
13 namespace WingMan.MouseKey 10 namespace WingMan.MouseKey
14 { 11 {
15 public class MouseKeyBindingsSynchronizer 12 public class MouseKeyBindingsSynchronizer
16 { 13 {
17 public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); 14 public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e);
18 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; -  
19   -  
20 private MouseKeyBindings MouseKeyBindings { get; set; } -  
21   -  
22 private ConcurrentDictionary<string, List<MouseKeyBindingExchange>> SynchronizedMouseKeyBindings { get; set; } -  
23   15  
24 private MQTTCommunication MQTTCommunication { get; set; } -  
25   16 public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication,
26 private CancellationTokenSource SynchronizationCancellationTokenSource { get; set; } -  
27   -  
28 public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication) 17 TaskScheduler taskScheduler, CancellationToken cancellationToken)
29 { 18 {
30 MouseKeyBindings = mouseKeyBindings; 19 MouseKeyBindings = mouseKeyBindings;
31 this.MQTTCommunication = MQTTCommunication; 20 this.MQTTCommunication = MQTTCommunication;
-   21 CancellationToken = cancellationToken;
-   22 TaskScheduler = taskScheduler;
32   23  
33 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBindingExchange>>(); 24 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>();
34   25  
35 MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; 26 MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived;
36   -  
37 SynchronizationCancellationTokenSource = new CancellationTokenSource(); -  
38   27  
39 Task.Run(Synchronize, SynchronizationCancellationTokenSource.Token); 28 Task.Run(Synchronize, CancellationToken);
-   29 }
-   30  
-   31 private MouseKeyBindings MouseKeyBindings { get; }
-   32  
-   33 private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; }
-   34  
-   35 private MQTTCommunication MQTTCommunication { get; }
-   36  
-   37 private CancellationToken CancellationToken { get; }
-   38 private TaskScheduler TaskScheduler { get; }
40 } 39 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
-   40  
41   41 private async void MqttCommunicationOnOnMessageReceived(object sender,
42 private void MqttCommunicationOnOnMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) 42 MqttApplicationMessageReceivedEventArgs e)
43 { 43 {
44 if (e.ApplicationMessage.Topic != "exchange") 44 if (e.ApplicationMessage.Topic != "exchange")
45 return; 45 return;
46   46  
47 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) 47 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
48 { 48 {
49 memoryStream.Position = 0L; 49 memoryStream.Position = 0L;
-   50  
50   51 var mouseKeyBindingsExchange =
51 var mouseKeyBindingsExchange = (MouseKeyBindingsExchange)MouseKeyBindingsExchange.XmlSerializer.Deserialize(memoryStream); 52 (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
52   53  
53 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && 54 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
54 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.ExchangeBindings)) 55 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings))
55 return; 56 return;
56   57  
57 // Nick does not exist so the bindings will be added. 58 // Nick does not exist so the bindings will be added.
58 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, 59 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
59 mouseKeyBindingsExchange.ExchangeBindings, (s, list) => mouseKeyBindingsExchange.ExchangeBindings); 60 mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings);
-   61  
-   62 await Task.Delay(0)
60   63 .ContinueWith(
61 OnMouseKeyBindingsSynchronized?.Invoke(sender, 64 _ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
62 new MouseKeyBindingsSynchronizedEventArgs( 65 new MouseKeyBindingsSynchronizedEventArgs(
-   66 mouseKeyBindingsExchange)),
63 mouseKeyBindingsExchange.ExchangeBindings)); 67 CancellationToken, TaskContinuationOptions.None, TaskScheduler);
64 } 68 }
65 } 69 }
66   70  
67 private async Task Synchronize() 71 private async Task Synchronize()
68 { 72 {
69 do 73 do
70 { 74 {
71 await Task.Delay(1000).ConfigureAwait(false); 75 await Task.Delay(1000, CancellationToken).ConfigureAwait(false);
72   76  
73 if (!MouseKeyBindings.Bindings.Any()) 77 if (!MQTTCommunication.Running)
74 continue; 78 continue;
75   79  
76 using (var memoryStream = new MemoryStream()) 80 using (var memoryStream = new MemoryStream())
77 { 81 {
-   82 MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream,
78 MouseKeyBindingsExchange.XmlSerializer.Serialize(memoryStream, new MouseKeyBindingsExchange(MQTTCommunication.Nick, MouseKeyBindings)); 83 new MouseKeyBindingExchange(MQTTCommunication.Nick, MouseKeyBindings.Bindings));
79   84  
80 memoryStream.Position = 0L; 85 memoryStream.Position = 0L;
81   86  
82 await MQTTCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false); 87 await MQTTCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false);
83 } 88 }
84   -  
85 } while (!SynchronizationCancellationTokenSource.Token.IsCancellationRequested); 89 } while (!CancellationToken.IsCancellationRequested);
86 } 90 }
87 } 91 }
88 } 92 }
89   93