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.
 
 
 
 

98 lines
3.1 KiB

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