add method

List<Uint8List> add(
  1. List<int> chunk
)

Feeds received bytes in and returns whatever complete frame payloads (header plus body, without the length prefix) are now available.

Implementation

List<Uint8List> add(List<int> chunk) {
  _buffer.addAll(chunk);
  IsoTrace.log(IsoTraceKind.incoming, "${chunk.length} bytes from the socket, ${_buffer.length} buffered", bytes: Uint8List.fromList(chunk));
  final List<Uint8List> frames = <Uint8List>[];
  while (_buffer.length >= lengthBytes) {
    int length = 0;
    for (int i = 0; i < lengthBytes; i++) {
      length = length << 8 | _buffer[i] & 0xFF;
    }
    if (length == 0) {
      _buffer.removeRange(0, lengthBytes);
      continue;
    }
    if (_buffer.length < lengthBytes + length) {
      IsoTrace.log(IsoTraceKind.incoming, "frame says $length bytes, only ${_buffer.length - lengthBytes} arrived so far — waiting");
      break;
    }
    frames.add(Uint8List.fromList(_buffer.sublist(lengthBytes, lengthBytes + length)));
    _buffer.removeRange(0, lengthBytes + length);
  }
  return frames;
}