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 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using MQTTnet.Client;
  3. using MQTTnet.Server;
  4. namespace MQTTnet.Extensions.ManagedClient
  5. {
  6. public class ManagedMqttClientOptionsBuilder
  7. {
  8. private readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions();
  9. private 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 ManagedMqttClientOptions Build()
  59. {
  60. if (_clientOptionsBuilder != null)
  61. {
  62. _options.ClientOptions = _clientOptionsBuilder.Build();
  63. }
  64. if (_options.ClientOptions == null)
  65. {
  66. throw new InvalidOperationException("The ClientOptions cannot be null.");
  67. }
  68. return _options;
  69. }
  70. }
  71. }