diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-03-05 10:52:17 +0200 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2016-01-13 16:31:33 +0000 |
commit | 184d66caa5f7f93b7383319c5c8985524e0dc824 (patch) | |
tree | 272ce3f066d3c80f77dfca50fd0914b341c2f9da /examples/network/blockingfortuneclient | |
parent | ca6f11dcf207aa51ce32c9813ad1ab3790bb31ee (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/blockingfortuneclient')
-rw-r--r-- | examples/network/blockingfortuneclient/fortunethread.cpp | 28 |
1 files changed, 9 insertions, 19 deletions
diff --git a/examples/network/blockingfortuneclient/fortunethread.cpp b/examples/network/blockingfortuneclient/fortunethread.cpp index b948d504ae..a668e45d93 100644 --- a/examples/network/blockingfortuneclient/fortunethread.cpp +++ b/examples/network/blockingfortuneclient/fortunethread.cpp @@ -96,37 +96,27 @@ void FortuneThread::run() emit error(socket.error(), socket.errorString()); return; } -//! [8] //! [9] +//! [8] //! [11] - while (socket.bytesAvailable() < (int)sizeof(quint16)) { - if (!socket.waitForReadyRead(Timeout)) { - emit error(socket.error(), socket.errorString()); - return; - } -//! [9] //! [10] - } -//! [10] //! [11] - - quint16 blockSize; QDataStream in(&socket); in.setVersion(QDataStream::Qt_4_0); - in >> blockSize; + QString fortune; //! [11] //! [12] - while (socket.bytesAvailable() < blockSize) { + do { if (!socket.waitForReadyRead(Timeout)) { emit error(socket.error(), socket.errorString()); return; } -//! [12] //! [13] - } -//! [13] //! [14] + + in.startTransaction(); + in >> fortune; + } while (!in.commitTransaction()); +//! [12] //! [15] mutex.lock(); - QString fortune; - in >> fortune; emit newFortune(fortune); -//! [7] //! [14] //! [15] +//! [7] cond.wait(&mutex); serverName = hostName; |