Skip to content

Commit

Permalink
Simplify QAbstractSocket::readData()
Browse files Browse the repository at this point in the history
It needed refactoring after prior changes (bf6897e,
1ce203d, 48a4a67).

Change-Id: I06ee3f4f70db2a71acfc8e9c1da5b4ad7524179d
Reviewed-by: Oswald Buddenhagen <[email protected]>
  • Loading branch information
Alex Trotsenko committed Feb 3, 2016
1 parent 5b75a16 commit 29af35f
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions src/network/socket/qabstractsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2414,30 +2414,17 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
{
Q_D(QAbstractSocket);

// Check if the read notifier can be enabled again.
if (d->socketEngine && !d->socketEngine->isReadNotificationEnabled() && d->socketEngine->isValid())
d->socketEngine->setReadNotificationEnabled(true);
// if we're not connected, return -1 indicating EOF
if (!d->socketEngine || !d->socketEngine->isValid() || d->state != QAbstractSocket::ConnectedState)
return maxSize ? qint64(-1) : qint64(0);

if (!maxSize)
return 0;

// This is for a buffered QTcpSocket
if (d->isBuffered)
// if we're still connected, return 0 indicating there may be more data in the future
// if we're not connected, return -1 indicating EOF
return d->state == QAbstractSocket::ConnectedState ? qint64(0) : qint64(-1);

if (!d->socketEngine)
return -1; // no socket engine is probably EOF
if (!d->socketEngine->isValid())
return -1; // This is for unbuffered TCP when we already had been disconnected
if (d->state != QAbstractSocket::ConnectedState)
return -1; // This is for unbuffered TCP if we're not connected yet
qint64 readBytes = d->socketEngine->read(data, maxSize);
qint64 readBytes = (maxSize && !d->isBuffered) ? d->socketEngine->read(data, maxSize)
: qint64(0);
if (readBytes == -2) {
// -2 from the engine means no bytes available (EAGAIN) so read more later
return 0;
} else if (readBytes < 0) {
readBytes = 0;
}
if (readBytes < 0) {
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
d->resetSocketLayer();
d->state = QAbstractSocket::UnconnectedState;
Expand All @@ -2452,7 +2439,6 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
readBytes);
#endif
return readBytes;

}

/*! \reimp
Expand Down

0 comments on commit 29af35f

Please sign in to comment.