Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

94 righe
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Diagnostics;
  7. using MQTTnet.Core.Internal;
  8. namespace MQTTnet.Core.Server
  9. {
  10. public sealed class MqttServer : IMqttServer
  11. {
  12. private readonly MqttClientSessionsManager _clientSessionsManager;
  13. private readonly ICollection<IMqttServerAdapter> _adapters;
  14. private readonly MqttServerOptions _options;
  15. private CancellationTokenSource _cancellationTokenSource;
  16. public MqttServer(MqttServerOptions options, ICollection<IMqttServerAdapter> adapters)
  17. {
  18. _options = options ?? throw new ArgumentNullException(nameof(options));
  19. _adapters = adapters ?? throw new ArgumentNullException(nameof(adapters));
  20. _clientSessionsManager = new MqttClientSessionsManager(options);
  21. _clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e);
  22. }
  23. public IList<ConnectedMqttClient> GetConnectedClients()
  24. {
  25. return _clientSessionsManager.GetConnectedClients();
  26. }
  27. public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
  28. public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
  29. public void Publish(MqttApplicationMessage applicationMessage)
  30. {
  31. if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
  32. _clientSessionsManager.DispatchPublishPacket(null, applicationMessage.ToPublishPacket());
  33. }
  34. public void InjectClient(string identifier, IMqttCommunicationAdapter adapter)
  35. {
  36. if (adapter == null) throw new ArgumentNullException(nameof(adapter));
  37. if (_cancellationTokenSource == null) throw new InvalidOperationException("The MQTT server is not started.");
  38. OnClientConnected(this, new MqttClientConnectedEventArgs(identifier, adapter));
  39. }
  40. public void Start()
  41. {
  42. if (_cancellationTokenSource != null) throw new InvalidOperationException("The MQTT server is already started.");
  43. _cancellationTokenSource = new CancellationTokenSource();
  44. foreach (var adapter in _adapters)
  45. {
  46. adapter.ClientConnected += OnClientConnected;
  47. adapter.Start(_options);
  48. }
  49. MqttTrace.Information(nameof(MqttServer), "Started.");
  50. }
  51. public void Stop()
  52. {
  53. _cancellationTokenSource?.Cancel(false);
  54. _cancellationTokenSource?.Dispose();
  55. _cancellationTokenSource = null;
  56. foreach (var adapter in _adapters)
  57. {
  58. adapter.ClientConnected -= OnClientConnected;
  59. adapter.Stop();
  60. }
  61. _clientSessionsManager.Clear();
  62. MqttTrace.Information(nameof(MqttServer), "Stopped.");
  63. }
  64. private void OnClientConnected(object sender, MqttClientConnectedEventArgs eventArgs)
  65. {
  66. MqttTrace.Information(nameof(MqttServer), $"Client '{eventArgs.Identifier}': Connected.");
  67. ClientConnected?.Invoke(this, eventArgs);
  68. Task.Run(() => _clientSessionsManager.RunClientSessionAsync(eventArgs), _cancellationTokenSource.Token);
  69. }
  70. }
  71. }