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