DMMessage.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Text;
  3. namespace InABox.DigitalMatter
  4. {
  5. public abstract class DMMessage : DMObject
  6. {
  7. private ushort headerlength;
  8. public abstract byte Type { get; }
  9. public ushort CheckSum { get; set; }
  10. protected override void BeforeEncode()
  11. {
  12. base.BeforeEncode();
  13. AddByte(0x02);
  14. AddByte(0x55);
  15. headerlength = AddByte(Type);
  16. }
  17. protected override void AfterEncode()
  18. {
  19. base.AfterEncode();
  20. var payloadlength = (ushort)(BufferSize - headerlength);
  21. InsertUInt16(headerlength, payloadlength);
  22. }
  23. protected override void BeforeDecode()
  24. {
  25. base.BeforeDecode();
  26. var id1 = TakeByte();
  27. var id2 = TakeByte();
  28. var type = TakeByte();
  29. CheckSum = TakeUInt16();
  30. }
  31. public virtual byte[] Dump()
  32. {
  33. return Encoding.UTF8.GetBytes($"Type: {BitConverter.ToString(new[] { Type })}");
  34. }
  35. }
  36. }