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.5
Change-Id: Ieb68f67e2921f46acd682f81dfa5dc5b040c88f5
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
(cherry picked from commit fd1405e61b9939e2630c751367413e07fdc76408)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Samuel Gaist 2023-11-29 10:49:30 +01:00 committed by Qt Cherry-pick Bot
parent 03ac42cd8e
commit 1bd40b3e94
2 changed files with 5 additions and 12 deletions

View File

@ -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;

View File

@ -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();