mirror of
https://github.com/qt/qtmqtt.git
synced 2026-10-25 16:16:26 +00:00
Disallow property changes while connected
Certain properties cannot be changed while connected and hence this should be reflected in the behavior. Task-number: QTBUG-67176 Change-Id: Ib9baafa1ac8d99d7a071839c892187efb51491e0 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
parent
ee7961e9be
commit
bcc2a4e7b5
@ -580,6 +580,12 @@ quint16 QMqttClient::keepAlive() const
|
||||
void QMqttClient::setHostname(const QString &hostname)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing hostname while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_hostname == hostname)
|
||||
return;
|
||||
|
||||
@ -590,6 +596,12 @@ void QMqttClient::setHostname(const QString &hostname)
|
||||
void QMqttClient::setPort(quint16 port)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing port while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_port == port)
|
||||
return;
|
||||
|
||||
@ -600,11 +612,12 @@ void QMqttClient::setPort(quint16 port)
|
||||
void QMqttClient::setClientId(const QString &clientId)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
if (d->m_clientId == clientId)
|
||||
return;
|
||||
|
||||
d->m_clientId = clientId;
|
||||
emit clientIdChanged(clientId);
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing client ID while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
d->setClientId(clientId);
|
||||
}
|
||||
|
||||
void QMqttClient::setKeepAlive(quint16 keepAlive)
|
||||
@ -625,6 +638,12 @@ void QMqttClient::setKeepAlive(quint16 keepAlive)
|
||||
void QMqttClient::setProtocolVersion(ProtocolVersion protocolVersion)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing protocol version while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_protocolVersion == protocolVersion)
|
||||
return;
|
||||
|
||||
@ -652,6 +671,12 @@ void QMqttClient::setState(ClientState state)
|
||||
void QMqttClient::setUsername(const QString &username)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing username while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_username == username)
|
||||
return;
|
||||
|
||||
@ -662,6 +687,12 @@ void QMqttClient::setUsername(const QString &username)
|
||||
void QMqttClient::setPassword(const QString &password)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing password while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_password == password)
|
||||
return;
|
||||
|
||||
@ -672,6 +703,12 @@ void QMqttClient::setPassword(const QString &password)
|
||||
void QMqttClient::setCleanSession(bool cleanSession)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing clean session while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_cleanSession == cleanSession)
|
||||
return;
|
||||
|
||||
@ -682,6 +719,12 @@ void QMqttClient::setCleanSession(bool cleanSession)
|
||||
void QMqttClient::setWillTopic(const QString &willTopic)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing will topic while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_willTopic == willTopic)
|
||||
return;
|
||||
|
||||
@ -692,6 +735,12 @@ void QMqttClient::setWillTopic(const QString &willTopic)
|
||||
void QMqttClient::setWillQoS(quint8 willQoS)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing will qos while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_willQoS == willQoS)
|
||||
return;
|
||||
|
||||
@ -702,6 +751,12 @@ void QMqttClient::setWillQoS(quint8 willQoS)
|
||||
void QMqttClient::setWillMessage(const QByteArray &willMessage)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing will message while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_willMessage == willMessage)
|
||||
return;
|
||||
|
||||
@ -712,6 +767,12 @@ void QMqttClient::setWillMessage(const QByteArray &willMessage)
|
||||
void QMqttClient::setWillRetain(bool willRetain)
|
||||
{
|
||||
Q_D(QMqttClient);
|
||||
|
||||
if (state() != QMqttClient::Disconnected) {
|
||||
qCDebug(lcMqttClient) << "Changing will retain while connected is not possible.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->m_willRetain == willRetain)
|
||||
return;
|
||||
|
||||
@ -763,4 +824,15 @@ void QMqttClientPrivate::setStateAndError(QMqttClient::ClientState s, QMqttClien
|
||||
q->setError(e);
|
||||
}
|
||||
|
||||
void QMqttClientPrivate::setClientId(const QString &id)
|
||||
{
|
||||
Q_Q(QMqttClient);
|
||||
|
||||
if (m_clientId == id)
|
||||
return;
|
||||
|
||||
m_clientId = id;
|
||||
emit q->clientIdChanged(id);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
@ -57,6 +57,7 @@ public:
|
||||
QMqttClientPrivate(QMqttClient *c);
|
||||
~QMqttClientPrivate() override;
|
||||
void setStateAndError(QMqttClient::ClientState s, QMqttClient::ClientError e = QMqttClient::NoError);
|
||||
void setClientId(const QString &id);
|
||||
QMqttClient *m_client{nullptr};
|
||||
QString m_hostname;
|
||||
quint16 m_port{0};
|
||||
|
||||
@ -691,7 +691,7 @@ void QMqttConnection::readConnackProperties()
|
||||
case 0x12: { // 3.2.2.3.7 Assigned clientId
|
||||
const QString assignedClientId = readBufferTyped<QString>(&propertyLength);
|
||||
serverProperties.serverData->details |= QMqttServerConnectionProperties::AssignedClientId;
|
||||
m_clientPrivate->m_client->setClientId(assignedClientId);
|
||||
m_clientPrivate->setClientId(assignedClientId);
|
||||
break;
|
||||
}
|
||||
case 0x22: { // 3.2.2.3.8 Topic Alias Maximum
|
||||
|
||||
@ -66,6 +66,8 @@ private Q_SLOTS:
|
||||
void reconnect_QTBUG65726();
|
||||
void openIODevice_QTBUG66955_data();
|
||||
void openIODevice_QTBUG66955();
|
||||
void staticProperties_QTBUG_67176_data();
|
||||
void staticProperties_QTBUG_67176();
|
||||
private:
|
||||
QProcess m_brokerProcess;
|
||||
QString m_testBroker;
|
||||
@ -551,6 +553,48 @@ void Tst_QMqttClient::openIODevice_QTBUG66955()
|
||||
QTRY_COMPARE(trans.written, 1);
|
||||
}
|
||||
|
||||
DefaultVersionTestData(Tst_QMqttClient::staticProperties_QTBUG_67176_data)
|
||||
|
||||
void Tst_QMqttClient::staticProperties_QTBUG_67176()
|
||||
{
|
||||
QFETCH(QMqttClient::ProtocolVersion, mqttVersion);
|
||||
|
||||
VersionClient(mqttVersion, client);
|
||||
client.setHostname(m_testBroker);
|
||||
client.setPort(m_port);
|
||||
|
||||
const QString clientId = client.clientId();
|
||||
const quint16 keepAlive = client.keepAlive();
|
||||
const bool clean = client.cleanSession();
|
||||
|
||||
client.connectToHost();
|
||||
QTRY_VERIFY2(client.state() == QMqttClient::Connected, "Could not connect to broker.");
|
||||
|
||||
client.setClientId(QLatin1String("someclient"));
|
||||
QCOMPARE(client.clientId(), clientId);
|
||||
|
||||
client.setHostname(QLatin1String("some.domain.foo"));
|
||||
QCOMPARE(client.hostname(), m_testBroker);
|
||||
|
||||
client.setPort(1234);
|
||||
QCOMPARE(client.port(), m_port);
|
||||
|
||||
client.setKeepAlive(keepAlive + 10);
|
||||
QCOMPARE(client.keepAlive(), keepAlive);
|
||||
|
||||
client.setProtocolVersion(QMqttClient::MQTT_3_1);
|
||||
QCOMPARE(client.protocolVersion(), mqttVersion);
|
||||
|
||||
client.setUsername(QLatin1String("someUser"));
|
||||
QCOMPARE(client.username(), QLatin1String());
|
||||
|
||||
client.setPassword(QLatin1String("somePassword"));
|
||||
QCOMPARE(client.password(), QLatin1String());
|
||||
|
||||
client.setCleanSession(!clean);
|
||||
QCOMPARE(client.cleanSession(), clean);
|
||||
}
|
||||
|
||||
QTEST_MAIN(Tst_QMqttClient)
|
||||
|
||||
#include "tst_qmqttclient.moc"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user