DMDataRequest.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace InABox.DigitalMatter
  6. {
  7. public class DMDataRequest : DMMessage
  8. {
  9. public List<DMRecord> _records = new();
  10. public override byte Type => 0x04;
  11. public DMRecord[] Records
  12. {
  13. get => _records.ToArray();
  14. set => _records = value.ToList();
  15. }
  16. protected override void DoDecode()
  17. {
  18. _records.Clear();
  19. ushort bytestaken = 0;
  20. while (bytestaken < CheckSum)
  21. {
  22. var length = PeekUInt16(0);
  23. var record = new DMRecord();
  24. var buf = TakeBytes(length);
  25. bytestaken += length;
  26. record.Decode(buf);
  27. _records.Add(record);
  28. }
  29. }
  30. protected override void DoEncode()
  31. {
  32. foreach (var record in _records)
  33. AddBytes(record.Encode());
  34. InsertUInt16(0, BufferSize);
  35. }
  36. public override byte[] Dump()
  37. {
  38. return Encoding.UTF8.GetBytes($"(DataRequest):\n- {string.Join("\n- ", Records.Select(x => x.Dump()))}");
  39. }
  40. }
  41. }