Segment.vala 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. namespace LibPeer.Protocols.Stp.Segments {
  2. public const uint8 SEGMENT_ACKNOWLEDGEMENT = 0x06;
  3. public const uint8 SEGMENT_CONTROL = 0x10;
  4. public const uint8 SEGMENT_PAYLOAD = 0x0E;
  5. public abstract class Segment : Object {
  6. protected abstract uint8 identifier { get; }
  7. public void serialise(OutputStream stream) {
  8. MemoryOutputStream buffer = new MemoryOutputStream(null, GLib.realloc, GLib.free);
  9. buffer.write({identifier});
  10. serialise_data(buffer);
  11. stream.write(buffer.steal_data());
  12. }
  13. protected abstract void serialise_data(OutputStream stream);
  14. public static Segment deserialise(InputStream stream) {
  15. uint8[] segment_type = new uint8[1];
  16. stream.read(segment_type);
  17. switch (segment_type[0]) {
  18. case SEGMENT_ACKNOWLEDGEMENT:
  19. return new Acknowledgement.from_stream(stream);
  20. case SEGMENT_CONTROL:
  21. return new Control.from_stream(stream);
  22. case SEGMENT_PAYLOAD:
  23. return new Payload.from_stream(stream);
  24. default:
  25. assert_not_reached();
  26. }
  27. }
  28. }
  29. }