WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 5  →  ?path2? @ 6
/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)
/trunk/WingMan/WingManForm.Designer.cs
@@ -100,7 +100,7 @@
this.WingBindingsComboBox.Name = "WingBindingsComboBox";
this.WingBindingsComboBox.Size = new System.Drawing.Size(232, 21);
this.WingBindingsComboBox.TabIndex = 5;
this.WingBindingsComboBox.SelectedIndexChanged += new System.EventHandler(this.WingBindingsComboBoxIndexChanged);
this.WingBindingsComboBox.SelectionChangeCommitted += new System.EventHandler(this.WingBindingsComboBoxSelectionChangeCompleted);
//
// button2
//
/trunk/WingMan/WingManForm.cs
@@ -3,6 +3,7 @@
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using WingMan.Communication;
@@ -17,8 +18,10 @@
{
InitializeComponent();
 
MQTTCommunication = new MQTTCommunication();
FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
 
MQTTCommunication = new MQTTCommunication(FormTaskScheduler);
 
MouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>());
 
HelmBindingSource = new BindingSource
@@ -49,44 +52,42 @@
 
private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e)
{
this.Invoke((MethodInvoker) delegate
foreach (var binding in e.ExchangeBindings)
{
foreach (var binding in e.ExchangeBindings)
{
ActivityTextBox.AppendText(
$"{Strings.Synchronized_bindings_with_client} : {binding.Nick} : {binding.Names.Count}{Environment.NewLine}");
ActivityTextBox.AppendText(
$"{Strings.Synchronized_bindings_with_client} : {binding.Nick} : {binding.Names.Count}{Environment.NewLine}");
 
 
var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding =>
string.Equals(exchangeBinding.Nick, binding.Nick, StringComparison.Ordinal));
var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding =>
string.Equals(exchangeBinding.Nick, binding.Nick, StringComparison.Ordinal));
 
// If the nick has not been registered add it.
if (exchangeBindings == null)
{
MouseKeyBindingsExchange.ExchangeBindings.Add(binding);
continue;
}
// If the nick has not been registered add it.
if (exchangeBindings == null)
{
MouseKeyBindingsExchange.ExchangeBindings.Add(binding);
continue;
}
 
// If the nick has been added, then update the binding names.
exchangeBindings.Names = binding.Names;
// If the nick has been added, then update the binding names.
exchangeBindings.Names = binding.Names;
 
// Update wing list box.
var selectedExchangeBinding = (MouseKeyBindingExchange)WingBindingsComboBox.SelectedItem;
if (selectedExchangeBinding == null || !string.Equals(selectedExchangeBinding.Nick, binding.Nick))
continue;
// Update wing list box.
var selectedExchangeBinding = (MouseKeyBindingExchange) WingBindingsComboBox.SelectedItem;
if (selectedExchangeBinding == null || !string.Equals(selectedExchangeBinding.Nick, binding.Nick))
continue;
 
WingBindingsListBox.Items.Clear();
WingBindingsListBox.DisplayMember = "Name";
WingBindingsListBox.ValueMember = "Name";
WingBindingsListBox.Items.AddRange(exchangeBindings.Names.Select(name => (object)name).ToArray());
WingBindingsListBox.Items.Clear();
WingBindingsListBox.DisplayMember = "Name";
WingBindingsListBox.ValueMember = "Name";
WingBindingsListBox.Items.AddRange(exchangeBindings.Names.Select(name => (object) name).ToArray());
 
}
}
 
WingBindingSource.ResetBindings(false);
 
});
WingBindingSource.ResetBindings(false);
}
 
private static TaskScheduler FormTaskScheduler { get; set; }
 
private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; }
 
private List<string> MouseKeyCombo { get; set; }
@@ -329,9 +330,9 @@
LobbySayTextBox.Text = string.Empty;
}
 
private void WingBindingsComboBoxIndexChanged(object sender, EventArgs e)
private void WingBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e)
{
var exchangeBinding = (MouseKeyBindingExchange) WingBindingsComboBox.SelectedItem;
var exchangeBinding = (MouseKeyBindingExchange)WingBindingsComboBox.SelectedItem;
if (exchangeBinding == null)
return;