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 2.7 KiB

6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Buffers;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Exceptions;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Serializer;
  7. namespace MQTTnet.AspNetCore
  8. {
  9. public static class ReaderExtensions
  10. {
  11. public static bool TryDeserialize(this IMqttPacketSerializer serializer, in ReadOnlySequence<byte> input, out MqttBasePacket packet, out SequencePosition consumed, out SequencePosition observed)
  12. {
  13. packet = null;
  14. consumed = input.Start;
  15. observed = input.End;
  16. var copy = input;
  17. if (copy.Length < 2)
  18. {
  19. return false;
  20. }
  21. var fixedheader = copy.First.Span[0];
  22. if (!TryReadBodyLength(ref copy, out var bodyLength))
  23. {
  24. return false;
  25. }
  26. if (copy.Length < bodyLength)
  27. {
  28. return false;
  29. }
  30. var bodySlice = copy.Slice(0, bodyLength);
  31. var buffer = bodySlice.GetArray();
  32. packet = serializer.Deserialize(new ReceivedMqttPacket(fixedheader, new MqttPacketBodyReader(buffer, 0, buffer.Length)));
  33. consumed = bodySlice.End;
  34. observed = bodySlice.End;
  35. return true;
  36. }
  37. private static byte[] GetArray(this in ReadOnlySequence<byte> input)
  38. {
  39. if (input.IsSingleSegment)
  40. {
  41. return input.First.Span.ToArray();
  42. }
  43. // Should be rare
  44. return input.ToArray();
  45. }
  46. private static bool TryReadBodyLength(ref ReadOnlySequence<byte> input, out int result)
  47. {
  48. // Alorithm taken from https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html.
  49. var multiplier = 1;
  50. var value = 0;
  51. byte encodedByte;
  52. var index = 1;
  53. result = 0;
  54. var temp = input.Slice(0, Math.Min(5, input.Length)).GetArray();
  55. do
  56. {
  57. if (index == temp.Length)
  58. {
  59. return false;
  60. }
  61. encodedByte = temp[index];
  62. index++;
  63. value += (byte)(encodedByte & 127) * multiplier;
  64. if (multiplier > 128 * 128 * 128)
  65. {
  66. throw new MqttProtocolViolationException($"Remaining length is invalid (Data={string.Join(",", temp.AsSpan(1, index).ToArray())}).");
  67. }
  68. multiplier *= 128;
  69. } while ((encodedByte & 128) != 0);
  70. input = input.Slice(index);
  71. result = value;
  72. return true;
  73. }
  74. }
  75. }