From d4cf48901c9020cb5654cb1ab8506f78f44fea9c Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 16 Sep 2020 02:16:06 +0200 Subject: [PATCH] SocketLineReader: don't call packets bytes --- core/backends/lan/landevicelink.cpp | 4 ++-- core/backends/lan/socketlinereader.h | 4 ++-- tests/testsocketlinereader.cpp | 6 +----- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index 135e3d02d..829df9e1d 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -100,7 +100,7 @@ bool LanDeviceLink::sendPacket(NetworkPacket& np) void LanDeviceLink::dataReceived() { - if (m_socketLineReader->bytesAvailable() == 0) return; + if (!m_socketLineReader->hasPacketsAvailable()) return; const QByteArray serializedPacket = m_socketLineReader->readLine(); NetworkPacket packet((QString())); @@ -134,7 +134,7 @@ void LanDeviceLink::dataReceived() Q_EMIT receivedPacket(packet); - if (m_socketLineReader->bytesAvailable() > 0) { + if (m_socketLineReader->hasPacketsAvailable()) { QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection); } diff --git a/core/backends/lan/socketlinereader.h b/core/backends/lan/socketlinereader.h index 890807b3b..77728843b 100644 --- a/core/backends/lan/socketlinereader.h +++ b/core/backends/lan/socketlinereader.h @@ -26,14 +26,14 @@ class KDECONNECTCORE_EXPORT SocketLineReader public: explicit SocketLineReader(QSslSocket* socket, QObject* parent = nullptr); + bool hasPacketsAvailable() const { return !m_packets.isEmpty(); } QByteArray readLine() { return m_packets.dequeue(); } qint64 write(const QByteArray& data) { return m_socket->write(data); } QHostAddress peerAddress() const { return m_socket->peerAddress(); } QSslCertificate peerCertificate() const { return m_socket->peerCertificate(); } - qint64 bytesAvailable() const { return m_packets.size(); } QSslSocket* m_socket; - + Q_SIGNALS: void readyRead(); diff --git a/tests/testsocketlinereader.cpp b/tests/testsocketlinereader.cpp index acb43d418..0e3603d29 100644 --- a/tests/testsocketlinereader.cpp +++ b/tests/testsocketlinereader.cpp @@ -87,12 +87,8 @@ void TestSocketLineReader::socketLineReader() void TestSocketLineReader::newPacket() { - if (!m_reader->bytesAvailable()) { - return; - } - int maxLoops = 5; - while(m_reader->bytesAvailable() > 0 && maxLoops > 0) { + while(m_reader->hasPacketsAvailable() && maxLoops > 0) { --maxLoops; const QByteArray packet = m_reader->readLine(); if (!packet.isEmpty()) {