1 module draklib.protocol.packet; 2 import draklib.bytestream; 3 import draklib.util : NotImplementedException; 4 5 abstract class Packet { 6 void encode(out byte[] data) { 7 ByteStream stream = ByteStream.alloc(getSize()); 8 stream.writeUByte(getID()); 9 _encode(stream); 10 data = stream.getBuffer().dup; 11 stream.clear(); 12 } 13 14 void decode(byte[] data) { 15 ByteStream stream = ByteStream.wrap(data); 16 stream.skip(1); // ID 17 _decode(stream); 18 } 19 20 protected void _encode(ref ByteStream stream) { 21 throw new NotImplementedException("Encoding has not been implemented by underlying class."); 22 } 23 24 protected void _decode(ref ByteStream stream) { 25 throw new NotImplementedException("Decoding has not been implemented by underlying class."); 26 } 27 28 abstract uint getSize(); 29 abstract ubyte getID(); 30 }