DMField.cs 924 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. namespace InABox.DigitalMatter
  3. {
  4. public abstract class DMField : DMObject
  5. {
  6. private int _payloadaddress;
  7. public abstract byte Type { get; }
  8. public ushort Length { get; set; }
  9. protected override void BeforeEncode()
  10. {
  11. base.BeforeEncode();
  12. _payloadaddress = AddByte(Type);
  13. }
  14. protected override void AfterEncode()
  15. {
  16. base.AfterEncode();
  17. Length = BufferSize;
  18. InsertUInt16(_payloadaddress, Length);
  19. }
  20. protected override void BeforeDecode()
  21. {
  22. base.BeforeDecode();
  23. int type = TakeByte();
  24. Length = PeekByte(0) == 255 ? TakeUInt16() : TakeByte();
  25. }
  26. public abstract bool IsValid();
  27. public virtual string Dump()
  28. {
  29. return BitConverter.ToString(new[] { Type });
  30. }
  31. }
  32. }