Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

99 righe
3.1 KiB

  1. using JTActiveSafety.Protocol.Buffers;
  2. using System;
  3. using System.Buffers;
  4. using System.Buffers.Binary;
  5. using System.Text;
  6. namespace JTActiveSafety.Protocol.MessagePack
  7. {
  8. ref struct JTActiveSafetyMessagePackWriter
  9. {
  10. private JTActiveSafetyBufferWriter writer;
  11. public JTActiveSafetyMessagePackWriter(Span<byte> buffer)
  12. {
  13. this.writer = new JTActiveSafetyBufferWriter(buffer);
  14. }
  15. public byte[] FlushAndGetArray()
  16. {
  17. return writer.Written.ToArray();
  18. }
  19. public void WriteByte(byte value)
  20. {
  21. var span = writer.Free;
  22. span[0] = value;
  23. writer.Advance(1);
  24. }
  25. public void WriteUInt16(ushort value)
  26. {
  27. BinaryPrimitives.WriteUInt16BigEndian(writer.Free, value);
  28. writer.Advance(2);
  29. }
  30. public void WriteInt32(int value)
  31. {
  32. BinaryPrimitives.WriteInt32BigEndian(writer.Free, value);
  33. writer.Advance(4);
  34. }
  35. public void WriteUInt64(ulong value)
  36. {
  37. BinaryPrimitives.WriteUInt64BigEndian(writer.Free, value);
  38. writer.Advance(8);
  39. }
  40. public void WriteUInt32(uint value)
  41. {
  42. BinaryPrimitives.WriteUInt32BigEndian(writer.Free, value);
  43. writer.Advance(4);
  44. }
  45. public void WriteString(string value)
  46. {
  47. byte[] codeBytes = Encoding.UTF8.GetBytes(value);
  48. codeBytes.CopyTo(writer.Free);
  49. writer.Advance(codeBytes.Length);
  50. }
  51. public void WriteArray(ReadOnlySpan<byte> src)
  52. {
  53. src.CopyTo(writer.Free);
  54. writer.Advance(src.Length);
  55. }
  56. public void WriteBCD(string value, int len)
  57. {
  58. string bcdText = value ?? "";
  59. int startIndex = 0;
  60. int noOfZero = len - bcdText.Length;
  61. if (noOfZero > 0)
  62. {
  63. bcdText = bcdText.Insert(startIndex, new string('0', noOfZero));
  64. }
  65. int byteIndex = 0;
  66. int count = len / 2;
  67. var bcdSpan = bcdText.AsSpan();
  68. var spanFree = writer.Free;
  69. while (startIndex < bcdText.Length && byteIndex < count)
  70. {
  71. spanFree[byteIndex++] = Convert.ToByte(bcdSpan.Slice(startIndex, 2).ToString(), 16);
  72. startIndex += 2;
  73. }
  74. writer.Advance(byteIndex);
  75. }
  76. /// <summary>
  77. /// 数字编码 大端模式、高位在前
  78. /// </summary>
  79. /// <param name="value"></param>
  80. /// <param name="len"></param>
  81. public void WriteBigNumber(string value, int len)
  82. {
  83. var spanFree = writer.Free;
  84. ulong number = string.IsNullOrEmpty(value) ? 0 : (ulong)double.Parse(value);
  85. for (int i = len - 1; i >= 0; i--)
  86. {
  87. spanFree[i] = (byte)(number & 0xFF); //取低8位
  88. number = number >> 8;
  89. }
  90. writer.Advance(len);
  91. }
  92. public int GetCurrentPosition()
  93. {
  94. return writer.WrittenCount;
  95. }
  96. }
  97. }