encodeOneBinaryPacket static method

dynamic encodeOneBinaryPacket(
  1. dynamic p,
  2. dynamic doneCallback(
    1. dynamic _
    )
)

Implementation

static encodeOneBinaryPacket(p, Function(dynamic _) doneCallback) {
  onBinaryPacketEncode(dynamic packet) {
    var encodingLength = '${packet.length}';
    Uint8List sizeBuffer;

    if (packet is String) {
      sizeBuffer = Uint8List(encodingLength.length + 2);
      sizeBuffer[0] = 0; // is a string (not true binary = 0)
      for (var i = 0; i < encodingLength.length; i++) {
        sizeBuffer[i + 1] = int.parse(encodingLength[i]);
      }
      sizeBuffer[sizeBuffer.length - 1] = 255;
      return doneCallback(
          List.from(sizeBuffer)..addAll(stringToBuffer(packet)));
    }

    sizeBuffer = Uint8List(encodingLength.length + 2);
    sizeBuffer[0] = 1; // is binary (true binary = 1)
    for (var i = 0; i < encodingLength.length; i++) {
      sizeBuffer[i + 1] = int.parse(encodingLength[i]);
    }
    sizeBuffer[sizeBuffer.length - 1] = 255;

    doneCallback(List.from(sizeBuffer)..addAll(packet));
  }

  encodePacket(p,
      supportsBinary: true, utf8encode: true, callback: onBinaryPacketEncode);
}