From fd1405e61b9939e2630c751367413e07fdc76408 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Wed, 29 Nov 2023 10:49:30 +0100 Subject: [PATCH] examples: update local fortune example to use transaction The local version of the fortune server and clients were not using the transaction paradigm yet. This patches fixes it and makes the example in line with the network version. Pick-to: 6.6 6.5 Change-Id: Ieb68f67e2921f46acd682f81dfa5dc5b040c88f5 Reviewed-by: Christian Ehrlicher --- examples/corelib/ipc/localfortuneclient/client.cpp | 14 ++++---------- examples/corelib/ipc/localfortuneserver/server.cpp | 3 +-- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/examples/corelib/ipc/localfortuneclient/client.cpp b/examples/corelib/ipc/localfortuneclient/client.cpp index 8336728f79..b71409560b 100644 --- a/examples/corelib/ipc/localfortuneclient/client.cpp +++ b/examples/corelib/ipc/localfortuneclient/client.cpp @@ -63,20 +63,14 @@ void Client::requestNewFortune() void Client::readFortune() { - if (blockSize == 0) { - // Relies on the fact that QDataStream serializes a quint32 into - // sizeof(quint32) bytes - if (socket->bytesAvailable() < (int)sizeof(quint32)) - return; - in >> blockSize; - } - - if (socket->bytesAvailable() < blockSize || in.atEnd()) - return; + in.startTransaction(); QString nextFortune; in >> nextFortune; + if (!in.commitTransaction()) + return; + if (nextFortune == currentFortune) { QTimer::singleShot(0, this, &Client::requestNewFortune); return; diff --git a/examples/corelib/ipc/localfortuneserver/server.cpp b/examples/corelib/ipc/localfortuneserver/server.cpp index 77322e03bd..a602a57f72 100644 --- a/examples/corelib/ipc/localfortuneserver/server.cpp +++ b/examples/corelib/ipc/localfortuneserver/server.cpp @@ -103,10 +103,9 @@ void Server::sendFortune() { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_6_0); + out.setVersion(QDataStream::Qt_6_5); const int fortuneIndex = QRandomGenerator::global()->bounded(0, fortunes.size()); const QString &message = fortunes.at(fortuneIndex); - out << quint32(message.size()); out << message; QLocalSocket *clientConnection = server->nextPendingConnection();