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.

MqttApplicationMessageBuilder.cs 3.2 KiB

преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 6 години
преди 7 години
преди 6 години
преди 7 години
преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using MQTTnet.Exceptions;
  6. using MQTTnet.Protocol;
  7. namespace MQTTnet
  8. {
  9. public class MqttApplicationMessageBuilder
  10. {
  11. private MqttQualityOfServiceLevel _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  12. private string _topic;
  13. private byte[] _payload;
  14. private bool _retain;
  15. public MqttApplicationMessageBuilder WithTopic(string topic)
  16. {
  17. _topic = topic;
  18. return this;
  19. }
  20. public MqttApplicationMessageBuilder WithPayload(IEnumerable<byte> payload)
  21. {
  22. if (payload == null)
  23. {
  24. _payload = null;
  25. return this;
  26. }
  27. _payload = payload.ToArray();
  28. return this;
  29. }
  30. public MqttApplicationMessageBuilder WithPayload(Stream payload)
  31. {
  32. return WithPayload(payload, payload.Length - payload.Position);
  33. }
  34. public MqttApplicationMessageBuilder WithPayload(Stream payload, long length)
  35. {
  36. if (payload == null)
  37. {
  38. _payload = null;
  39. return this;
  40. }
  41. if (payload.Length == 0)
  42. {
  43. _payload = new byte[0];
  44. }
  45. else
  46. {
  47. _payload = new byte[length];
  48. payload.Read(_payload, 0, _payload.Length);
  49. }
  50. return this;
  51. }
  52. public MqttApplicationMessageBuilder WithPayload(string payload)
  53. {
  54. if (payload == null)
  55. {
  56. _payload = null;
  57. return this;
  58. }
  59. _payload = string.IsNullOrEmpty(payload) ? new byte[0] : Encoding.UTF8.GetBytes(payload);
  60. return this;
  61. }
  62. public MqttApplicationMessageBuilder WithQualityOfServiceLevel(MqttQualityOfServiceLevel qualityOfServiceLevel)
  63. {
  64. _qualityOfServiceLevel = qualityOfServiceLevel;
  65. return this;
  66. }
  67. public MqttApplicationMessageBuilder WithRetainFlag(bool value = true)
  68. {
  69. _retain = value;
  70. return this;
  71. }
  72. public MqttApplicationMessageBuilder WithAtLeastOnceQoS()
  73. {
  74. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce;
  75. return this;
  76. }
  77. public MqttApplicationMessageBuilder WithAtMostOnceQoS()
  78. {
  79. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  80. return this;
  81. }
  82. public MqttApplicationMessageBuilder WithExactlyOnceQoS()
  83. {
  84. _qualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce;
  85. return this;
  86. }
  87. public MqttApplicationMessage Build()
  88. {
  89. if (string.IsNullOrEmpty(_topic))
  90. {
  91. throw new MqttProtocolViolationException("Topic is not set.");
  92. }
  93. return new MqttApplicationMessage
  94. {
  95. Topic = _topic,
  96. Payload = _payload ?? new byte[0],
  97. QualityOfServiceLevel = _qualityOfServiceLevel,
  98. Retain = _retain
  99. };
  100. }
  101. }
  102. }