WingMan – Diff between revs 9 and 10

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 9 Rev 10
Line -... Line 1...
-   1 using System;
1 using System.Collections.Concurrent; 2 using System.Collections.Concurrent;
2 using System.Collections.Generic; 3 using System.Collections.Generic;
3 using System.IO; 4 using System.IO;
4 using System.Linq; 5 using System.Linq;
5 using System.Threading; 6 using System.Threading;
6 using System.Threading.Tasks; 7 using System.Threading.Tasks;
7 using MQTTnet; 8 using MQTTnet;
8 using WingMan.Communication; 9 using WingMan.Communication;
Line 9... Line 10...
9   10  
10 namespace WingMan.MouseKey 11 namespace WingMan.MouseKey
11 { 12 {
12 public class MouseKeyBindingsSynchronizer 13 public class KeyBindingsSynchronizer : IDisposable
13 { 14 {
Line 14... Line 15...
14 public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); 15 public delegate void MouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e);
15   16  
16 public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MqttCommunication mqttCommunication, 17 public KeyBindingsSynchronizer(LocalKeyBindings localKeyBindings, MqttCommunication mqttCommunication,
17 TaskScheduler taskScheduler, CancellationToken cancellationToken) 18 TaskScheduler taskScheduler, CancellationToken cancellationToken)
18 { 19 {
19 MouseKeyBindings = mouseKeyBindings; 20 LocalKeyBindings = localKeyBindings;
20 MqttCommunication = mqttCommunication; 21 MqttCommunication = mqttCommunication;
Line 21... Line 22...
21 CancellationToken = cancellationToken; 22 CancellationToken = cancellationToken;
Line 22... Line 23...
22 TaskScheduler = taskScheduler; 23 TaskScheduler = taskScheduler;
Line 23... Line 24...
23   24  
24 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>(); 25 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<KeyBinding>>();
Line 25... Line 26...
25   26  
Line 26... Line 27...
26 mqttCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; 27 MqttCommunication.OnMessageReceived += MqttCommunicationOnMessageReceived;
Line 27... Line 28...
27   28  
Line 28... Line 29...
28 Task.Run(Synchronize, CancellationToken); 29 Task.Run(PeriodicSynchronize, CancellationToken);
29 } 30 }
-   31  
-   32 private LocalKeyBindings LocalKeyBindings { get; }
-   33  
-   34 private ConcurrentDictionary<string, List<KeyBinding>> SynchronizedMouseKeyBindings { get; }
-   35  
-   36 private MqttCommunication MqttCommunication { get; }
30   37  
Line 31... Line 38...
31 private MouseKeyBindings MouseKeyBindings { get; } 38 private CancellationToken CancellationToken { get; }
32   39 private TaskScheduler TaskScheduler { get; }
33 private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; } 40  
34   41 public void Dispose()
35 private MqttCommunication MqttCommunication { get; } 42 {
Line 36... Line 43...
36   43 MqttCommunication.OnMessageReceived -= MqttCommunicationOnMessageReceived;
37 private CancellationToken CancellationToken { get; } 44 }
38 private TaskScheduler TaskScheduler { get; } 45  
Line 39... Line 46...
39 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; 46 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
40   47  
Line 41... Line 48...
41 private async void MqttCommunicationOnOnMessageReceived(object sender, 48 private async void MqttCommunicationOnMessageReceived(object sender,
42 MqttApplicationMessageReceivedEventArgs e) 49 MqttApplicationMessageReceivedEventArgs e)
43 { 50 {
Line 44... Line -...
44 if (e.ApplicationMessage.Topic != "exchange") -  
45 return; 51 if (e.ApplicationMessage.Topic != "exchange")
46   52 return;
-   53  
Line 47... Line 54...
47 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) 54 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
48 { 55 {
49 memoryStream.Position = 0L; 56 memoryStream.Position = 0L;
50   57  
51 var mouseKeyBindingsExchange = 58 var mouseKeyBindingsExchange =
52 (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream); 59 (KeyBindingExchange) KeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
-   60  
-   61 // Do not add own bindings.
-   62 if (string.Equals(mouseKeyBindingsExchange.Nick, MqttCommunication.Nick))
-   63 return;
53   64  
54 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && 65 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
Line 55... Line 66...
55 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings)) 66 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.KeyBindings))
56 return; 67 return;
57   68  
58 // Nick does not exist so the bindings will be added. 69 await Task.Delay(0)
59 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, 70 .ContinueWith(
Line 60... Line 71...
60 mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings); 71 _ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
61   72 new KeyBindingsSynchronizerEventArgs(
Line 62... Line 73...
62 await Task.Delay(0) 73 mouseKeyBindingsExchange)),
63 .ContinueWith( 74 CancellationToken, TaskContinuationOptions.None, TaskScheduler);
64 _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, 75  
65 new MouseKeyBindingsSynchronizedEventArgs( 76 // Nick does not exist so the bindings will be added.
Line 66... Line 77...
66 mouseKeyBindingsExchange)), 77 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
Line 67... Line 78...
67 CancellationToken, TaskContinuationOptions.None, TaskScheduler); 78 mouseKeyBindingsExchange.KeyBindings, (s, list) => mouseKeyBindingsExchange.KeyBindings);
68 } 79 }
69 } 80 }
70   81  
71 private async Task Synchronize() 82 private async Task PeriodicSynchronize()
72 { 83 {