summaryrefslogtreecommitdiffstats
path: root/examples/network/fortuneclient
diff options
context:
space:
mode:
authorAlex Trotsenko <alex1973tr@gmail.com>2015-03-05 10:52:17 +0200
committerAlex Trotsenko <alex1973tr@gmail.com>2016-01-13 16:31:33 +0000
commit184d66caa5f7f93b7383319c5c8985524e0dc824 (patch)
tree272ce3f066d3c80f77dfca50fd0914b341c2f9da /examples/network/fortuneclient
parentca6f11dcf207aa51ce32c9813ad1ab3790bb31ee (diff)
QDataStream: handle incomplete reads from QIODevice
This adds a way to resume reading from a stream after a ReadPastEnd error. This is done by introducing a stream read transaction mechanism that keeps read data in an internal buffer and rolls it back on failure. [ChangeLog][QtCore] Added QDataStream startTransaction(), commitTransaction(), rollbackTransaction(), abortTransaction() functions to support read transactions. Task-number: QTBUG-44418 Change-Id: Ibf946e1939a5573c4182fea7e26608947218c2d9 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Diffstat (limited to 'examples/network/fortuneclient')
-rw-r--r--examples/network/fortuneclient/client.cpp34
-rw-r--r--examples/network/fortuneclient/client.h3
2 files changed, 12 insertions, 25 deletions
diff --git a/examples/network/fortuneclient/client.cpp b/examples/network/fortuneclient/client.cpp
index 42fed30445..a71b90dda8 100644
--- a/examples/network/fortuneclient/client.cpp
+++ b/examples/network/fortuneclient/client.cpp
@@ -49,10 +49,7 @@ Client::Client(QWidget *parent)
, hostCombo(new QComboBox)
, portLineEdit(new QLineEdit)
, getFortuneButton(new QPushButton(tr("Get Fortune")))
-//! [1]
, tcpSocket(new QTcpSocket(this))
-//! [1]
- , blockSize(0)
, networkSession(Q_NULLPTR)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
@@ -100,6 +97,11 @@ Client::Client(QWidget *parent)
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
+//! [1]
+ in.setDevice(tcpSocket);
+ in.setVersion(QDataStream::Qt_4_0);
+//! [1]
+
connect(hostCombo, &QComboBox::editTextChanged,
this, &Client::enableGetFortuneButton);
connect(portLineEdit, &QLineEdit::textChanged,
@@ -171,7 +173,6 @@ Client::Client(QWidget *parent)
void Client::requestNewFortune()
{
getFortuneButton->setEnabled(false);
- blockSize = 0;
tcpSocket->abort();
//! [7]
tcpSocket->connectToHost(hostCombo->currentText(),
@@ -183,39 +184,24 @@ void Client::requestNewFortune()
//! [8]
void Client::readFortune()
{
-//! [9]
- QDataStream in(tcpSocket);
- in.setVersion(QDataStream::Qt_4_0);
-
- if (blockSize == 0) {
- if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
- return;
-//! [8]
-
-//! [10]
- in >> blockSize;
- }
-
- if (tcpSocket->bytesAvailable() < blockSize)
- return;
-//! [10] //! [11]
+ in.startTransaction();
QString nextFortune;
in >> nextFortune;
+ if (!in.commitTransaction())
+ return;
+
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, &Client::requestNewFortune);
return;
}
-//! [11]
-//! [12]
currentFortune = nextFortune;
-//! [9]
statusLabel->setText(currentFortune);
getFortuneButton->setEnabled(true);
}
-//! [12]
+//! [8]
//! [13]
void Client::displayError(QAbstractSocket::SocketError socketError)
diff --git a/examples/network/fortuneclient/client.h b/examples/network/fortuneclient/client.h
index 9b7d6f4dbf..3b07275ded 100644
--- a/examples/network/fortuneclient/client.h
+++ b/examples/network/fortuneclient/client.h
@@ -43,6 +43,7 @@
#include <QDialog>
#include <QTcpSocket>
+#include <QDataStream>
QT_BEGIN_NAMESPACE
class QComboBox;
@@ -75,8 +76,8 @@ private:
QPushButton *getFortuneButton;
QTcpSocket *tcpSocket;
+ QDataStream in;
QString currentFortune;
- quint16 blockSize;
QNetworkSession *networkSession;
};