No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MqttFactory.cs 3.9 KiB

hace 7 años
hace 7 años
hace 7 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 MQTTnet.Implementations;
  8. using MQTTnet.Core.ManagedClient;
  9. using MQTTnet.Core.Server;
  10. using System.Linq;
  11. using MQTTnet.Core.Channel;
  12. namespace MQTTnet
  13. {
  14. public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory
  15. {
  16. private readonly IServiceProvider _serviceProvider;
  17. private static IServiceProvider BuildServiceProvider()
  18. {
  19. var serviceProvider = new ServiceCollection()
  20. .AddMqttClient()
  21. .AddMqttServer()
  22. .AddLogging()
  23. .BuildServiceProvider();
  24. serviceProvider.GetRequiredService<ILoggerFactory>()
  25. .AddMqttTrace();
  26. return serviceProvider;
  27. }
  28. public MqttFactory()
  29. : this(BuildServiceProvider())
  30. {
  31. }
  32. public MqttFactory(IServiceProvider serviceProvider)
  33. {
  34. _serviceProvider = serviceProvider;
  35. }
  36. public IMqttCommunicationAdapter CreateClientMqttCommunicationAdapter(IMqttClientOptions options)
  37. {
  38. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  39. return new MqttChannelCommunicationAdapter(CreateMqttCommunicationChannel(options), CreateSerializer(options.ProtocolVersion), logger);
  40. }
  41. public IMqttCommunicationAdapter CreateServerMqttCommunicationAdapter(IMqttCommunicationChannel channel)
  42. {
  43. var serializer = _serviceProvider.GetRequiredService<IMqttPacketSerializer>();
  44. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  45. return new MqttChannelCommunicationAdapter(channel, serializer, logger);
  46. }
  47. public IMqttCommunicationChannel CreateMqttCommunicationChannel(IMqttClientOptions options)
  48. {
  49. if (options == null) throw new ArgumentNullException(nameof(options));
  50. switch (options)
  51. {
  52. case MqttClientTcpOptions tcpOptions:
  53. return CreateTcpChannel(tcpOptions);
  54. case MqttClientWebSocketOptions webSocketOptions:
  55. return CreateWebSocketChannel(webSocketOptions);
  56. default:
  57. throw new NotSupportedException();
  58. }
  59. }
  60. public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions)
  61. {
  62. return new MqttTcpChannel(tcpOptions);
  63. }
  64. public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions)
  65. {
  66. return new MqttWebSocketChannel(webSocketOptions);
  67. }
  68. public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion)
  69. {
  70. return new MqttPacketSerializer()
  71. {
  72. ProtocolVersion = protocolVersion
  73. };
  74. }
  75. public MqttClientSession CreateClientSession(string sessionId, MqttClientSessionsManager mqttClientSessionsManager)
  76. {
  77. return new MqttClientSession(sessionId, mqttClientSessionsManager, _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(), _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>());
  78. }
  79. public IMqttClient CreateMqttClient()
  80. {
  81. return _serviceProvider.GetRequiredService<IMqttClient>();
  82. }
  83. public ManagedMqttClient CreateManagedMqttClient()
  84. {
  85. return _serviceProvider.GetRequiredService<ManagedMqttClient>();
  86. }
  87. public IMqttServer CreateMqttServer()
  88. {
  89. return _serviceProvider.GetRequiredService<IMqttServer>();
  90. }
  91. }
  92. }