Não pode escolher mais do que 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 3.4 KiB

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