WingMan – Diff between revs 12 and 14
?pathlinks?
Rev 12 | Rev 14 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.IO; |
2 | using System.IO; |
|
3 | using System.Net; |
3 | using System.Net; |
|
4 | using System.Text; |
- | ||
5 | using System.Threading; |
4 | using System.Threading; |
|
6 | using System.Threading.Tasks; |
5 | using System.Threading.Tasks; |
|
7 | using MQTTnet; |
6 | using MQTTnet; |
|
8 | using MQTTnet.Client; |
7 | using MQTTnet.Client; |
|
9 | using MQTTnet.Extensions.ManagedClient; |
8 | using MQTTnet.Extensions.ManagedClient; |
|
10 | using MQTTnet.Protocol; |
9 | using MQTTnet.Protocol; |
|
11 | using MQTTnet.Server; |
10 | using MQTTnet.Server; |
|
12 | using WingMan.Utilities; |
11 | using WingMan.Utilities; |
|
13 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
12 | using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs; |
|
14 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
13 | using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs; |
|
15 | |
14 | |
|
16 | namespace WingMan.Communication |
15 | namespace WingMan.Communication |
|
17 | { |
16 | { |
|
18 | public class MqttCommunication : IDisposable |
17 | public class MqttCommunication : IDisposable |
|
19 | { |
18 | { |
|
20 | public delegate void ClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
19 | public delegate void ClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
|
21 | |
20 | |
|
22 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
21 | public delegate void ClientConnected(object sender, MqttClientConnectedEventArgs e); |
|
23 | |
22 | |
|
24 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
23 | public delegate void ClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e); |
|
25 | |
24 | |
|
26 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
25 | public delegate void ClientDisconnected(object sender, MqttClientDisconnectedEventArgs e); |
|
27 | |
26 | |
|
28 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
27 | public delegate void ClientSubscribed(object sender, MqttClientSubscribedTopicEventArgs e); |
|
29 | |
28 | |
|
30 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
29 | public delegate void ClientUnsubscribed(object sender, MqttClientUnsubscribedTopicEventArgs e); |
|
31 | |
30 | |
|
32 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
31 | public delegate void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e); |
|
33 | |
32 | |
|
34 | public delegate void ServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
33 | public delegate void ServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e); |
|
35 | |
34 | |
|
36 | public delegate void ServerClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e); |
35 | public delegate void ServerClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e); |
|
37 | |
36 | |
|
38 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
37 | public delegate void ServerClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e); |
|
39 | |
38 | |
|
40 | public delegate void ServerStarted(object sender, EventArgs e); |
39 | public delegate void ServerStarted(object sender, EventArgs e); |
|
41 | |
40 | |
|
42 | public delegate void ServerStopped(object sender, EventArgs e); |
41 | public delegate void ServerStopped(object sender, EventArgs e); |
|
43 | |
42 | |
|
44 | public MqttCommunication(TaskScheduler taskScheduler, CancellationToken cancellationToken) |
43 | public MqttCommunication(TaskScheduler taskScheduler, CancellationToken cancellationToken) |
|
45 | { |
44 | { |
|
46 | TaskScheduler = taskScheduler; |
45 | TaskScheduler = taskScheduler; |
|
47 | CancellationToken = cancellationToken; |
46 | CancellationToken = cancellationToken; |
|
48 | |
47 | |
|
49 | Client = new MqttFactory().CreateManagedMqttClient(); |
48 | Client = new MqttFactory().CreateManagedMqttClient(); |
|
50 | Server = new MqttFactory().CreateMqttServer(); |
49 | Server = new MqttFactory().CreateMqttServer(); |
|
51 | } |
50 | } |
|
52 | |
51 | |
|
53 | private TaskScheduler TaskScheduler { get; } |
52 | private TaskScheduler TaskScheduler { get; } |
|
54 | |
53 | |
|
55 | private IManagedMqttClient Client { get; } |
54 | private IManagedMqttClient Client { get; } |
|
56 | |
55 | |
|
57 | private IMqttServer Server { get; } |
56 | private IMqttServer Server { get; } |
|
58 | |
57 | |
|
59 | public bool Running { get; set; } |
58 | public bool Running { get; set; } |
|
60 | |
59 | |
|
61 | public string Nick { get; set; } |
60 | public string Nick { get; set; } |
|
62 | |
61 | |
|
63 | private IPAddress IpAddress { get; set; } |
62 | private IPAddress IpAddress { get; set; } |
|
64 | |
63 | |
|
65 | private int Port { get; set; } |
64 | private int Port { get; set; } |
|
66 | |
65 | |
|
67 | private string Password { get; set; } |
66 | private string Password { get; set; } |
|
68 | |
67 | |
|
69 | private CancellationToken CancellationToken { get; } |
68 | private CancellationToken CancellationToken { get; } |
|
70 | |
69 | |
|
71 | public MqttCommunicationType Type { get; set; } |
70 | public MqttCommunicationType Type { get; set; } |
|
72 | |
71 | |
|
73 | public async void Dispose() |
72 | public async void Dispose() |
|
74 | { |
73 | { |
|
75 | await Stop(); |
74 | await Stop(); |
|
76 | } |
75 | } |
|
77 | |
76 | |
|
78 | public event ClientAuthenticationFailed OnClientAuthenticationFailed; |
77 | public event ClientAuthenticationFailed OnClientAuthenticationFailed; |
|
79 | |
78 | |
|
80 | public event ServerAuthenticationFailed OnServerAuthenticationFailed; |
79 | public event ServerAuthenticationFailed OnServerAuthenticationFailed; |
|
81 | |
80 | |
|
82 | public event MessageReceived OnMessageReceived; |
81 | public event MessageReceived OnMessageReceived; |
|
83 | |
82 | |
|
84 | public event ClientConnected OnClientConnected; |
83 | public event ClientConnected OnClientConnected; |
|
85 | |
84 | |
|
86 | public event ClientDisconnected OnClientDisconnected; |
85 | public event ClientDisconnected OnClientDisconnected; |
|
87 | |
86 | |
|
88 | public event ClientConnectionFailed OnClientConnectionFailed; |
87 | public event ClientConnectionFailed OnClientConnectionFailed; |
|
89 | |
88 | |
|
90 | public event ClientUnsubscribed OnClientUnsubscribed; |
89 | public event ClientUnsubscribed OnClientUnsubscribed; |
|
91 | |
90 | |
|
92 | public event ClientSubscribed OnClientSubscribed; |
91 | public event ClientSubscribed OnClientSubscribed; |
|
93 | |
92 | |
|
94 | public event ServerClientDisconnected OnServerClientDisconnected; |
93 | public event ServerClientDisconnected OnServerClientDisconnected; |
|
95 | |
94 | |
|
96 | public event ServerClientConnected OnServerClientConnected; |
95 | public event ServerClientConnected OnServerClientConnected; |
|
97 | |
96 | |
|
98 | public event ServerStarted OnServerStarted; |
97 | public event ServerStarted OnServerStarted; |
|
99 | |
98 | |
|
100 | public event ServerStopped OnServerStopped; |
99 | public event ServerStopped OnServerStopped; |
|
101 | |
100 | |
|
102 | public async Task<bool> Start(MqttCommunicationType type, IPAddress ipAddress, int port, string nick, |
101 | public async Task<bool> Start(MqttCommunicationType type, IPAddress ipAddress, int port, string nick, |
|
103 | string password) |
102 | string password) |
|
104 | { |
103 | { |
|
105 | Type = type; |
104 | Type = type; |
|
106 | IpAddress = ipAddress; |
105 | IpAddress = ipAddress; |
|
107 | Port = port; |
106 | Port = port; |
|
108 | Nick = nick; |
107 | Nick = nick; |
|
109 | Password = password; |
108 | Password = password; |
|
110 | |
109 | |
|
111 | switch (type) |
110 | switch (type) |
|
112 | { |
111 | { |
|
113 | case MqttCommunicationType.Client: |
112 | case MqttCommunicationType.Client: |
|
114 | return await StartClient(); |
113 | return await StartClient(); |
|
115 | case MqttCommunicationType.Server: |
114 | case MqttCommunicationType.Server: |
|
116 | return await StartServer(); |
115 | return await StartServer(); |
|
117 | } |
116 | } |
|
118 | |
117 | |
|
119 | return false; |
118 | return false; |
|
120 | } |
119 | } |
|
121 | |
120 | |
|
122 | private async Task<bool> StartClient() |
121 | private async Task<bool> StartClient() |
|
123 | { |
122 | { |
|
124 | var clientOptions = new MqttClientOptionsBuilder() |
123 | var clientOptions = new MqttClientOptionsBuilder() |
|
125 | .WithTcpServer(IpAddress.ToString(), Port); |
124 | .WithTcpServer(IpAddress.ToString(), Port); |
|
126 | |
125 | |
|
127 | // Setup and start a managed MQTT client. |
126 | // Setup and start a managed MQTT client. |
|
128 | var options = new ManagedMqttClientOptionsBuilder() |
127 | var options = new ManagedMqttClientOptionsBuilder() |
|
129 | .WithClientOptions(clientOptions.Build()) |
128 | .WithClientOptions(clientOptions.Build()) |
|
130 | .Build(); |
129 | .Build(); |
|
131 | |
130 | |
|
132 | BindClientHandlers(); |
131 | BindClientHandlers(); |
|
133 | |
132 | |
|
134 | await Client.SubscribeAsync( |
133 | await Client.SubscribeAsync( |
|
135 | new TopicFilterBuilder() |
134 | new TopicFilterBuilder() |
|
136 | .WithTopic("lobby") |
135 | .WithTopic("lobby") |
|
137 | .Build() |
136 | .Build() |
|
138 | ); |
137 | ); |
|
139 | |
138 | |
|
140 | await Client.SubscribeAsync( |
139 | await Client.SubscribeAsync( |
|
141 | new TopicFilterBuilder() |
140 | new TopicFilterBuilder() |
|
142 | .WithTopic("exchange") |
141 | .WithTopic("exchange") |
|
143 | .Build() |
142 | .Build() |
|
144 | ); |
143 | ); |
|
145 | |
144 | |
|
146 | await Client.SubscribeAsync( |
145 | await Client.SubscribeAsync( |
|
147 | new TopicFilterBuilder() |
146 | new TopicFilterBuilder() |
|
148 | .WithTopic("execute") |
147 | .WithTopic("execute") |
|
149 | .Build() |
148 | .Build() |
|
150 | ); |
149 | ); |
|
151 | |
150 | |
|
152 | await Client.StartAsync(options); |
151 | await Client.StartAsync(options); |
|
153 | |
152 | |
|
154 | Running = true; |
153 | Running = true; |
|
155 | |
154 | |
|
156 | return Running; |
155 | return Running; |
|
157 | } |
156 | } |
|
158 | |
157 | |
|
159 | private async Task StopClient() |
158 | private async Task StopClient() |
|
160 | { |
159 | { |
|
161 | UnbindClientHandlers(); |
160 | UnbindClientHandlers(); |
|
162 | |
161 | |
|
163 | await Client.StopAsync(); |
162 | await Client.StopAsync(); |
|
164 | } |
163 | } |
|
165 | |
164 | |
|
166 | public void BindClientHandlers() |
165 | public void BindClientHandlers() |
|
167 | { |
166 | { |
|
168 | Client.Connected += ClientOnConnected; |
167 | Client.Connected += ClientOnConnected; |
|
169 | Client.Disconnected += ClientOnDisconnected; |
168 | Client.Disconnected += ClientOnDisconnected; |
|
170 | Client.ConnectingFailed += ClientOnConnectingFailed; |
169 | Client.ConnectingFailed += ClientOnConnectingFailed; |
|
171 | Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; |
170 | Client.ApplicationMessageReceived += ClientOnApplicationMessageReceived; |
|
172 | } |
171 | } |
|
173 | |
172 | |
|
174 | public void UnbindClientHandlers() |
173 | public void UnbindClientHandlers() |
|
175 | { |
174 | { |
|
176 | Client.Connected -= ClientOnConnected; |
175 | Client.Connected -= ClientOnConnected; |
|
177 | Client.Disconnected -= ClientOnDisconnected; |
176 | Client.Disconnected -= ClientOnDisconnected; |
|
178 | Client.ConnectingFailed -= ClientOnConnectingFailed; |
177 | Client.ConnectingFailed -= ClientOnConnectingFailed; |
|
179 | Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
178 | Client.ApplicationMessageReceived -= ClientOnApplicationMessageReceived; |
|
180 | } |
179 | } |
|
181 | |
180 | |
|
182 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
181 | private async void ClientOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
183 | { |
182 | { |
|
184 | try |
183 | try |
|
185 | { |
184 | { |
|
186 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
185 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
|
187 | |
186 | |
|
188 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
187 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
189 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
188 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
190 | } |
189 | } |
|
191 | catch (Exception ex) |
190 | catch (Exception ex) |
|
192 | { |
191 | { |
|
193 | await Task.Delay(0, CancellationToken).ContinueWith( |
192 | await Task.Delay(0, CancellationToken).ContinueWith( |
|
194 | _ => OnClientAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
193 | _ => OnClientAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
|
195 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
194 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
196 | } |
195 | } |
|
197 | } |
196 | } |
|
198 | |
197 | |
|
199 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
198 | private async void ClientOnConnectingFailed(object sender, MqttManagedProcessFailedEventArgs e) |
|
200 | { |
199 | { |
|
201 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
200 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnectionFailed?.Invoke(sender, e), |
|
202 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
201 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
203 | } |
202 | } |
|
204 | |
203 | |
|
205 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
204 | private async void ClientOnDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
|
206 | { |
205 | { |
|
207 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
206 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientDisconnected?.Invoke(sender, e), |
|
208 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
207 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
209 | } |
208 | } |
|
210 | |
209 | |
|
211 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
210 | private async void ClientOnConnected(object sender, MqttClientConnectedEventArgs e) |
|
212 | { |
211 | { |
|
213 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
212 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientConnected?.Invoke(sender, e), |
|
214 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
213 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
215 | } |
214 | } |
|
216 | |
215 | |
|
217 | private async Task<bool> StartServer() |
216 | private async Task<bool> StartServer() |
|
218 | { |
217 | { |
|
219 | var optionsBuilder = new MqttServerOptionsBuilder() |
218 | var optionsBuilder = new MqttServerOptionsBuilder() |
|
220 | .WithDefaultEndpointBoundIPAddress(IpAddress) |
219 | .WithDefaultEndpointBoundIPAddress(IpAddress) |
|
221 | .WithDefaultEndpointPort(Port); |
220 | .WithDefaultEndpointPort(Port); |
|
222 | |
221 | |
|
223 | BindServerHandlers(); |
222 | BindServerHandlers(); |
|
224 | |
223 | |
|
225 | try |
224 | try |
|
226 | { |
225 | { |
|
227 | await Server.StartAsync(optionsBuilder.Build()); |
226 | await Server.StartAsync(optionsBuilder.Build()); |
|
228 | |
227 | |
|
229 | Running = true; |
228 | Running = true; |
|
230 | } |
229 | } |
|
231 | catch (Exception) |
230 | catch (Exception) |
|
232 | { |
231 | { |
|
233 | Running = false; |
232 | Running = false; |
|
234 | } |
233 | } |
|
235 | |
234 | |
|
236 | return Running; |
235 | return Running; |
|
237 | } |
236 | } |
|
238 | |
237 | |
|
239 | private async Task StopServer() |
238 | private async Task StopServer() |
|
240 | { |
239 | { |
|
241 | UnbindServerHandlers(); |
240 | UnbindServerHandlers(); |
|
242 | |
241 | |
|
243 | await Server.StopAsync(); |
242 | await Server.StopAsync(); |
|
244 | } |
243 | } |
|
245 | |
244 | |
|
246 | private void BindServerHandlers() |
245 | private void BindServerHandlers() |
|
247 | { |
246 | { |
|
248 | Server.Started += ServerOnStarted; |
247 | Server.Started += ServerOnStarted; |
|
249 | Server.Stopped += ServerOnStopped; |
248 | Server.Stopped += ServerOnStopped; |
|
250 | Server.ClientConnected += ServerOnClientConnected; |
249 | Server.ClientConnected += ServerOnClientConnected; |
|
251 | Server.ClientDisconnected += ServerOnClientDisconnected; |
250 | Server.ClientDisconnected += ServerOnClientDisconnected; |
|
252 | Server.ClientSubscribedTopic += ServerOnClientSubscribedTopic; |
251 | Server.ClientSubscribedTopic += ServerOnClientSubscribedTopic; |
|
253 | Server.ClientUnsubscribedTopic += ServerOnClientUnsubscribedTopic; |
252 | Server.ClientUnsubscribedTopic += ServerOnClientUnsubscribedTopic; |
|
254 | Server.ApplicationMessageReceived += ServerOnApplicationMessageReceived; |
253 | Server.ApplicationMessageReceived += ServerOnApplicationMessageReceived; |
|
255 | } |
254 | } |
|
256 | |
255 | |
|
257 | private void UnbindServerHandlers() |
256 | private void UnbindServerHandlers() |
|
258 | { |
257 | { |
|
259 | Server.Started -= ServerOnStarted; |
258 | Server.Started -= ServerOnStarted; |
|
260 | Server.Stopped -= ServerOnStopped; |
259 | Server.Stopped -= ServerOnStopped; |
|
261 | Server.ClientConnected -= ServerOnClientConnected; |
260 | Server.ClientConnected -= ServerOnClientConnected; |
|
262 | Server.ClientDisconnected -= ServerOnClientDisconnected; |
261 | Server.ClientDisconnected -= ServerOnClientDisconnected; |
|
263 | Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; |
262 | Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; |
|
264 | Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; |
263 | Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; |
|
265 | Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
264 | Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
|
266 | } |
265 | } |
|
267 | |
266 | |
|
268 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
267 | private async void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
269 | { |
268 | { |
|
270 | try |
269 | try |
|
271 | { |
270 | { |
|
272 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
271 | e.ApplicationMessage.Payload = await AES.Decrypt(e.ApplicationMessage.Payload, Password); |
|
273 | |
272 | |
|
274 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
273 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnMessageReceived?.Invoke(sender, e), |
|
275 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
274 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
276 | } |
275 | } |
|
277 | catch (Exception ex) |
276 | catch (Exception ex) |
|
278 | { |
277 | { |
|
279 | foreach (var clientSessionStatus in await Server.GetClientSessionsStatusAsync()) |
278 | foreach (var clientSessionStatus in await Server.GetClientSessionsStatusAsync()) |
|
280 | { |
279 | { |
|
281 | if (!string.Equals(clientSessionStatus.ClientId, e.ClientId, StringComparison.Ordinal)) |
280 | if (!string.Equals(clientSessionStatus.ClientId, e.ClientId, StringComparison.Ordinal)) |
|
282 | continue; |
281 | continue; |
|
283 | |
282 | |
|
284 | await clientSessionStatus.DisconnectAsync(); |
283 | await clientSessionStatus.DisconnectAsync(); |
|
285 | } |
284 | } |
|
286 | |
285 | |
|
287 | await Task.Delay(0, CancellationToken).ContinueWith( |
286 | await Task.Delay(0, CancellationToken).ContinueWith( |
|
288 | _ => OnServerAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
287 | _ => OnServerAuthenticationFailed?.Invoke(sender, new MqttAuthenticationFailureEventArgs(e, ex)), |
|
289 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
288 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
290 | } |
289 | } |
|
291 | } |
290 | } |
|
292 | |
291 | |
|
293 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
292 | private async void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
|
294 | { |
293 | { |
|
295 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
294 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientUnsubscribed?.Invoke(sender, e), |
|
296 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
295 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
297 | } |
296 | } |
|
298 | |
297 | |
|
299 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
298 | private async void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
|
300 | { |
299 | { |
|
301 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
300 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnClientSubscribed?.Invoke(sender, e), |
|
302 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
301 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
303 | } |
302 | } |
|
304 | |
303 | |
|
305 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
304 | private async void ServerOnClientDisconnected(object sender, MQTTnet.Server.MqttClientDisconnectedEventArgs e) |
|
306 | { |
305 | { |
|
307 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
306 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientDisconnected?.Invoke(sender, e), |
|
308 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
307 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
309 | } |
308 | } |
|
310 | |
309 | |
|
311 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
310 | private async void ServerOnClientConnected(object sender, MQTTnet.Server.MqttClientConnectedEventArgs e) |
|
312 | { |
311 | { |
|
313 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
312 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerClientConnected?.Invoke(sender, e), |
|
314 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
313 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
315 | } |
314 | } |
|
316 | |
315 | |
|
317 | private async void ServerOnStopped(object sender, EventArgs e) |
316 | private async void ServerOnStopped(object sender, EventArgs e) |
|
318 | { |
317 | { |
|
319 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStopped?.Invoke(sender, e), |
318 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStopped?.Invoke(sender, e), |
|
320 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
319 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
321 | } |
320 | } |
|
322 | |
321 | |
|
323 | private async void ServerOnStarted(object sender, EventArgs e) |
322 | private async void ServerOnStarted(object sender, EventArgs e) |
|
324 | { |
323 | { |
|
325 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStarted?.Invoke(sender, e), |
324 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnServerStarted?.Invoke(sender, e), |
|
326 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
325 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
|
327 | } |
326 | } |
|
328 | |
327 | |
|
329 | public async Task Stop() |
328 | public async Task Stop() |
|
330 | { |
329 | { |
|
331 | switch (Type) |
330 | switch (Type) |
|
332 | { |
331 | { |
|
333 | case MqttCommunicationType.Server: |
332 | case MqttCommunicationType.Server: |
|
334 | await StopServer(); |
333 | await StopServer(); |
|
335 | break; |
334 | break; |
|
336 | case MqttCommunicationType.Client: |
335 | case MqttCommunicationType.Client: |
|
337 | await StopClient(); |
336 | await StopClient(); |
|
338 | break; |
337 | break; |
|
339 | } |
338 | } |
|
340 | |
339 | |
|
341 | Running = false; |
340 | Running = false; |
|
342 | } |
341 | } |
|
343 | |
342 | |
|
344 | public async Task Broadcast(string topic, byte[] payload) |
343 | public async Task Broadcast(string topic, byte[] payload) |
|
345 | { |
344 | { |
|
346 | using (var payloadStream = new MemoryStream(await AES.Encrypt(payload, Password))) |
345 | using (var payloadStream = new MemoryStream(await AES.Encrypt(payload, Password))) |
|
347 | { |
346 | { |
|
348 | switch (Type) |
347 | switch (Type) |
|
349 | { |
348 | { |
|
350 | case MqttCommunicationType.Client: |
349 | case MqttCommunicationType.Client: |
|
351 | await Client.PublishAsync(new [] |
350 | await Client.PublishAsync(new[] |
|
352 | { |
351 | { |
|
353 | new MqttApplicationMessage |
352 | new MqttApplicationMessage |
|
354 | { |
353 | { |
|
355 | Topic = topic, |
354 | Topic = topic, |
|
356 | Payload = payloadStream.ToArray(), |
355 | Payload = payloadStream.ToArray(), |
|
357 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
356 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
|
358 | } |
357 | } |
|
359 | }); |
358 | }); |
|
360 | break; |
359 | break; |
|
361 | case MqttCommunicationType.Server: |
360 | case MqttCommunicationType.Server: |
|
362 | await Server.PublishAsync(new[] |
361 | await Server.PublishAsync(new[] |
|
363 | { |
362 | { |
|
364 | new MqttApplicationMessage |
363 | new MqttApplicationMessage |
|
365 | { |
364 | { |
|
366 | Topic = topic, |
365 | Topic = topic, |
|
367 | Payload = payloadStream.ToArray(), |
366 | Payload = payloadStream.ToArray(), |
|
368 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
367 | QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce |
|
369 | } |
368 | } |
|
370 | }); |
369 | }); |
|
371 | break; |
370 | break; |
|
372 | } |
371 | } |
|
373 | } |
372 | } |
|
374 | } |
373 | } |
|
375 | } |
374 | } |
|
376 | } |
375 | } |
|
377 | |
376 | |