WingMan – Diff between revs 35 and 36

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