You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

59 lines
1.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Client;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Implementations;
  7. using MQTTnet.ManagedClient;
  8. using MQTTnet.Server;
  9. namespace MQTTnet
  10. {
  11. public class MqttFactory : IMqttClientFactory, IMqttServerFactory
  12. {
  13. public IMqttClient CreateMqttClient()
  14. {
  15. return CreateMqttClient(new MqttNetLogger());
  16. }
  17. public IMqttClient CreateMqttClient(IMqttNetLogger logger)
  18. {
  19. if (logger == null) throw new ArgumentNullException(nameof(logger));
  20. return new MqttClient(new MqttClientAdapterFactory(), logger);
  21. }
  22. public IManagedMqttClient CreateManagedMqttClient()
  23. {
  24. return new ManagedMqttClient(CreateMqttClient(), new MqttNetLogger().CreateChildLogger());
  25. }
  26. public IManagedMqttClient CreateManagedMqttClient(IMqttNetLogger logger)
  27. {
  28. if (logger == null) throw new ArgumentNullException(nameof(logger));
  29. return new ManagedMqttClient(CreateMqttClient(), logger.CreateChildLogger());
  30. }
  31. public IMqttServer CreateMqttServer()
  32. {
  33. var logger = new MqttNetLogger();
  34. return CreateMqttServer(logger);
  35. }
  36. public IMqttServer CreateMqttServer(IMqttNetLogger logger)
  37. {
  38. if (logger == null) throw new ArgumentNullException(nameof(logger));
  39. return CreateMqttServer(new List<IMqttServerAdapter> { new MqttTcpServerAdapter(logger.CreateChildLogger()) }, logger);
  40. }
  41. public IMqttServer CreateMqttServer(IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger)
  42. {
  43. if (adapters == null) throw new ArgumentNullException(nameof(adapters));
  44. if (logger == null) throw new ArgumentNullException(nameof(logger));
  45. return new MqttServer(adapters, logger.CreateChildLogger());
  46. }
  47. }
  48. }