You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReaderExtensions.cs 3.3 KiB

6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
6 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Buffers;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Exceptions;
  5. using MQTTnet.Formatter;
  6. using MQTTnet.Packets;
  7. namespace MQTTnet.AspNetCore
  8. {
  9. public static class ReaderExtensions
  10. {
  11. public static bool TryDecode(this MqttPacketFormatterAdapter formatter,
  12. SpanBasedMqttPacketBodyReader reader,
  13. in ReadOnlySequence<byte> input,
  14. out MqttBasePacket packet,
  15. out SequencePosition consumed,
  16. out SequencePosition observed,
  17. out int bytesRead)
  18. {
  19. if (formatter == null) throw new ArgumentNullException(nameof(formatter));
  20. packet = null;
  21. consumed = input.Start;
  22. observed = input.End;
  23. bytesRead = 0;
  24. var copy = input;
  25. if (copy.Length < 2)
  26. {
  27. return false;
  28. }
  29. var fixedheader = copy.First.Span[0];
  30. if (!TryReadBodyLength(ref copy, out int headerLength, out var bodyLength))
  31. {
  32. return false;
  33. }
  34. if (copy.Length < bodyLength)
  35. {
  36. return false;
  37. }
  38. var bodySlice = copy.Slice(0, bodyLength);
  39. var buffer = bodySlice.GetMemory();
  40. reader.SetBuffer(buffer);
  41. var receivedMqttPacket = new ReceivedMqttPacket(fixedheader, reader, buffer.Length + 2);
  42. if (formatter.ProtocolVersion == MqttProtocolVersion.Unknown)
  43. {
  44. formatter.DetectProtocolVersion(receivedMqttPacket);
  45. }
  46. packet = formatter.Decode(receivedMqttPacket);
  47. consumed = bodySlice.End;
  48. observed = bodySlice.End;
  49. bytesRead = headerLength + bodyLength;
  50. return true;
  51. }
  52. private static ReadOnlyMemory<byte> GetMemory(this in ReadOnlySequence<byte> input)
  53. {
  54. if (input.IsSingleSegment)
  55. {
  56. return input.First;
  57. }
  58. // Should be rare
  59. return input.ToArray();
  60. }
  61. private static bool TryReadBodyLength(ref ReadOnlySequence<byte> input, out int headerLength, out int bodyLength)
  62. {
  63. // Alorithm taken from https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html.
  64. var multiplier = 1;
  65. var value = 0;
  66. byte encodedByte;
  67. var index = 1;
  68. headerLength = 0;
  69. bodyLength = 0;
  70. var temp = input.Slice(0, Math.Min(5, input.Length)).GetMemory();
  71. do
  72. {
  73. if (index == temp.Length)
  74. {
  75. return false;
  76. }
  77. encodedByte = temp.Span[index];
  78. index++;
  79. value += (byte)(encodedByte & 127) * multiplier;
  80. if (multiplier > 128 * 128 * 128)
  81. {
  82. throw new MqttProtocolViolationException($"Remaining length is invalid (Data={string.Join(",", temp.Slice(1, index).ToArray())}).");
  83. }
  84. multiplier *= 128;
  85. } while ((encodedByte & 128) != 0);
  86. input = input.Slice(index);
  87. headerLength = index;
  88. bodyLength = value;
  89. return true;
  90. }
  91. }
  92. }