WingMan – Diff between revs 5 and 7
?pathlinks?
Rev 5 | Rev 7 | |||
---|---|---|---|---|
Line 1... | Line -... | |||
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; |
|
Line 12... | Line 9... | |||
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); |
- | ||
18 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
- | ||
19 | |
- | ||
20 | private MouseKeyBindings MouseKeyBindings { get; set; } |
- | ||
21 | |
- | ||
Line 22... | Line 14... | |||
22 | private ConcurrentDictionary<string, List<MouseKeyBindingExchange>> SynchronizedMouseKeyBindings { get; set; } |
14 | public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); |
|
23 | |
- | ||
24 | private MQTTCommunication MQTTCommunication { get; set; } |
15 | |
|
25 | |
- | ||
26 | private CancellationTokenSource SynchronizationCancellationTokenSource { get; set; } |
- | ||
27 | |
16 | public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication, |
|
28 | public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication) |
17 | TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
29 | { |
18 | { |
|
- | 19 | MouseKeyBindings = mouseKeyBindings; |
||
- | 20 | this.MQTTCommunication = MQTTCommunication; |
||
Line 30... | Line 21... | |||
30 | MouseKeyBindings = mouseKeyBindings; |
21 | CancellationToken = cancellationToken; |
|
Line 31... | Line 22... | |||
31 | this.MQTTCommunication = MQTTCommunication; |
22 | TaskScheduler = taskScheduler; |
|
Line 32... | Line -... | |||
32 | |
- | ||
33 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBindingExchange>>(); |
- | ||
34 | |
23 | |
|
35 | MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; |
24 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>(); |
|
Line -... | Line 25... | |||
- | 25 | |
||
- | 26 | MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; |
||
- | 27 | |
||
- | 28 | Task.Run(Synchronize, CancellationToken); |
||
- | 29 | } |
||
- | 30 | |
||
- | 31 | private MouseKeyBindings MouseKeyBindings { get; } |
||
- | 32 | |
||
- | 33 | private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; } |
||
- | 34 | |
||
36 | |
35 | private MQTTCommunication MQTTCommunication { get; } |
|
- | 36 | |
||
37 | SynchronizationCancellationTokenSource = new CancellationTokenSource(); |
37 | private CancellationToken CancellationToken { get; } |
|
38 | |
38 | private TaskScheduler TaskScheduler { get; } |
|
39 | Task.Run(Synchronize, SynchronizationCancellationTokenSource.Token); |
39 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
|
Line 40... | Line 40... | |||
40 | } |
40 | |
|
41 | |
41 | private async void MqttCommunicationOnOnMessageReceived(object sender, |
|
42 | private void MqttCommunicationOnOnMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
42 | MqttApplicationMessageReceivedEventArgs e) |
|
Line -... | Line 43... | |||
- | 43 | { |
||
43 | { |
44 | if (e.ApplicationMessage.Topic != "exchange") |
|
Line 44... | Line 45... | |||
44 | if (e.ApplicationMessage.Topic != "exchange") |
45 | return; |
|
45 | return; |
46 | |
|
46 | |
47 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
|
Line 47... | Line 48... | |||
47 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
48 | { |
|
48 | { |
49 | memoryStream.Position = 0L; |
|
49 | memoryStream.Position = 0L; |
50 | |
|
Line -... | Line 51... | |||
- | 51 | var mouseKeyBindingsExchange = |
||
- | 52 | (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream); |
||
50 | |
53 | |
|
51 | var mouseKeyBindingsExchange = (MouseKeyBindingsExchange)MouseKeyBindingsExchange.XmlSerializer.Deserialize(memoryStream); |
54 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
|
52 | |
55 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings)) |
|
- | 56 | return; |
||
53 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
57 | |
|
54 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.ExchangeBindings)) |
58 | // Nick does not exist so the bindings will be added. |
|
Line 55... | Line 59... | |||
55 | return; |
59 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
|
56 | |
60 | mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings); |
|
57 | // Nick does not exist so the bindings will be added. |
61 | |
|
58 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
62 | await Task.Delay(0) |
|
59 | mouseKeyBindingsExchange.ExchangeBindings, (s, list) => mouseKeyBindingsExchange.ExchangeBindings); |
63 | .ContinueWith( |
|
Line 60... | Line 64... | |||
60 | |
64 | _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, |
|
61 | OnMouseKeyBindingsSynchronized?.Invoke(sender, |
65 | new MouseKeyBindingsSynchronizedEventArgs( |
|
Line 62... | Line 66... | |||
62 | new MouseKeyBindingsSynchronizedEventArgs( |
66 | mouseKeyBindingsExchange)), |
|
63 | mouseKeyBindingsExchange.ExchangeBindings)); |
67 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
- | 68 | } |
||
64 | } |
69 | } |
|
Line 65... | Line 70... | |||
65 | } |
70 | |
|
Line 66... | Line 71... | |||
66 | |
71 | private async Task Synchronize() |
|
67 | private async Task Synchronize() |
72 | { |
|
68 | { |
- | ||
69 | do |
73 | do |
|
70 | { |
74 | { |
|
71 | await Task.Delay(1000).ConfigureAwait(false); |
75 | await Task.Delay(1000, CancellationToken).ConfigureAwait(false); |
|
72 | |
76 | |