toBytes method
Converts the VERSION_NEGOTIATION packet to raw bytes
Implementation
Uint8List toBytes() {
// version(4) + dcidLen(1) + dcid + scidLen(1) + scid + versions
final headerLength =
4 + 1 + destinationCid.length + 1 + sourceCid.length;
final buffer = Uint8List(headerLength + (supportedVersions.length * 4));
final view = ByteData.view(buffer.buffer);
int offset = 0;
// Write version (0 for VERSION_NEGOTIATION)
view.setUint32(offset, 0, Endian.big);
offset += 4;
// Write destination CID
view.setUint8(offset, destinationCid.length);
offset += 1;
buffer.setAll(offset, destinationCid.bytes);
offset += destinationCid.length;
// Write source CID
view.setUint8(offset, sourceCid.length);
offset += 1;
buffer.setAll(offset, sourceCid.bytes);
offset += sourceCid.length;
// Write supported versions
for (final version in supportedVersions) {
view.setUint32(offset, version, Endian.big);
offset += 4;
}
return buffer;
}