One of the examples were using a different version than the others. Though QString's formatting probably didn't change since then so it was no problem. Anyway, pretend like we're releasing it now for the first time and set 6.5 on all of them Task-number: QTBUG-108875 Change-Id: I28b496ab3d8ff54c503a032ba15882cdf3d5eccf Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> (cherry picked from commit 71cd3bc074b4eea3158ba037ae2fe1c5b922ff3d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
91 lines
1.7 KiB
C++
91 lines
1.7 KiB
C++
// Copyright (C) 2016 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include "fortunethread.h"
|
|
|
|
FortuneThread::FortuneThread(QObject *parent)
|
|
: QThread(parent), quit(false)
|
|
{
|
|
}
|
|
|
|
//! [0]
|
|
FortuneThread::~FortuneThread()
|
|
{
|
|
mutex.lock();
|
|
quit = true;
|
|
cond.wakeOne();
|
|
mutex.unlock();
|
|
wait();
|
|
}
|
|
//! [0]
|
|
|
|
//! [1] //! [2]
|
|
void FortuneThread::requestNewFortune(const QString &hostName, quint16 port)
|
|
{
|
|
//! [1]
|
|
QMutexLocker locker(&mutex);
|
|
this->hostName = hostName;
|
|
this->port = port;
|
|
//! [3]
|
|
if (!isRunning())
|
|
start();
|
|
else
|
|
cond.wakeOne();
|
|
}
|
|
//! [2] //! [3]
|
|
|
|
//! [4]
|
|
void FortuneThread::run()
|
|
{
|
|
mutex.lock();
|
|
//! [4] //! [5]
|
|
QString serverName = hostName;
|
|
quint16 serverPort = port;
|
|
mutex.unlock();
|
|
//! [5]
|
|
|
|
//! [6]
|
|
while (!quit) {
|
|
//! [7]
|
|
const int Timeout = 5 * 1000;
|
|
|
|
QTcpSocket socket;
|
|
socket.connectToHost(serverName, serverPort);
|
|
//! [6] //! [8]
|
|
|
|
if (!socket.waitForConnected(Timeout)) {
|
|
emit error(socket.error(), socket.errorString());
|
|
return;
|
|
}
|
|
//! [8] //! [11]
|
|
|
|
QDataStream in(&socket);
|
|
in.setVersion(QDataStream::Qt_6_5);
|
|
QString fortune;
|
|
//! [11] //! [12]
|
|
|
|
do {
|
|
if (!socket.waitForReadyRead(Timeout)) {
|
|
emit error(socket.error(), socket.errorString());
|
|
return;
|
|
}
|
|
|
|
in.startTransaction();
|
|
in >> fortune;
|
|
} while (!in.commitTransaction());
|
|
//! [12] //! [15]
|
|
|
|
mutex.lock();
|
|
emit newFortune(fortune);
|
|
//! [7]
|
|
|
|
cond.wait(&mutex);
|
|
serverName = hostName;
|
|
serverPort = port;
|
|
mutex.unlock();
|
|
}
|
|
//! [15]
|
|
}
|