DMDataRequest.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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(IDMReadBuffer buffer)
  17. {
  18. _records.Clear();
  19. ushort bytestaken = 0;
  20. while (bytestaken < CheckSum)
  21. {
  22. var length = buffer.PeekUInt16(0);
  23. var record = new DMRecord();
  24. record.Decode(new DMPartialReadBuffer(buffer, 0, length));
  25. bytestaken += length;
  26. _records.Add(record);
  27. }
  28. }
  29. protected override void DoEncode(IDMWriteBuffer buffer)
  30. {
  31. foreach (var record in _records)
  32. record.Encode(buffer);
  33. buffer.InsertUInt16(0, buffer.BufferSize);
  34. }
  35. public override byte[] Dump()
  36. {
  37. return Encoding.UTF8.GetBytes($"(DataRequest):\n- {string.Join("\n- ", Records.Select(x => x.Dump()))}");
  38. }
  39. }
  40. }