diff options
author | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2018-03-09 11:04:18 +0100 |
---|---|---|
committer | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2018-03-12 09:10:50 +0000 |
commit | 434ed837417ed0692bc55fc882d918a47a6b7e71 (patch) | |
tree | ee743c23d79494d97f6fbd92cc0a4387f9d819fa | |
parent | 539bc149f00361f3d76970eeab4557b6a2eb6bca (diff) |
Fix connecting via an already open QIODevice transportv5.11.0-beta4v5.11.0-beta3
In case a transport is handed over to QMqttClient which is based on
QIODevice and already opened, the implementation was not sending a
CONNECT request and hence the broker never got informed about a new
client establishing connection. Hence manually send the connect
statement.
For QAbstractSocket based transports this was done already.
Task-number: QTBUG-66955
Change-Id: I50cba1b6dd03d0af561d4e39a3b57af5b2161a4b
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
-rw-r--r-- | src/mqtt/qmqttconnection.cpp | 2 | ||||
-rw-r--r-- | tests/auto/qmqttclient/tst_qmqttclient.cpp | 36 |
2 files changed, 37 insertions, 1 deletions
diff --git a/src/mqtt/qmqttconnection.cpp b/src/mqtt/qmqttconnection.cpp index aaaa514..b60816a 100644 --- a/src/mqtt/qmqttconnection.cpp +++ b/src/mqtt/qmqttconnection.cpp @@ -126,7 +126,7 @@ bool QMqttConnection::ensureTransportOpen(const QString &sslPeerName) if (m_transportType == QMqttClient::IODevice) { if (m_transport->isOpen()) - return true; + return sendControlConnect(); if (!m_transport->open(QIODevice::ReadWrite)) { qWarning("Could not open Transport IO device"); diff --git a/tests/auto/qmqttclient/tst_qmqttclient.cpp b/tests/auto/qmqttclient/tst_qmqttclient.cpp index e6cf055..b94bfd4 100644 --- a/tests/auto/qmqttclient/tst_qmqttclient.cpp +++ b/tests/auto/qmqttclient/tst_qmqttclient.cpp @@ -57,6 +57,7 @@ private Q_SLOTS: void dataIncludingZero(); void publishLongTopic(); void reconnect_QTBUG65726(); + void openIODevice_QTBUG66955(); private: QProcess m_brokerProcess; QString m_testBroker; @@ -447,6 +448,41 @@ void Tst_QMqttClient::reconnect_QTBUG65726() QTRY_COMPARE(client.error(), QMqttClient::NoError); } +class IOTransport : public QIODevice +{ +public: + bool open(OpenMode mode) override { + return QIODevice::open(mode); + } + qint64 readData(char *data, qint64 maxlen) override { + Q_UNUSED(data); + Q_UNUSED(maxlen); + return 0; + } + qint64 writeData(const char *data, qint64 len) override { + Q_UNUSED(data); + Q_UNUSED(len); + if (data[0] == 0x10) + written = 1; + else + qWarning() << "Received unknown/invalid data"; + return len; + } + QAtomicInt written{0}; +}; + +void Tst_QMqttClient::openIODevice_QTBUG66955() +{ + IOTransport trans; + trans.open(QIODevice::ReadWrite); + + QMqttClient client; + client.setTransport(&trans, QMqttClient::IODevice); + client.connectToHost(); + + QTRY_COMPARE(trans.written, 1); +} + QTEST_MAIN(Tst_QMqttClient) #include "tst_qmqttclient.moc" |