Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

94 wiersze
2.9 KiB

  1. using System;
  2. using MQTTnet.Client.Options;
  3. using MQTTnet.Server;
  4. namespace MQTTnet.Extensions.ManagedClient
  5. {
  6. public class ManagedMqttClientOptionsBuilder
  7. {
  8. readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions();
  9. MqttClientOptionsBuilder _clientOptionsBuilder;
  10. public ManagedMqttClientOptionsBuilder WithMaxPendingMessages(int value)
  11. {
  12. _options.MaxPendingMessages = value;
  13. return this;
  14. }
  15. public ManagedMqttClientOptionsBuilder WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy value)
  16. {
  17. _options.PendingMessagesOverflowStrategy = value;
  18. return this;
  19. }
  20. public ManagedMqttClientOptionsBuilder WithAutoReconnectDelay(TimeSpan value)
  21. {
  22. _options.AutoReconnectDelay = value;
  23. return this;
  24. }
  25. public ManagedMqttClientOptionsBuilder WithStorage(IManagedMqttClientStorage value)
  26. {
  27. _options.Storage = value;
  28. return this;
  29. }
  30. public ManagedMqttClientOptionsBuilder WithClientOptions(IMqttClientOptions value)
  31. {
  32. if (_clientOptionsBuilder != null)
  33. {
  34. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  35. }
  36. _options.ClientOptions = value ?? throw new ArgumentNullException(nameof(value));
  37. return this;
  38. }
  39. public ManagedMqttClientOptionsBuilder WithClientOptions(MqttClientOptionsBuilder builder)
  40. {
  41. if (_options.ClientOptions != null)
  42. {
  43. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  44. }
  45. _clientOptionsBuilder = builder;
  46. return this;
  47. }
  48. public ManagedMqttClientOptionsBuilder WithClientOptions(Action<MqttClientOptionsBuilder> options)
  49. {
  50. if (options == null) throw new ArgumentNullException(nameof(options));
  51. if (_clientOptionsBuilder == null)
  52. {
  53. _clientOptionsBuilder = new MqttClientOptionsBuilder();
  54. }
  55. options(_clientOptionsBuilder);
  56. return this;
  57. }
  58. public ManagedMqttClientOptionsBuilder WithMaxTopicFiltersInSubscribeUnsubscribePackets(int value)
  59. {
  60. _options.MaxTopicFiltersInSubscribeUnsubscribePackets = value;
  61. return this;
  62. }
  63. public ManagedMqttClientOptions Build()
  64. {
  65. if (_clientOptionsBuilder != null)
  66. {
  67. _options.ClientOptions = _clientOptionsBuilder.Build();
  68. }
  69. if (_options.ClientOptions == null)
  70. {
  71. throw new InvalidOperationException("The ClientOptions cannot be null.");
  72. }
  73. return _options;
  74. }
  75. }
  76. }