MySQLPacket.decodeGenericPacket constructor

MySQLPacket.decodeGenericPacket(
  1. Uint8List buffer
)

Implementation

factory MySQLPacket.decodeGenericPacket(Uint8List buffer) {
  final byteData = ByteData.sublistView(buffer);
  int offset = 0;

  final header = MySQLPacket.decodePacketHeader(buffer);
  offset += 4;
  final payloadLength = header.item1;
  final sequenceNumber = header.item2;

  final type = byteData.getUint8(offset);

  MySQLPacketPayload payload;

  if (type == 0x00 && payloadLength >= 7) {
    // OK packet
    payload = MySQLPacketOK.decode(Uint8List.sublistView(buffer, offset));
  } else if (type == 0xfe && payloadLength < 9) {
    // EOF packet
    payload = MySQLPacketEOF.decode(Uint8List.sublistView(buffer, offset));
  } else if (type == 0xff) {
    payload = MySQLPacketError.decode(Uint8List.sublistView(buffer, offset));
  } else if (type == 0x01) {
    payload = MySQLPacketExtraAuthData.decode(
        Uint8List.sublistView(buffer, offset));
  } else {
    throw MySQLProtocolException("Unsupported generic packet: $buffer");
  }

  return MySQLPacket(
    sequenceID: sequenceNumber,
    payloadLength: payloadLength,
    payload: payload,
  );
}