1 /* 2 * This file was automatically generated by sel-utils and 3 * released under the MIT License. 4 * 5 * License: https://github.com/sel-project/sel-utils/blob/master/LICENSE 6 * Repository: https://github.com/sel-project/sel-utils 7 * Generated from https://github.com/sel-project/sel-utils/blob/master/xml/protocol/hncom2.xml 8 */ 9 /** 10 * Packets used for sending more than one packet at once. 11 */ 12 module sul.protocol.hncom2.util; 13 14 import std.bitmanip : write, peek; 15 static import std.conv; 16 import std.system : Endian; 17 import std.typetuple : TypeTuple; 18 import std.typecons : Tuple; 19 import std.uuid : UUID; 20 21 import sul.utils.buffer; 22 import sul.utils.var; 23 24 static import sul.protocol.hncom2.types; 25 26 static if(__traits(compiles, { import sul.metadata.hncom2; })) import sul.metadata.hncom2; 27 28 alias Packets = TypeTuple!(Uncompressed, Compressed); 29 30 class Uncompressed : Buffer { 31 32 public enum ubyte ID = 1; 33 34 public enum bool CLIENTBOUND = true; 35 public enum bool SERVERBOUND = true; 36 37 public enum string[] FIELDS = ["packets"]; 38 39 public ubyte[][] packets; 40 41 public pure nothrow @safe @nogc this() {} 42 43 public pure nothrow @safe @nogc this(ubyte[][] packets) { 44 this.packets = packets; 45 } 46 47 public pure nothrow @safe ubyte[] encode(bool writeId=true)() { 48 _buffer.length = 0; 49 static if(writeId){ writeBigEndianUbyte(ID); } 50 writeBytes(varuint.encode(cast(uint)packets.length)); foreach(cfavc;packets){ writeBytes(varuint.encode(cast(uint)cfavc.length)); writeBytes(cfavc); } 51 return _buffer; 52 } 53 54 public pure nothrow @safe void decode(bool readId=true)() { 55 static if(readId){ ubyte _id; _id=readBigEndianUbyte(); } 56 packets.length=varuint.decode(_buffer, &_index); foreach(ref cfavc;packets){ cfavc.length=varuint.decode(_buffer, &_index); if(_buffer.length>=_index+cfavc.length){ cfavc=_buffer[_index.._index+cfavc.length].dup; _index+=cfavc.length; } } 57 } 58 59 public static pure nothrow @safe Uncompressed fromBuffer(bool readId=true)(ubyte[] buffer) { 60 Uncompressed ret = new Uncompressed(); 61 ret._buffer = buffer; 62 ret.decode!readId(); 63 return ret; 64 } 65 66 public override string toString() { 67 return "Uncompressed(packets: " ~ std.conv.to!string(this.packets) ~ ")"; 68 } 69 70 } 71 72 class Compressed : Buffer { 73 74 public enum ubyte ID = 2; 75 76 public enum bool CLIENTBOUND = true; 77 public enum bool SERVERBOUND = true; 78 79 public enum string[] FIELDS = ["size", "payload"]; 80 81 public uint size; 82 public ubyte[] payload; 83 84 public pure nothrow @safe @nogc this() {} 85 86 public pure nothrow @safe @nogc this(uint size, ubyte[] payload=(ubyte[]).init) { 87 this.size = size; 88 this.payload = payload; 89 } 90 91 public pure nothrow @safe ubyte[] encode(bool writeId=true)() { 92 _buffer.length = 0; 93 static if(writeId){ writeBigEndianUbyte(ID); } 94 writeBytes(varuint.encode(size)); 95 writeBytes(payload); 96 return _buffer; 97 } 98 99 public pure nothrow @safe void decode(bool readId=true)() { 100 static if(readId){ ubyte _id; _id=readBigEndianUbyte(); } 101 size=varuint.decode(_buffer, &_index); 102 payload=_buffer[_index..$].dup; _index=_buffer.length; 103 } 104 105 public static pure nothrow @safe Compressed fromBuffer(bool readId=true)(ubyte[] buffer) { 106 Compressed ret = new Compressed(); 107 ret._buffer = buffer; 108 ret.decode!readId(); 109 return ret; 110 } 111 112 public override string toString() { 113 return "Compressed(size: " ~ std.conv.to!string(this.size) ~ ", payload: " ~ std.conv.to!string(this.payload) ~ ")"; 114 } 115 116 } 117