WingMan – Diff between revs 5 and 6

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 5 Rev 6
Line 1... Line 1...
1 using System; 1 using System;
2 using System.Net; 2 using System.Net;
-   3 using System.Threading;
3 using System.Threading.Tasks; 4 using System.Threading.Tasks;
4 using MQTTnet; 5 using MQTTnet;
5 using MQTTnet.Client; 6 using MQTTnet.Client;
6 using MQTTnet.Extensions.ManagedClient; 7 using MQTTnet.Extensions.ManagedClient;
7 using MQTTnet.Server; 8 using MQTTnet.Server;
Line 10... Line 11...
10   11  
11 namespace WingMan.Communication 12 namespace WingMan.Communication
12 { 13 {
13 public class MQTTCommunication 14 public class MQTTCommunication
14 { 15 {
15 public MQTTCommunication(IPAddress ipAddress, int port, string nick) :this() 16 public MQTTCommunication(TaskScheduler taskScheduler)
16 { 17 {
17 IPAddress = ipAddress; -  
18 Port = port; 18 TaskScheduler = taskScheduler;
19 Nick = nick; -  
Line 20... Line -...
20 } -  
21   -  
22 public MQTTCommunication() 19 CancellationTokenSource = new CancellationTokenSource();
23 { 20  
24 Client = new MqttFactory().CreateManagedMqttClient(); 21 Client = new MqttFactory().CreateManagedMqttClient();
Line -... Line 22...
-   22 Server = new MqttFactory().CreateMqttServer();
-   23 }
25 Server = new MqttFactory().CreateMqttServer(); 24  
Line 26... Line 25...
26 } 25 private TaskScheduler TaskScheduler { get; set; }
Line 27... Line 26...
27   26  
Line 35... Line 34...
35   34  
Line 36... Line 35...
36 private IPAddress IPAddress { get; set; } 35 private IPAddress IPAddress { get; set; }
Line -... Line 36...
-   36  
-   37 private int Port { get; set; }
37   38  
Line 38... Line 39...
38 private int Port { get; set; } 39 private CancellationTokenSource CancellationTokenSource { get; set; }
Line 39... Line 40...
39   40  
Line 148... Line 149...
148 Client.Disconnected -= ClientOnDisconnected; 149 Client.Disconnected -= ClientOnDisconnected;
149 Client.ConnectingFailed -= ClientOnConnectingFailed; 150 Client.ConnectingFailed -= ClientOnConnectingFailed;
150 Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; 151 Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived;
151 } 152 }
Line 152... Line 153...
152   153  
153 private void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) 154 private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
154 { 155 {
-   156 await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e),
155 OnMessageReceived?.Invoke(sender, e); 157 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
Line 156... Line 158...
156 } 158 }
157   159  
158 private void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) 160 private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e)
-   161 {
159 { 162 await Task.Delay(0).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e),
Line 160... Line 163...
160 OnClientConnectionFailed?.Invoke(sender, e); 163 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
161 } 164 }
162   165  
-   166 private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e)
163 private void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) 167 {
Line 164... Line 168...
164 { 168 await Task.Delay(0).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e),
165 OnClientDisconnected?.Invoke(sender, e); 169 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
166 } 170 }
-   171  
167   172 private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e)
Line 168... Line 173...
168 private void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) 173 {
169 { 174 await Task.Delay(0).ContinueWith(_ => OnClientConnected?.Invoke(sender, e),
170 OnClientConnected?.Invoke(sender, e); 175 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
Line 225... Line 230...
225 Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; 230 Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic;
226 Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; 231 Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic;
227 Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; 232 Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived;
228 } 233 }
Line 229... Line 234...
229   234  
230 private void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) 235 private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
231 { 236 {
-   237 await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e),
232 OnMessageReceived?.Invoke(sender, e); 238 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
Line 233... Line 239...
233 } 239 }
234   240  
235 private void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) 241 private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e)
-   242 {
236 { 243 await Task.Delay(0).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e),
Line 237... Line 244...
237 OnClientUnsubscribed?.Invoke(sender, e); 244 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
238 } 245 }
239   246  
-   247 private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e)
240 private void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) 248 {
Line 241... Line 249...
241 { 249 await Task.Delay(0).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e),
242 OnClientSubscribed?.Invoke(sender, e); 250 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
243 } 251 }
-   252  
244   253 private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e)
Line 245... Line 254...
245 private void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) 254 {
246 { 255 await Task.Delay(0).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e),
247 OnServerClientDisconnected?.Invoke(sender, e); 256 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false);
-   257 }
248 } 258  
Line 249... Line 259...
249   259 private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e)
250 private void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) 260 {
251 { 261 await Task.Delay(0).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e),