onData method

void onData(
  1. Uint8List input
)
inherited

called by the strategy when raw data is received

Implementation

void onData(Uint8List input) {
  var data = input;
  if (_stash.isNotEmpty) {
    data = Uint8List.fromList(_stash + data);
    _stash = Uint8List(0);
  }

  var byteOffset = 0;

  if (data.isEmpty) {
    return;
  }

  var cycles = 0;

  while (byteOffset < data.length && cycles < 100) {
    if (_remainingBytes < 0) {
      if (data.length - byteOffset < 2) {
        _stash = Uint8List.fromList(data.sublist(byteOffset));

        return;
      }

      _remainingBytes = (data[byteOffset] << 8) | data[byteOffset + 1];
      byteOffset += 2;
    }

    cycles++;

    final leftInBuffer = data.length - byteOffset;

    final int bytesToRead = min(_remainingBytes, leftInBuffer);

    _buffer.addAll(data.sublist(byteOffset, byteOffset + bytesToRead));

    _remainingBytes -= bytesToRead;

    byteOffset += bytesToRead;

    if (_remainingBytes == 0) {
      _remainingBytes = -1;

      _processBuffer(_buffer);
      _buffer.clear();
    }
  }
}