WingMan – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 1 Rev 2
Line 1... Line 1...
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
-   3 using System.IO;
3 using System.Linq; 4 using System.Linq;
4 using System.Net; 5 using System.Net;
-   6 using System.Runtime.Remoting.Messaging;
5 using System.Text; 7 using System.Text;
6 using System.Threading.Tasks; 8 using System.Threading.Tasks;
-   9 using System.Windows.Forms;
-   10 using System.Xml.Serialization;
7 using MQTTnet; 11 using MQTTnet;
8 using MQTTnet.Client; 12 using MQTTnet.Client;
9 using MQTTnet.Extensions.ManagedClient; 13 using MQTTnet.Extensions.ManagedClient;
Line 10... Line 14...
10   14  
11 namespace WingMan.Communication 15 namespace WingMan.Communication
12 { 16 {
13 public class MQTTClient : IDisposable 17 public class MQTTClient : IDisposable
-   18 {
14 { 19 private WingManForm WingManForm { get; set; }
15 private IManagedMqttClient Client { get; set; } 20 private IManagedMqttClient Client { get; set; }
Line -... Line 21...
-   21 public bool ClientRunning { get; set; }
-   22  
16 public bool ClientRunning { get; set; } 23 public string Nick { get; set; }
17   24  
18 public MQTTClient() 25 public MQTTClient()
19 { 26 {
Line -... Line 27...
-   27 Client = new MqttFactory().CreateManagedMqttClient();
-   28 }
-   29  
-   30 public MQTTClient(WingManForm wingManForm) : this()
-   31 {
20 Client = new MqttFactory().CreateManagedMqttClient(); 32 WingManForm = wingManForm;
21 } 33 }
-   34  
-   35 public async Task Start(IPAddress ipAddress, int port, string nick)
-   36 {
-   37 Nick = nick;
-   38  
22   39 var clientOptions = new MqttClientOptionsBuilder()
23 public async Task Start(IPAddress ipAddress, int port) 40 .WithTcpServer(ipAddress.ToString(), port);
24 { 41  
25 // Setup and start a managed MQTT client. 42 // Setup and start a managed MQTT client.
26 var options = new ManagedMqttClientOptionsBuilder() -  
27 .WithAutoReconnectDelay(TimeSpan.FromSeconds(5)) -  
28 .WithClientOptions(new MqttClientOptionsBuilder() 43 var options = new ManagedMqttClientOptionsBuilder()
-   44 .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
29 .WithTcpServer(ipAddress.ToString(), port) 45 .WithClientOptions(clientOptions.Build())
Line 30... Line 46...
30 .WithTls().Build()) 46 .Build();
31 .Build(); 47  
32 BindHandlers(); 48 BindHandlers();
33   -  
34 await Client.SubscribeAsync( 49  
35 new TopicFilterBuilder() 50 await Client.SubscribeAsync(
Line -... Line 51...
-   51 new TopicFilterBuilder()
-   52 .WithTopic("lobby")
-   53 .Build()
-   54 );
-   55  
-   56 await Client.SubscribeAsync(
36 .WithTopic("lobby") 57 new TopicFilterBuilder()
Line 37... Line 58...
37 .WithTopic("exchange") 58 .WithTopic("exchange")
38 .Build() 59 .Build()
Line 39... Line 60...
39 ); 60 );
40   61  
-   62 await Client.StartAsync(options);
-   63  
-   64 ClientRunning = true;
-   65 }
-   66  
-   67 private void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
-   68 {
41 await Client.StartAsync(options); 69 if (e.ApplicationMessage.Topic == "lobby")
-   70 {
-   71 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
-   72 {
-   73 memoryStream.Position = 0L;
42   74  
Line 43... Line 75...
43 ClientRunning = true; 75 var lobbyMessage = (LobbyMessage)LobbyMessage.XmlSerializer.Deserialize(memoryStream);
44 } 76  
-   77 UpdateLobbyMessage(lobbyMessage.Nick, lobbyMessage.Message);
-   78 }
Line -... Line 79...
-   79 return;
-   80 }
-   81 }
-   82  
-   83 private void ClientOnConnected(object sender, MqttClientConnectedEventArgs e)
-   84 {
-   85 LogActivity(Properties.Strings.Client_connected);
-   86 }
-   87  
-   88 private void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e)
-   89 {
-   90 LogActivity(Properties.Strings.Client_disconnected, e.Exception.Message);
-   91 }
-   92  
-   93 private void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e)
-   94 {
-   95 LogActivity(Properties.Strings.Client_connection_failed, e.Exception.Message);
-   96 }
-   97  
-   98 private void UpdateLobbyMessage(string client, string message)
-   99 {
-   100 WingManForm.LobbyTextBox.Invoke((MethodInvoker)delegate
-   101 {
-   102 WingManForm.LobbyTextBox.AppendText($"{client} : {message}" + Environment.NewLine);
-   103 });
45   104 }
Line 46... Line 105...
46 private void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) 105  
47 { 106 private void LogActivity(params string[] messages)
48   107 {
Line 72... Line 131...
72 } 131 }
Line 73... Line 132...
73   132  
74 public void BindHandlers() 133 public void BindHandlers()
75 { 134 {
-   135 Client.Connected += ClientOnConnected;
-   136 Client.Disconnected += ClientOnDisconnected;
76 Client.Connected += ClientOnConnected; 137 Client.ConnectingFailed += ClientOnConnectingFailed;
77 Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; 138 Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived;
Line 78... Line 139...
78 } 139 }
79   140  
80 public void UnbindHandlers() 141 public void UnbindHandlers()
-   142 {
-   143 Client.Connected -= ClientOnConnected;
81 { 144 Client.Disconnected -= ClientOnDisconnected;
82 Client.Connected -= ClientOnConnected; 145 Client.ConnectingFailed -= ClientOnConnectingFailed;
-   146 Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived;
-   147 }
-   148  
-   149 public async Task BroadcastLobbyMessage(string text)
-   150 {
-   151 using (var memoryStream = new MemoryStream())
-   152 {
-   153 LobbyMessage.XmlSerializer.Serialize(memoryStream, new LobbyMessage()
-   154 {
-   155 Message = text,
-   156 Nick = Nick
-   157 });
-   158  
-   159 memoryStream.Position = 0L;
-   160  
-   161 await Client.PublishAsync(new ManagedMqttApplicationMessage
-   162 {
-   163 ApplicationMessage = new MqttApplicationMessage
-   164 {
-   165 Payload = memoryStream.ToArray(),
-   166 Topic = "lobby"
-   167 }
-   168 });
-   169  
83 Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; 170 }
84 } 171 }