headerBytes method

Uint8Buffer headerBytes()

Gets the value of the Mqtt header as a byte array

Implementation

typed.Uint8Buffer headerBytes() {
  final headerBytes = typed.Uint8Buffer();

  // Build the bytes that make up the header. The first byte is a
  // combination of message type, dup, qos and retain, and the
  // following bytes (up to 4 of them) are the size of the
  // payload + variable header.
  final messageTypeLength = messageType!.index << 4;
  final duplicateLength = (duplicate ? 1 : 0) << 3;
  final qosLength = qos.index << 1;
  final retainLength = retain ? 1 : 0;
  final firstByte =
      messageTypeLength + duplicateLength + qosLength + retainLength;
  headerBytes.add(firstByte);
  headerBytes.addAll(getRemainingLengthBytes());
  return headerBytes;
}