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.

ManagedMqttClientOptionsBuilder.cs 2.7 KiB

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