MqttV500PropertiesReader.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using MQTTnet.Exceptions;
  4. using MQTTnet.Packets;
  5. using MQTTnet.Protocol;
  6. namespace MQTTnet.Formatter.V5
  7. {
  8. public class MqttV500PropertiesReader
  9. {
  10. private readonly IMqttPacketBodyReader _body;
  11. private readonly int _length;
  12. private readonly int _targetOffset;
  13. public MqttV500PropertiesReader(IMqttPacketBodyReader body)
  14. {
  15. _body = body ?? throw new ArgumentNullException(nameof(body));
  16. if (!body.EndOfStream)
  17. {
  18. _length = (int)body.ReadVariableLengthInteger();
  19. }
  20. _targetOffset = body.Offset + _length;
  21. }
  22. public MqttPropertyId CurrentPropertyId { get; private set; }
  23. public bool MoveNext()
  24. {
  25. if (_length == 0)
  26. {
  27. return false;
  28. }
  29. if (_body.Offset >= _targetOffset)
  30. {
  31. return false;
  32. }
  33. CurrentPropertyId = (MqttPropertyId)_body.ReadByte();
  34. return true;
  35. }
  36. public void AddUserPropertyTo(List<MqttUserProperty> userProperties)
  37. {
  38. if (userProperties == null) throw new ArgumentNullException(nameof(userProperties));
  39. var name = _body.ReadStringWithLengthPrefix();
  40. var value = _body.ReadStringWithLengthPrefix();
  41. userProperties.Add(new MqttUserProperty(name, value));
  42. }
  43. public string ReadReasonString()
  44. {
  45. return _body.ReadStringWithLengthPrefix();
  46. }
  47. public string ReadAuthenticationMethod()
  48. {
  49. return _body.ReadStringWithLengthPrefix();
  50. }
  51. public byte[] ReadAuthenticationData()
  52. {
  53. return _body.ReadWithLengthPrefix();
  54. }
  55. public bool ReadRetainAvailable()
  56. {
  57. return _body.ReadBoolean();
  58. }
  59. public uint ReadSessionExpiryInterval()
  60. {
  61. return _body.ReadFourByteInteger();
  62. }
  63. public ushort ReadReceiveMaximum()
  64. {
  65. return _body.ReadTwoByteInteger();
  66. }
  67. public string ReadAssignedClientIdentifier()
  68. {
  69. return _body.ReadStringWithLengthPrefix();
  70. }
  71. public string ReadServerReference()
  72. {
  73. return _body.ReadStringWithLengthPrefix();
  74. }
  75. public ushort ReadTopicAliasMaximum()
  76. {
  77. return _body.ReadTwoByteInteger();
  78. }
  79. public uint ReadMaximumPacketSize()
  80. {
  81. return _body.ReadFourByteInteger();
  82. }
  83. public ushort ReadServerKeepAlive()
  84. {
  85. return _body.ReadTwoByteInteger();
  86. }
  87. public string ReadResponseInformation()
  88. {
  89. return _body.ReadStringWithLengthPrefix();
  90. }
  91. public bool ReadSharedSubscriptionAvailable()
  92. {
  93. return _body.ReadBoolean();
  94. }
  95. public bool ReadSubscriptionIdentifiersAvailable()
  96. {
  97. return _body.ReadBoolean();
  98. }
  99. public bool ReadWildcardSubscriptionAvailable()
  100. {
  101. return _body.ReadBoolean();
  102. }
  103. public uint ReadSubscriptionIdentifier()
  104. {
  105. return _body.ReadVariableLengthInteger();
  106. }
  107. public MqttPayloadFormatIndicator? ReadPayloadFormatIndicator()
  108. {
  109. return (MqttPayloadFormatIndicator)_body.ReadByte();
  110. }
  111. public uint ReadMessageExpiryInterval()
  112. {
  113. return _body.ReadFourByteInteger();
  114. }
  115. public ushort ReadTopicAlias()
  116. {
  117. return _body.ReadTwoByteInteger();
  118. }
  119. public string ReadResponseTopic()
  120. {
  121. return _body.ReadStringWithLengthPrefix();
  122. }
  123. public byte[] ReadCorrelationData()
  124. {
  125. return _body.ReadWithLengthPrefix();
  126. }
  127. public string ReadContentType()
  128. {
  129. return _body.ReadStringWithLengthPrefix();
  130. }
  131. public uint ReadWillDelayInterval()
  132. {
  133. return _body.ReadFourByteInteger();
  134. }
  135. public bool RequestResponseInformation()
  136. {
  137. return _body.ReadBoolean();
  138. }
  139. public bool RequestProblemInformation()
  140. {
  141. return _body.ReadBoolean();
  142. }
  143. public void ThrowInvalidPropertyIdException(Type type)
  144. {
  145. throw new MqttProtocolViolationException($"Property ID '{CurrentPropertyId}' is not supported for package type '{type.Name}'.");
  146. }
  147. }
  148. }