Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

132 Zeilen
4.7 KiB

  1. using System;
  2. using MQTTnet.Core.Adapter;
  3. using MQTTnet.Core.Client;
  4. using MQTTnet.Core.Serializer;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Options;
  8. using MQTTnet.Implementations;
  9. using MQTTnet.Core.ManagedClient;
  10. using MQTTnet.Core.Server;
  11. using MQTTnet.Core.Channel;
  12. namespace MQTTnet
  13. {
  14. public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory
  15. {
  16. private readonly IServiceProvider _serviceProvider;
  17. public MqttFactory()
  18. : this(BuildServiceProvider())
  19. {
  20. }
  21. public MqttFactory(IServiceProvider serviceProvider)
  22. {
  23. _serviceProvider = serviceProvider;
  24. }
  25. public ILoggerFactory GetLoggerFactory()
  26. {
  27. return _serviceProvider.GetRequiredService<ILoggerFactory>();
  28. }
  29. public IMqttCommunicationAdapter CreateClientCommunicationAdapter(IMqttClientOptions options)
  30. {
  31. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  32. return new MqttChannelCommunicationAdapter(CreateCommunicationChannel(options.ChannelOptions), CreateSerializer(options.ProtocolVersion), logger);
  33. }
  34. public IMqttCommunicationAdapter CreateServerCommunicationAdapter(IMqttCommunicationChannel channel)
  35. {
  36. var serializer = _serviceProvider.GetRequiredService<IMqttPacketSerializer>();
  37. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  38. return new MqttChannelCommunicationAdapter(channel, serializer, logger);
  39. }
  40. public IMqttCommunicationChannel CreateCommunicationChannel(IMqttClientChannelOptions options)
  41. {
  42. if (options == null) throw new ArgumentNullException(nameof(options));
  43. switch (options)
  44. {
  45. case MqttClientTcpOptions tcpOptions:
  46. return CreateTcpChannel(tcpOptions);
  47. case MqttClientWebSocketOptions webSocketOptions:
  48. return CreateWebSocketChannel(webSocketOptions);
  49. default:
  50. throw new NotSupportedException();
  51. }
  52. }
  53. public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions)
  54. {
  55. return new MqttTcpChannel(tcpOptions);
  56. }
  57. public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions)
  58. {
  59. return new MqttWebSocketChannel(webSocketOptions);
  60. }
  61. public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion)
  62. {
  63. return new MqttPacketSerializer
  64. {
  65. ProtocolVersion = protocolVersion
  66. };
  67. }
  68. public MqttClientSession CreateClientSession(string clientId, MqttClientSessionsManager clientSessionsManager)
  69. {
  70. return new MqttClientSession(
  71. clientId,
  72. _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>(),
  73. clientSessionsManager,
  74. _serviceProvider.GetRequiredService<MqttClientSubscriptionsManager>(),
  75. _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(),
  76. _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>(),
  77. _serviceProvider.GetRequiredService<IMqttClientRetainedMessageManager>()
  78. );
  79. }
  80. public IMqttClient CreateMqttClient()
  81. {
  82. return _serviceProvider.GetRequiredService<IMqttClient>();
  83. }
  84. public IManagedMqttClient CreateManagedMqttClient()
  85. {
  86. return _serviceProvider.GetRequiredService<IManagedMqttClient>();
  87. }
  88. public IMqttServer CreateMqttServer()
  89. {
  90. return _serviceProvider.GetRequiredService<IMqttServer>();
  91. }
  92. public IMqttServer CreateMqttServer(Action<MqttServerOptions> configure)
  93. {
  94. if (configure == null) throw new ArgumentNullException(nameof(configure));
  95. var options = _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>();
  96. configure(options.Value);
  97. return _serviceProvider.GetRequiredService<IMqttServer>();
  98. }
  99. private static IServiceProvider BuildServiceProvider()
  100. {
  101. var serviceProvider = new ServiceCollection()
  102. .AddMqttClient()
  103. .AddMqttServer()
  104. .AddLogging()
  105. .BuildServiceProvider();
  106. serviceProvider.GetRequiredService<ILoggerFactory>()
  107. .AddMqttTrace();
  108. return serviceProvider;
  109. }
  110. }
  111. }