-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MqttClientSessionsManager.cs
644 lines (543 loc) · 25.5 KB
/
MqttClientSessionsManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Adapter;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Diagnostics;
using MQTTnet.Diagnostics.Logger;
using MQTTnet.Exceptions;
using MQTTnet.Formatter;
using MQTTnet.Internal;
using MQTTnet.Packets;
using MQTTnet.Protocol;
using MQTTnet.Server.Status;
namespace MQTTnet.Server.Internal
{
public sealed class MqttClientSessionsManager : IDisposable
{
readonly BlockingCollection<MqttPendingApplicationMessage> _messageQueue =
new BlockingCollection<MqttPendingApplicationMessage>();
readonly AsyncLock _createConnectionSyncRoot = new AsyncLock();
readonly Dictionary<string, MqttClientConnection> _clientConnections =
new Dictionary<string, MqttClientConnection>(4096);
readonly Dictionary<string, MqttClientSession>
_clientSessions = new Dictionary<string, MqttClientSession>(4096);
readonly IDictionary<object, object> _serverSessionItems = new ConcurrentDictionary<object, object>();
readonly MqttServerEventDispatcher _eventDispatcher;
readonly IMqttRetainedMessagesManager _retainedMessagesManager;
readonly IMqttServerOptions _options;
readonly MqttNetSourceLogger _logger;
readonly IMqttNetLogger _rootLogger;
public MqttClientSessionsManager(
IMqttServerOptions options,
IMqttRetainedMessagesManager retainedMessagesManager,
MqttServerEventDispatcher eventDispatcher,
IMqttNetLogger logger)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger.WithSource(nameof(MqttClientSessionsManager));
_rootLogger = logger;
_eventDispatcher = eventDispatcher ?? throw new ArgumentNullException(nameof(eventDispatcher));
_options = options ?? throw new ArgumentNullException(nameof(options));
_retainedMessagesManager = retainedMessagesManager ??
throw new ArgumentNullException(nameof(retainedMessagesManager));
}
public void Start(CancellationToken cancellationToken)
{
Task.Run(() => TryProcessQueuedApplicationMessagesAsync(cancellationToken), cancellationToken)
.RunInBackground(_logger);
}
async Task<MqttConnectPacket> ReceiveConnectPacket(IMqttChannelAdapter channelAdapter,
CancellationToken cancellationToken)
{
try
{
using (var timeoutToken = new CancellationTokenSource(_options.DefaultCommunicationTimeout))
using (var effectiveCancellationToken =
CancellationTokenSource.CreateLinkedTokenSource(timeoutToken.Token, cancellationToken))
{
var firstPacket = await channelAdapter.ReceivePacketAsync(effectiveCancellationToken.Token)
.ConfigureAwait(false);
if (firstPacket is MqttConnectPacket connectPacket)
{
return connectPacket;
}
}
}
catch (OperationCanceledException)
{
_logger.Warning("Client '{0}': Connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
}
catch (MqttCommunicationTimedOutException)
{
_logger.Warning("Client '{0}': Connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
}
_logger.Warning("Client '{0}': First received packet was no 'CONNECT' packet [MQTT-3.1.0-1].",
channelAdapter.Endpoint);
return null;
}
public async Task HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter,
CancellationToken cancellationToken)
{
MqttClientConnection clientConnection = null;
try
{
var connectPacket = await ReceiveConnectPacket(channelAdapter, cancellationToken).ConfigureAwait(false);
if (connectPacket == null)
{
// Nothing was received in time etc.
return;
}
MqttConnAckPacket connAckPacket;
var connectionValidatorContext =
await ValidateConnection(connectPacket, channelAdapter).ConfigureAwait(false);
if (connectionValidatorContext.ReasonCode != MqttConnectReasonCode.Success)
{
// Send failure response here without preparing a session!
connAckPacket =
channelAdapter.PacketFormatterAdapter.DataConverter.CreateConnAckPacket(
connectionValidatorContext);
await channelAdapter.SendPacketAsync(connAckPacket, cancellationToken).ConfigureAwait(false);
return;
}
connAckPacket =
channelAdapter.PacketFormatterAdapter.DataConverter.CreateConnAckPacket(connectionValidatorContext);
// Pass connAckPacket so that IsSessionPresent flag can be set if the client session already exists
clientConnection = await CreateClientConnection(connectPacket, connAckPacket, channelAdapter,
connectionValidatorContext).ConfigureAwait(false);
await channelAdapter.SendPacketAsync(connAckPacket, cancellationToken).ConfigureAwait(false);
await _eventDispatcher.SafeNotifyClientConnectedAsync(connectPacket, channelAdapter)
.ConfigureAwait(false);
await clientConnection.RunAsync().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
_logger.Error(exception, exception.Message);
}
finally
{
if (clientConnection != null)
{
if (clientConnection.ClientId != null)
{
// in case it is a takeover _clientConnections already contains the new connection
if (!clientConnection.IsTakenOver)
{
lock (_clientConnections)
{
_clientConnections.Remove(clientConnection.ClientId);
}
if ((!_options.EnablePersistentSessions) || (!clientConnection.Session.IsPersistent))
{
await DeleteSessionAsync(clientConnection.ClientId).ConfigureAwait(false);
}
}
}
var endpoint = clientConnection.Endpoint;
if (clientConnection.ClientId != null && !clientConnection.IsTakenOver)
{
// The event is fired at a separate place in case of a handover!
await _eventDispatcher.SafeNotifyClientDisconnectedAsync(
clientConnection.ClientId,
clientConnection.IsCleanDisconnect
? MqttClientDisconnectType.Clean
: MqttClientDisconnectType.NotClean,
endpoint).ConfigureAwait(false);
}
}
await channelAdapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None)
.ConfigureAwait(false);
}
}
public async Task CloseAllConnectionsAsync()
{
List<MqttClientConnection> connections;
lock (_clientConnections)
{
connections = _clientConnections.Values.ToList();
_clientConnections.Clear();
}
foreach (var connection in connections)
{
await connection.StopAsync(MqttClientDisconnectReason.NormalDisconnection).ConfigureAwait(false);
}
}
public List<MqttClientConnection> GetConnections()
{
lock (_clientConnections)
{
return _clientConnections.Values.ToList();
}
}
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
{
var result = new List<IMqttClientStatus>();
lock (_clientConnections)
{
foreach (var connection in _clientConnections.Values)
{
var clientStatus = new MqttClientStatus(connection);
connection.FillClientStatus(clientStatus);
var sessionStatus = new MqttSessionStatus(connection.Session, this);
connection.Session.FillSessionStatus(sessionStatus);
clientStatus.Session = sessionStatus;
result.Add(clientStatus);
}
}
return Task.FromResult((IList<IMqttClientStatus>) result);
}
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
{
var result = new List<IMqttSessionStatus>();
lock (_clientSessions)
{
foreach (var session in _clientSessions.Values)
{
var sessionStatus = new MqttSessionStatus(session, this);
session.FillSessionStatus(sessionStatus);
result.Add(sessionStatus);
}
}
return Task.FromResult((IList<IMqttSessionStatus>) result);
}
public void DispatchApplicationMessage(MqttApplicationMessage applicationMessage, MqttClientConnection sender)
{
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
_messageQueue.Add(new MqttPendingApplicationMessage(applicationMessage, sender));
}
public async Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
{
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
if (topicFilters is null) throw new ArgumentNullException(nameof(topicFilters));
var fakeSubscribePacket = new MqttSubscribePacket
{
TopicFilters = topicFilters.ToList()
};
var clientSession = GetClientSession(clientId);
var subscribeResult = await clientSession.SubscriptionsManager.Subscribe(fakeSubscribePacket)
.ConfigureAwait(false);
foreach (var retainedApplicationMessage in subscribeResult.RetainedApplicationMessages)
{
clientSession.ApplicationMessagesQueue.Enqueue(retainedApplicationMessage);
}
}
public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
{
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
var fakeUnsubscribePacket = new MqttUnsubscribePacket
{
TopicFilters = topicFilters.ToList()
};
return GetClientSession(clientId).SubscriptionsManager.Unsubscribe(fakeUnsubscribePacket);
}
public async Task DeleteSessionAsync(string clientId)
{
MqttClientConnection connection;
MqttClientSession session;
lock (_clientConnections)
{
_clientConnections.TryGetValue(clientId, out connection);
}
lock (_clientSessions)
{
_clientSessions.TryGetValue(clientId, out session);
_clientSessions.Remove(clientId);
}
if (connection != null)
{
await connection.StopAsync(MqttClientDisconnectReason.NormalDisconnection).ConfigureAwait(false);
}
session?.Dispose();
_logger.Verbose("Session for client '{0}' deleted.", clientId);
}
public void Dispose()
{
_messageQueue?.Dispose();
}
async Task TryProcessQueuedApplicationMessagesAsync(CancellationToken cancellationToken)
{
// Make sure all queued messages are proccessed befor server stops.
while (!cancellationToken.IsCancellationRequested || _messageQueue.Any())
{
try
{
await TryProcessNextQueuedApplicationMessage().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
_logger.Error(exception, "Unhandled exception while processing queued application messages.");
}
}
}
async Task TryProcessNextQueuedApplicationMessage()
{
try
{
MqttPendingApplicationMessage pendingApplicationMessage;
try
{
pendingApplicationMessage = _messageQueue.Take();
}
catch (ArgumentNullException)
{
return;
}
catch (ObjectDisposedException)
{
return;
}
var clientConnection = pendingApplicationMessage.Sender;
var senderClientId = clientConnection?.ClientId ?? _options.ClientId;
var applicationMessage = pendingApplicationMessage.ApplicationMessage;
var interceptor = _options.ApplicationMessageInterceptor;
if (interceptor != null)
{
var interceptorContext =
await InterceptApplicationMessageAsync(interceptor, clientConnection, applicationMessage)
.ConfigureAwait(false);
if (interceptorContext != null)
{
if (interceptorContext.CloseConnection)
{
if (clientConnection != null)
{
await clientConnection.StopAsync(MqttClientDisconnectReason.NormalDisconnection)
.ConfigureAwait(false);
}
}
if (interceptorContext.ApplicationMessage == null || !interceptorContext.AcceptPublish)
{
return;
}
applicationMessage = interceptorContext.ApplicationMessage;
}
}
await _eventDispatcher.SafeNotifyApplicationMessageReceivedAsync(senderClientId, applicationMessage)
.ConfigureAwait(false);
if (applicationMessage.Retain)
{
await _retainedMessagesManager.HandleMessageAsync(senderClientId, applicationMessage)
.ConfigureAwait(false);
}
var deliveryCount = 0;
List<MqttClientSession> sessions;
lock (_clientSessions)
{
sessions = _clientSessions.Values.ToList();
}
foreach (var clientSession in sessions)
{
var checkSubscriptionsResult = clientSession.SubscriptionsManager.CheckSubscriptions(
applicationMessage.Topic,
applicationMessage.QualityOfServiceLevel,
senderClientId);
if (!checkSubscriptionsResult.IsSubscribed)
{
continue;
}
_logger.Verbose("Client '{0}': Queued application message with topic '{1}'.",
clientSession.ClientId, applicationMessage.Topic);
var queuedApplicationMessage = new MqttQueuedApplicationMessage
{
ApplicationMessage = applicationMessage,
SubscriptionQualityOfServiceLevel = checkSubscriptionsResult.QualityOfServiceLevel,
SubscriptionIdentifiers = checkSubscriptionsResult.SubscriptionIdentifiers
};
if (checkSubscriptionsResult.RetainAsPublished)
{
// Transfer the original retain state from the publisher.
// This is a MQTTv5 feature.
queuedApplicationMessage.IsRetainedMessage = applicationMessage.Retain;
}
clientSession.ApplicationMessagesQueue.Enqueue(queuedApplicationMessage);
deliveryCount++;
}
if (deliveryCount == 0)
{
var undeliveredMessageInterceptor = _options.UndeliveredMessageInterceptor;
if (undeliveredMessageInterceptor == null)
{
return;
}
// The delegate signature is the same as for regular message interceptor. So the call is fine and just uses a different interceptor.
await InterceptApplicationMessageAsync(undeliveredMessageInterceptor, clientConnection,
applicationMessage).ConfigureAwait(false);
}
}
catch (Exception exception)
{
_logger.Error(exception, "Unhandled exception while processing next queued application message.");
}
}
async Task<MqttConnectionValidatorContext> ValidateConnection(MqttConnectPacket connectPacket,
IMqttChannelAdapter channelAdapter)
{
var context = new MqttConnectionValidatorContext(connectPacket, channelAdapter)
{
SessionItems = new ConcurrentDictionary<object, object>()
};
var connectionValidator = _options.ConnectionValidator;
if (connectionValidator == null)
{
context.ReasonCode = MqttConnectReasonCode.Success;
return context;
}
await connectionValidator.ValidateConnectionAsync(context).ConfigureAwait(false);
// Check the client ID and set a random one if supported.
if (string.IsNullOrEmpty(connectPacket.ClientId) &&
channelAdapter.PacketFormatterAdapter.ProtocolVersion == MqttProtocolVersion.V500)
{
connectPacket.ClientId = context.AssignedClientIdentifier;
}
if (string.IsNullOrEmpty(connectPacket.ClientId))
{
context.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
}
return context;
}
async Task<MqttClientConnection> CreateClientConnection(
MqttConnectPacket connectPacket,
MqttConnAckPacket connAckPacket,
IMqttChannelAdapter channelAdapter,
MqttConnectionValidatorContext context)
{
MqttClientConnection connection;
bool sessionShouldPersist;
if (context.ProtocolVersion == MqttProtocolVersion.V500)
{
// MQTT 5.0 section 3.1.2.11.2
// The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0 [MQTT-3.1.2-23].
//
// A Client that only wants to process messages while connected will set the Clean Start to 1 and set the Session Expiry Interval to 0.
// It will not receive Application Messages published before it connected and has to subscribe afresh to any topics that it is interested
// in each time it connects.
// Persist if SessionExpiryInterval != 0, but may start with a clean session
sessionShouldPersist = context.SessionExpiryInterval != 0;
}
else
{
// MQTT 3.1.1 section 3.1.2.4: persist only if 'not CleanSession'
//
// If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one.
// This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be
// reused in any subsequent Session [MQTT-3.1.2-6].
sessionShouldPersist = !connectPacket.CleanSession;
}
using (await _createConnectionSyncRoot.WaitAsync(CancellationToken.None).ConfigureAwait(false))
{
MqttClientSession session;
lock (_clientSessions)
{
if (!_clientSessions.TryGetValue(connectPacket.ClientId, out session))
{
_logger.Verbose("Created a new session for client '{0}'.", connectPacket.ClientId);
session = CreateSession(connectPacket.ClientId, context.SessionItems, sessionShouldPersist);
}
else
{
if (connectPacket.CleanSession)
{
_logger.Verbose("Deleting existing session of client '{0}'.", connectPacket.ClientId);
session = CreateSession(connectPacket.ClientId, context.SessionItems, sessionShouldPersist);
}
else
{
_logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
connAckPacket.IsSessionPresent = true;
}
}
_clientSessions[connectPacket.ClientId] = session;
}
MqttClientConnection existingConnection;
lock (_clientConnections)
{
_clientConnections.TryGetValue(connectPacket.ClientId, out existingConnection);
connection = CreateConnection(connectPacket, channelAdapter, session);
_clientConnections[connectPacket.ClientId] = connection;
}
if (existingConnection != null)
{
await _eventDispatcher.SafeNotifyClientDisconnectedAsync(existingConnection.ClientId,
MqttClientDisconnectType.Takeover, existingConnection.Endpoint);
existingConnection.IsTakenOver = true;
await existingConnection.StopAsync(MqttClientDisconnectReason.SessionTakenOver)
.ConfigureAwait(false);
}
}
return connection;
}
async Task<MqttApplicationMessageInterceptorContext> InterceptApplicationMessageAsync(
IMqttServerApplicationMessageInterceptor interceptor,
MqttClientConnection clientConnection,
MqttApplicationMessage applicationMessage)
{
string senderClientId;
IDictionary<object, object> sessionItems;
var messageIsFromServer = clientConnection == null;
if (messageIsFromServer)
{
senderClientId = _options.ClientId;
sessionItems = _serverSessionItems;
}
else
{
senderClientId = clientConnection.ClientId;
sessionItems = clientConnection.Session.Items;
}
var interceptorContext = new MqttApplicationMessageInterceptorContext
{
ClientId = senderClientId,
SessionItems = sessionItems,
AcceptPublish = true,
ApplicationMessage = applicationMessage,
CloseConnection = false
};
await interceptor.InterceptApplicationMessagePublishAsync(interceptorContext).ConfigureAwait(false);
return interceptorContext;
}
MqttClientSession GetClientSession(string clientId)
{
lock (_clientSessions)
{
if (!_clientSessions.TryGetValue(clientId, out var session))
{
throw new InvalidOperationException($"Client session '{clientId}' is unknown.");
}
return session;
}
}
MqttClientConnection CreateConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter,
MqttClientSession session)
{
return new MqttClientConnection(
connectPacket,
channelAdapter,
session,
_options,
this,
_rootLogger);
}
MqttClientSession CreateSession(string clientId, IDictionary<object, object> sessionItems, bool isPersistent)
{
return new MqttClientSession(
clientId,
sessionItems,
_eventDispatcher,
_options,
_retainedMessagesManager,
isPersistent
);
}
}
}