CmdBridge: Fix handle partial package marker

Change-Id: Ifbae08fdd04a292735b0bf515d7c9b43f75cc7d9
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-10-11 08:36:32 +02:00
parent e2547608c0
commit ae6a0bb706

View File

@@ -320,21 +320,31 @@ expected_str<QFuture<Environment>> Client::start()
QThread::currentThread()->quit(); QThread::currentThread()->quit();
}); });
auto stateMachine = [state = int(0), packetSize(0), packetData = QByteArray(), this]( auto stateMachine =
[markerOffset = 0, state = int(0), packetSize(0), packetData = QByteArray(), this](
QByteArray &buffer) mutable { QByteArray &buffer) mutable {
static const QByteArray MagicCode{GOBRIDGE_MAGIC_PACKET_MARKER}; static const QByteArrayView MagicCode{GOBRIDGE_MAGIC_PACKET_MARKER};
if (state == 0) { if (state == 0) {
int start = buffer.indexOf(MagicCode); const auto offsetMagicCode = MagicCode.mid(markerOffset);
int start = buffer.indexOf(offsetMagicCode);
if (start == -1) { if (start == -1) {
if (buffer.size() < offsetMagicCode.size()
&& offsetMagicCode.startsWith(buffer)) {
// Partial magic marker?
markerOffset += buffer.size();
buffer.clear();
return;
}
// Broken package, search for next magic marker // Broken package, search for next magic marker
qCWarning(clientLog) << "Magic marker was not found"; qCWarning(clientLog) << "Magic marker was not found";
// If we don't find a magic marker, the rest of the buffer is trash. // If we don't find a magic marker, the rest of the buffer is trash.
buffer.clear(); buffer.clear();
} else { } else {
buffer.remove(0, start + MagicCode.size()); buffer.remove(0, start + offsetMagicCode.size());
state = 1; state = 1;
} }
markerOffset = 0;
} }
if (state == 1) { if (state == 1) {