WingMan – Diff between revs 32 and 35
?pathlinks?
Rev 32 | Rev 35 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Concurrent; |
2 | using System.Collections.Concurrent; |
|
3 | using System.Collections.Generic; |
3 | using System.Collections.Generic; |
|
4 | using System.IO; |
4 | using System.IO; |
|
5 | using System.Linq; |
5 | using System.Linq; |
|
6 | using System.Threading; |
6 | using System.Threading; |
|
7 | using System.Threading.Tasks; |
7 | using System.Threading.Tasks; |
|
8 | using MQTTnet; |
- | ||
9 | using WingMan.Communication; |
8 | using WingMan.Communication; |
|
10 | |
9 | |
|
11 | namespace WingMan.Bindings |
10 | namespace WingMan.Bindings |
|
12 | { |
11 | { |
|
13 | public class KeyBindingsSynchronizer : IDisposable |
12 | public class KeyBindingsSynchronizer : IDisposable |
|
14 | { |
13 | { |
|
15 | public delegate void MouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e); |
14 | public delegate void MouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e); |
|
16 | |
15 | |
|
17 | public KeyBindingsSynchronizer(LocalKeyBindings localKeyBindings, MqttCommunication mqttCommunication, |
16 | public KeyBindingsSynchronizer(LocalKeyBindings localKeyBindings, MqttCommunication mqttCommunication, |
|
18 | TaskScheduler taskScheduler, CancellationToken cancellationToken) |
17 | TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
19 | { |
18 | { |
|
20 | LocalKeyBindings = localKeyBindings; |
19 | LocalKeyBindings = localKeyBindings; |
|
21 | MqttCommunication = mqttCommunication; |
20 | MqttCommunication = mqttCommunication; |
|
22 | CancellationToken = cancellationToken; |
21 | CancellationToken = cancellationToken; |
|
23 | TaskScheduler = taskScheduler; |
22 | TaskScheduler = taskScheduler; |
|
24 | |
23 | |
|
25 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<KeyBinding>>(); |
24 | SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<KeyBinding>>(); |
|
26 | |
25 | |
|
27 | MqttCommunication.OnMessageReceived += MqttCommunicationOnMessageReceived; |
26 | MqttCommunication.OnMessageReceived += MqttCommunicationOnMessageReceived; |
|
28 | |
27 | |
|
29 | Task.Run(PeriodicSynchronize, CancellationToken); |
28 | Task.Run(PeriodicSynchronize, CancellationToken); |
|
30 | } |
29 | } |
|
31 | |
30 | |
|
32 | private LocalKeyBindings LocalKeyBindings { get; } |
31 | private LocalKeyBindings LocalKeyBindings { get; } |
|
33 | |
32 | |
|
34 | private ConcurrentDictionary<string, List<KeyBinding>> SynchronizedMouseKeyBindings { get; } |
33 | private ConcurrentDictionary<string, List<KeyBinding>> SynchronizedMouseKeyBindings { get; } |
|
35 | |
34 | |
|
36 | private MqttCommunication MqttCommunication { get; } |
35 | private MqttCommunication MqttCommunication { get; } |
|
37 | |
36 | |
|
38 | private CancellationToken CancellationToken { get; } |
37 | private CancellationToken CancellationToken { get; } |
|
39 | private TaskScheduler TaskScheduler { get; } |
38 | private TaskScheduler TaskScheduler { get; } |
|
40 | |
39 | |
|
41 | public void Dispose() |
40 | public void Dispose() |
|
42 | { |
41 | { |
|
43 | MqttCommunication.OnMessageReceived -= MqttCommunicationOnMessageReceived; |
42 | MqttCommunication.OnMessageReceived -= MqttCommunicationOnMessageReceived; |
|
44 | } |
43 | } |
|
45 | |
44 | |
|
46 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
45 | public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; |
|
47 | |
46 | |
|
48 | private async void MqttCommunicationOnMessageReceived(object sender, |
47 | private async void MqttCommunicationOnMessageReceived(object sender, |
|
49 | MqttApplicationMessageReceivedEventArgs e) |
48 | MqttCommunicationMessageReceivedEventArgs e) |
|
50 | { |
49 | { |
|
51 | if (e.ApplicationMessage.Topic != "exchange") |
50 | if (e.Topic != "exchange") |
|
52 | return; |
51 | return; |
|
53 | |
52 | |
|
54 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
53 | using (var memoryStream = new MemoryStream()) |
|
- | 54 | { |
||
- | 55 | await e.PayloadStream.CopyToAsync(memoryStream); |
||
55 | { |
56 | |
|
56 | memoryStream.Position = 0L; |
57 | memoryStream.Position = 0L; |
|
57 | |
58 | |
|
58 | var mouseKeyBindingsExchange = |
59 | var mouseKeyBindingsExchange = |
|
59 | (KeyBindingExchange) KeyBindingExchange.XmlSerializer.Deserialize(memoryStream); |
60 | (KeyBindingExchange) KeyBindingExchange.XmlSerializer.Deserialize(memoryStream); |
|
60 | |
61 | |
|
61 | // Do not add own bindings. |
62 | // Do not add own bindings. |
|
62 | if (string.Equals(mouseKeyBindingsExchange.Nick, MqttCommunication.Nick)) |
63 | if (string.Equals(mouseKeyBindingsExchange.Nick, MqttCommunication.Nick)) |
|
63 | return; |
64 | return; |
|
64 | |
65 | |
|
65 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
66 | if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && |
|
66 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.KeyBindings)) |
67 | mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.KeyBindings)) |
|
67 | return; |
68 | return; |
|
68 | |
69 | |
|
69 | await Task.Delay(0) |
70 | await Task.Delay(0) |
|
70 | .ContinueWith( |
71 | .ContinueWith( |
|
71 | _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, |
72 | _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, |
|
72 | new KeyBindingsSynchronizerEventArgs( |
73 | new KeyBindingsSynchronizerEventArgs( |
|
73 | mouseKeyBindingsExchange)), |
74 | mouseKeyBindingsExchange)), |
|
74 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
75 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
75 | |
76 | |
|
76 | // Nick does not exist so the bindings will be added. |
77 | // Nick does not exist so the bindings will be added. |
|
77 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
78 | SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, |
|
78 | mouseKeyBindingsExchange.KeyBindings, (s, list) => mouseKeyBindingsExchange.KeyBindings); |
79 | mouseKeyBindingsExchange.KeyBindings, (s, list) => mouseKeyBindingsExchange.KeyBindings); |
|
79 | } |
80 | } |
|
80 | } |
81 | } |
|
81 | |
82 | |
|
82 | private async Task PeriodicSynchronize() |
83 | private async Task PeriodicSynchronize() |
|
83 | { |
84 | { |
|
84 | do |
85 | do |
|
85 | { |
86 | { |
|
86 | await Task.Delay(1000, CancellationToken); |
87 | await Task.Delay(1000, CancellationToken); |
|
87 | |
88 | |
|
88 | if (!MqttCommunication.Running) |
89 | if (!MqttCommunication.Running) |
|
89 | continue; |
90 | continue; |
|
90 | |
91 | |
|
91 | using (var memoryStream = new MemoryStream()) |
92 | using (var memoryStream = new MemoryStream()) |
|
92 | { |
93 | { |
|
93 | KeyBindingExchange.XmlSerializer.Serialize(memoryStream, |
94 | KeyBindingExchange.XmlSerializer.Serialize(memoryStream, |
|
94 | new KeyBindingExchange(MqttCommunication.Nick, LocalKeyBindings.Bindings)); |
95 | new KeyBindingExchange(MqttCommunication.Nick, LocalKeyBindings.Bindings)); |
|
95 | |
96 | |
|
96 | memoryStream.Position = 0L; |
97 | memoryStream.Position = 0L; |
|
97 | |
98 | |
|
98 | await MqttCommunication.Broadcast("exchange", memoryStream.ToArray()); |
99 | await MqttCommunication.Broadcast("exchange", memoryStream.ToArray()); |
|
99 | } |
100 | } |
|
100 | } while (!CancellationToken.IsCancellationRequested); |
101 | } while (!CancellationToken.IsCancellationRequested); |
|
101 | } |
102 | } |
|
102 | } |
103 | } |
|
103 | } |
- | ||
104 | |
104 | } |
|
- | 105 | |
||
105 |
|
- | ||
106 | |
- | ||
107 | |
- | ||
108 | |
- |