serialize method

Uint8List serialize()

Implementation

Uint8List serialize() {
  // Ensure ALPN extension reflects semantic field
  upsertAlpnExtension();

  final body = QuicBuffer();

  // 1. legacy_version
  body.pushUint16(legacyVersion);

  // 2. random
  body.pushBytes(random);

  // 3. legacy_session_id
  body.pushUint8(sessionId.length);
  body.pushBytes(sessionId);

  // 4. cipher_suites
  body.pushUint16(cipherSuites.length * 2);
  for (final suite in cipherSuites) {
    body.pushUint16(suite);
  }

  // 5. legacy_compression_methods
  body.pushUint8(compressionMethods.length);
  body.pushBytes(compressionMethods);

  // 6. extensions
  final extBuffer = QuicBuffer();
  for (final ext in extensions) {
    extBuffer.pushUint16(ext.type);
    extBuffer.pushUint16(ext.data.length);
    extBuffer.pushBytes(ext.data);
  }

  final extBytes = extBuffer.toBytes();
  body.pushUint16(extBytes.length);
  body.pushBytes(extBytes);

  final bodyBytes = body.toBytes();

  // 7. Handshake header
  final header = Uint8List(4);
  header[0] = 0x01; // ClientHello
  header[1] = (bodyBytes.length >> 16) & 0xFF;
  header[2] = (bodyBytes.length >> 8) & 0xFF;
  header[3] = bodyBytes.length & 0xFF;

  return Uint8List.fromList([...header, ...bodyBytes]);
}