receive method

Future<RxPacket?> receive()

Implementation

Future<RxPacket?> receive() async {
  if (!_isReading) return null;

  _isReading = false;

  await _socket.readBuffer(_header);
  int dataSize = _header[0] + (_header[1] << 8) + (_header[2] << 16);
  int packetNumber = _header[3];
  var data = ReadBuffer(dataSize);
  await _socket.readBuffer(data);

  if (dataSize == 0xffffff) {
    final buffers = <ReadBuffer>[data];
    while (data.length == 0xffffff) {
      await _socket.readBuffer(_header);
      int dataSize = _header[0] + (_header[1] << 8) + (_header[2] << 16);
      packetNumber = _header[3];
      var buf = ReadBuffer(dataSize);
      await _socket.readBuffer(buf);
      buffers.add(buf);
    }

    int length = 0;
    for (ReadBuffer buf in buffers) length += buf.length;
    data = ReadBuffer(length);
    int start = 0;
    for (ReadBuffer buf in buffers) {
      data.data.setRange(start, start + buf.length, buf.data);
      start += buf.length;
    }
  }

  _isReading = true;

  return RxPacket(packetNumber, data);
}