WingMan – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
2 | office | 3 | using System.IO; |
1 | office | 4 | using System.Linq; |
5 | using System.Net; |
||
2 | office | 6 | using System.Runtime.Remoting.Messaging; |
1 | office | 7 | using System.Text; |
8 | using System.Threading.Tasks; |
||
2 | office | 9 | using System.Windows.Forms; |
10 | using System.Xml.Serialization; |
||
1 | office | 11 | using MQTTnet; |
12 | using MQTTnet.Client; |
||
13 | using MQTTnet.Extensions.ManagedClient; |
||
14 | |||
15 | namespace WingMan.Communication |
||
16 | { |
||
17 | public class MQTTClient : IDisposable |
||
18 | { |
||
2 | office | 19 | private WingManForm WingManForm { get; set; } |
1 | office | 20 | private IManagedMqttClient Client { get; set; } |
21 | public bool ClientRunning { get; set; } |
||
22 | |||
2 | office | 23 | public string Nick { get; set; } |
24 | |||
1 | office | 25 | public MQTTClient() |
26 | { |
||
27 | Client = new MqttFactory().CreateManagedMqttClient(); |
||
28 | } |
||
29 | |||
2 | office | 30 | public MQTTClient(WingManForm wingManForm) : this() |
1 | office | 31 | { |
2 | office | 32 | WingManForm = wingManForm; |
33 | } |
||
34 | |||
35 | public async Task Start(IPAddress ipAddress, int port, string nick) |
||
36 | { |
||
37 | Nick = nick; |
||
38 | |||
39 | var clientOptions = new MqttClientOptionsBuilder() |
||
40 | .WithTcpServer(ipAddress.ToString(), port); |
||
41 | |||
1 | office | 42 | // Setup and start a managed MQTT client. |
43 | var options = new ManagedMqttClientOptionsBuilder() |
||
44 | .WithAutoReconnectDelay(TimeSpan.FromSeconds(5)) |
||
2 | office | 45 | .WithClientOptions(clientOptions.Build()) |
1 | office | 46 | .Build(); |
2 | office | 47 | |
1 | office | 48 | BindHandlers(); |
49 | |||
50 | await Client.SubscribeAsync( |
||
51 | new TopicFilterBuilder() |
||
52 | .WithTopic("lobby") |
||
53 | .Build() |
||
54 | ); |
||
55 | |||
2 | office | 56 | await Client.SubscribeAsync( |
57 | new TopicFilterBuilder() |
||
58 | .WithTopic("exchange") |
||
59 | .Build() |
||
60 | ); |
||
61 | |||
1 | office | 62 | await Client.StartAsync(options); |
63 | |||
64 | ClientRunning = true; |
||
65 | } |
||
66 | |||
67 | private void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
||
68 | { |
||
2 | office | 69 | if (e.ApplicationMessage.Topic == "lobby") |
70 | { |
||
71 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
||
72 | { |
||
73 | memoryStream.Position = 0L; |
||
1 | office | 74 | |
2 | office | 75 | var lobbyMessage = (LobbyMessage)LobbyMessage.XmlSerializer.Deserialize(memoryStream); |
76 | |||
77 | UpdateLobbyMessage(lobbyMessage.Nick, lobbyMessage.Message); |
||
78 | } |
||
79 | return; |
||
80 | } |
||
1 | office | 81 | } |
82 | |||
83 | private void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
||
84 | { |
||
2 | office | 85 | LogActivity(Properties.Strings.Client_connected); |
86 | } |
||
1 | office | 87 | |
2 | office | 88 | private void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
89 | { |
||
90 | LogActivity(Properties.Strings.Client_disconnected, e.Exception.Message); |
||
1 | office | 91 | } |
92 | |||
2 | office | 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 | }); |
||
104 | } |
||
105 | |||
106 | private void LogActivity(params string[] messages) |
||
107 | { |
||
108 | WingManForm.ActivityTextBox.Invoke((MethodInvoker) delegate |
||
109 | { |
||
110 | WingManForm.ActivityTextBox.Text = |
||
111 | string.Join(" : ", messages) + Environment.NewLine + WingManForm.ActivityTextBox.Text; |
||
112 | }); |
||
113 | } |
||
114 | |||
1 | office | 115 | public async Task Stop() |
116 | { |
||
117 | UnbindHandlers(); |
||
118 | |||
119 | await Client.StopAsync(); |
||
120 | |||
121 | ClientRunning = false; |
||
122 | } |
||
123 | |||
124 | public void Dispose() |
||
125 | { |
||
126 | UnbindHandlers(); |
||
127 | |||
128 | Client.StopAsync().Wait(); |
||
129 | |||
130 | Client?.Dispose(); |
||
131 | } |
||
132 | |||
133 | public void BindHandlers() |
||
134 | { |
||
135 | Client.Connected += ClientOnConnected; |
||
2 | office | 136 | Client.Disconnected += ClientOnDisconnected; |
137 | Client.ConnectingFailed += ClientOnConnectingFailed; |
||
1 | office | 138 | Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; |
139 | } |
||
140 | |||
141 | public void UnbindHandlers() |
||
142 | { |
||
143 | Client.Connected -= ClientOnConnected; |
||
2 | office | 144 | Client.Disconnected -= ClientOnDisconnected; |
145 | Client.ConnectingFailed -= ClientOnConnectingFailed; |
||
1 | office | 146 | Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
147 | } |
||
2 | office | 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 | |||
170 | } |
||
171 | } |
||
1 | office | 172 | } |
173 | } |