buildMessageHeader static method

List<int> buildMessageHeader(
  1. int headerType,
  2. int messageLength,
  3. bool upgradedProtocol
)

Builds a message header according to a given headerType, messageLength, an the information if it is an upgradedProtocol. If the upgradedProtocol is true the header will have a size of 5 bytes otherwise 4.

Implementation

static List<int> buildMessageHeader(
    int headerType, int messageLength, bool upgradedProtocol) {
  if (upgradedProtocol) {
    if (messageLength > _maxMessageLengthConnectanum) {
      throw Exception(
          'Their should be no message length larger then 2^$maxMessageLengthConnectanumExponent');
    }
    var messageHeader = Uint8List(5);
    messageHeader[0] = headerType;
    messageHeader[1] = ((messageLength >> 24) & 0xFF);
    messageHeader[2] = ((messageLength >> 16) & 0xFF);
    messageHeader[3] = ((messageLength >> 8) & 0xFF);
    messageHeader[4] = (messageLength & 0xFF);
    return messageHeader.toList(growable: false);
  } else {
    if (messageLength > maxMessageLength) {
      throw Exception(
          'Their should be no message length larger then 2^$maxMessageLengthExponent');
    }
    var messageHeader = Uint8List(4);
    messageHeader[0] = headerType;
    messageHeader[1] = ((messageLength >> 16) & 0xFF);
    messageHeader[2] = ((messageLength >> 8) & 0xFF);
    messageHeader[3] = (messageLength & 0xFF);
    return messageHeader.toList(growable: false);
  }
}