/trunk/WingMan/Communication/MQTTCommunication.cs |
@@ -1,5 +1,6 @@ |
using System; |
using System.Net; |
using System.Threading; |
using System.Threading.Tasks; |
using MQTTnet; |
using MQTTnet.Client; |
@@ -12,19 +13,17 @@ |
{ |
public class MQTTCommunication |
{ |
public MQTTCommunication(IPAddress ipAddress, int port, string nick) :this() |
public MQTTCommunication(TaskScheduler taskScheduler) |
{ |
IPAddress = ipAddress; |
Port = port; |
Nick = nick; |
} |
TaskScheduler = taskScheduler; |
CancellationTokenSource = new CancellationTokenSource(); |
|
public MQTTCommunication() |
{ |
Client = new MqttFactory().CreateManagedMqttClient(); |
Server = new MqttFactory().CreateMqttServer(); |
} |
|
private TaskScheduler TaskScheduler { get; set; } |
|
private IManagedMqttClient Client { get; } |
|
private IMqttServer Server { get; } |
@@ -37,6 +36,8 @@ |
|
private int Port { get; set; } |
|
private CancellationTokenSource CancellationTokenSource { get; set; } |
|
public MQTTCommunicationType Type { get; set; } |
|
public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
@@ -150,24 +151,28 @@ |
Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
} |
|
private void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
{ |
OnMessageReceived?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler); |
} |
|
private void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
{ |
OnClientConnectionFailed?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
{ |
OnClientDisconnected?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
{ |
OnClientConnected?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private async Task StartServer() |
@@ -227,29 +232,34 @@ |
Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
} |
|
private void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
{ |
OnMessageReceived?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
{ |
OnClientUnsubscribed?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
{ |
OnClientSubscribed?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
{ |
OnServerClientDisconnected?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
{ |
OnServerClientConnected?.Invoke(sender, e); |
await Task.Delay(0).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler).ConfigureAwait(false); |
} |
|
private void ServerOnStopped(object sender, EventArgs e) |