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 <ch.ehrlicher@gmx.de>
bb10
Samuel Gaist 2023-11-29 10:49:30 +01:00
parent 8220173b02
commit fd1405e61b
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();