Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

119 wiersze
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Exceptions;
  8. using MQTTnet.Packets;
  9. using MQTTnet.Protocol;
  10. namespace MQTTnet.Serializer
  11. {
  12. public sealed class MqttPacketReader : BinaryReader
  13. {
  14. private readonly MqttPacketHeader _header;
  15. public MqttPacketReader(MqttPacketHeader header, Stream bodyStream)
  16. : base(bodyStream, Encoding.UTF8, true)
  17. {
  18. _header = header;
  19. }
  20. public bool EndOfRemainingData => BaseStream.Position == _header.BodyLength;
  21. public static MqttPacketHeader ReadHeaderFromSource(Stream stream, CancellationToken cancellationToken)
  22. {
  23. var buffer = stream.ReadByte();
  24. if (buffer == -1)
  25. {
  26. return null;
  27. }
  28. var fixedHeader = (byte)buffer;
  29. var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
  30. var bodyLength = ReadBodyLengthFromSource(stream, cancellationToken);
  31. return new MqttPacketHeader
  32. {
  33. FixedHeader = fixedHeader,
  34. ControlPacketType = controlPacketType,
  35. BodyLength = bodyLength
  36. };
  37. }
  38. public override ushort ReadUInt16()
  39. {
  40. var buffer = ReadBytes(2);
  41. var temp = buffer[0];
  42. buffer[0] = buffer[1];
  43. buffer[1] = temp;
  44. return BitConverter.ToUInt16(buffer, 0);
  45. }
  46. public string ReadStringWithLengthPrefix()
  47. {
  48. var buffer = ReadWithLengthPrefix();
  49. if (buffer.Length == 0)
  50. {
  51. return string.Empty;
  52. }
  53. return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  54. }
  55. public byte[] ReadWithLengthPrefix()
  56. {
  57. var length = ReadUInt16();
  58. if (length == 0)
  59. {
  60. return new byte[0];
  61. }
  62. return ReadBytes(length);
  63. }
  64. public byte[] ReadRemainingData()
  65. {
  66. return ReadBytes(_header.BodyLength - (int)BaseStream.Position);
  67. }
  68. private static int ReadBodyLengthFromSource(Stream stream, CancellationToken cancellationToken)
  69. {
  70. // Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html.
  71. var multiplier = 1;
  72. var value = 0;
  73. byte encodedByte;
  74. var readBytes = new List<byte>();
  75. do
  76. {
  77. if (cancellationToken.IsCancellationRequested)
  78. {
  79. throw new TaskCanceledException();
  80. }
  81. var buffer = stream.ReadByte();
  82. if (buffer == -1)
  83. {
  84. throw new MqttCommunicationException("Connection closed while reading remaining length data.");
  85. }
  86. encodedByte = (byte)buffer;
  87. readBytes.Add(encodedByte);
  88. value += (byte)(encodedByte & 127) * multiplier;
  89. multiplier *= 128;
  90. if (multiplier > 128 * 128 * 128)
  91. {
  92. throw new MqttProtocolViolationException($"Remaining length is invalid (Data={string.Join(",", readBytes)}).");
  93. }
  94. } while ((encodedByte & 128) != 0);
  95. return value;
  96. }
  97. }
  98. }