WingMan – Diff between revs 14 and 32

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