Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

104 rindas
3.2 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.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 fixedHeader = (byte)stream.ReadByte();
  25. var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
  26. var bodyLength = ReadBodyLengthFromSource(stream, cancellationToken);
  27. return new MqttPacketHeader
  28. {
  29. FixedHeader = fixedHeader,
  30. ControlPacketType = controlPacketType,
  31. BodyLength = bodyLength
  32. };
  33. }
  34. public override ushort ReadUInt16()
  35. {
  36. var buffer = ReadBytes(2);
  37. var temp = buffer[0];
  38. buffer[0] = buffer[1];
  39. buffer[1] = temp;
  40. return BitConverter.ToUInt16(buffer, 0);
  41. }
  42. public string ReadStringWithLengthPrefix()
  43. {
  44. var buffer = ReadWithLengthPrefix();
  45. return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  46. }
  47. public byte[] ReadWithLengthPrefix()
  48. {
  49. var length = ReadUInt16();
  50. return ReadBytes(length);
  51. }
  52. public byte[] ReadRemainingData()
  53. {
  54. return ReadBytes(_receivedMqttPacket.Header.BodyLength - (int)BaseStream.Position);
  55. }
  56. private static int ReadBodyLengthFromSource(Stream stream, CancellationToken cancellationToken)
  57. {
  58. // Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html.
  59. var multiplier = 1;
  60. var value = 0;
  61. byte encodedByte;
  62. var readBytes = new List<int>();
  63. do
  64. {
  65. if (cancellationToken.IsCancellationRequested)
  66. {
  67. throw new TaskCanceledException();
  68. }
  69. var buffer = stream.ReadByte();
  70. readBytes.Add(buffer);
  71. ////if (buffer == -1)
  72. ////{
  73. //// break;
  74. ////}
  75. encodedByte = (byte)buffer;
  76. value += (byte)(encodedByte & 127) * multiplier;
  77. multiplier *= 128;
  78. if (multiplier > 128 * 128 * 128)
  79. {
  80. throw new MqttProtocolViolationException($"Remaining length is invalid (Data={string.Join(",", readBytes)}).");
  81. }
  82. } while ((encodedByte & 128) != 0);
  83. return value;
  84. }
  85. }
  86. }