Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MqttPacketReader.cs 4.0 KiB

7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
6 anos atrás
7 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 async Task<MqttPacketHeader> ReadHeaderAsync(Stream stream, CancellationToken cancellationToken)
  22. {
  23. if (cancellationToken.IsCancellationRequested)
  24. {
  25. return null;
  26. }
  27. // Wait for the next package which starts with the header. At this point there will probably
  28. // some large delay and thus the thread should be put back to the pool (await). So ReadByte()
  29. // is not an option here.
  30. var buffer = new byte[1];
  31. var readCount = await stream.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);
  32. if (readCount <= 0)
  33. {
  34. return null;
  35. }
  36. var fixedHeader = buffer[0];
  37. var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
  38. var bodyLength = await ReadBodyLengthAsync(stream, cancellationToken).ConfigureAwait(false);
  39. return new MqttPacketHeader
  40. {
  41. FixedHeader = fixedHeader,
  42. ControlPacketType = controlPacketType,
  43. BodyLength = bodyLength
  44. };
  45. }
  46. public override ushort ReadUInt16()
  47. {
  48. var buffer = ReadBytes(2);
  49. var temp = buffer[0];
  50. buffer[0] = buffer[1];
  51. buffer[1] = temp;
  52. return BitConverter.ToUInt16(buffer, 0);
  53. }
  54. public string ReadStringWithLengthPrefix()
  55. {
  56. var buffer = ReadWithLengthPrefix();
  57. if (buffer.Length == 0)
  58. {
  59. return string.Empty;
  60. }
  61. return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  62. }
  63. public byte[] ReadWithLengthPrefix()
  64. {
  65. var length = ReadUInt16();
  66. if (length == 0)
  67. {
  68. return new byte[0];
  69. }
  70. return ReadBytes(length);
  71. }
  72. public byte[] ReadRemainingData()
  73. {
  74. return ReadBytes(_header.BodyLength - (int)BaseStream.Position);
  75. }
  76. private static async Task<int> ReadBodyLengthAsync(Stream stream, CancellationToken cancellationToken)
  77. {
  78. // Alorithm taken from https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html.
  79. var multiplier = 1;
  80. var value = 0;
  81. byte encodedByte;
  82. var buffer = new byte[1];
  83. var readBytes = new List<byte>();
  84. do
  85. {
  86. if (cancellationToken.IsCancellationRequested)
  87. {
  88. throw new TaskCanceledException();
  89. }
  90. var readCount = await stream.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);
  91. if (readCount <= 0)
  92. {
  93. throw new MqttCommunicationException("Connection closed while reading remaining length data.");
  94. }
  95. encodedByte = buffer[0];
  96. readBytes.Add(encodedByte);
  97. value += (byte)(encodedByte & 127) * multiplier;
  98. if (multiplier > 128 * 128 * 128)
  99. {
  100. throw new MqttProtocolViolationException($"Remaining length is invalid (Data={string.Join(",", readBytes)}).");
  101. }
  102. multiplier *= 128;
  103. } while ((encodedByte & 128) != 0);
  104. return value;
  105. }
  106. }
  107. }