diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-07 18:06:43 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-09 10:11:53 +0100 |
commit | a18c97f1a897e4653a47a45976d78bea5a2243be (patch) | |
tree | 2de14a6251690948987c6a8faee01d0daf1bc824 /src/serialbus | |
parent | bfb62b60ad6873ad1912df0f08ce9cf7fd80447b (diff) |
QModbusTcpServer: do not store client sockets in a separate list
The 'connections' list was used only for tracking who is connected.
We can just call
QObject::findChildren<QTcpSocket *>
on the QTcpServer instance for that.
This also fixes the issue with removing data from the container while
iterating over it.
The old code was also removing the data from the container while
iterating over it, because calling socket->disconnectFromHost() from
a ranged-for loop results in sending a QTcpSocket::disconnected()
signal and immediately calling the connected lambda.
This patch fixes the issue.
Pick-to: 6.5 6.4 6.2
Change-Id: I24d8c197545162af232d438d0941faba6ffc94d0
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/serialbus')
-rw-r--r-- | src/serialbus/qmodbustcpserver.cpp | 4 | ||||
-rw-r--r-- | src/serialbus/qmodbustcpserver_p.h | 5 |
2 files changed, 3 insertions, 6 deletions
diff --git a/src/serialbus/qmodbustcpserver.cpp b/src/serialbus/qmodbustcpserver.cpp index 715c55c..ad7e2e5 100644 --- a/src/serialbus/qmodbustcpserver.cpp +++ b/src/serialbus/qmodbustcpserver.cpp @@ -98,7 +98,9 @@ void QModbusTcpServer::close() if (d->m_tcpServer->isListening()) d->m_tcpServer->close(); - for (auto socket : std::as_const(d->connections)) + const auto childSockets = + d->m_tcpServer->findChildren<QTcpSocket *>(Qt::FindDirectChildrenOnly); + for (auto socket : childSockets) socket->disconnectFromHost(); setState(QModbusDevice::UnconnectedState); diff --git a/src/serialbus/qmodbustcpserver_p.h b/src/serialbus/qmodbustcpserver_p.h index 0f1cbfe..f7c392f 100644 --- a/src/serialbus/qmodbustcpserver_p.h +++ b/src/serialbus/qmodbustcpserver_p.h @@ -99,8 +99,6 @@ public: return; } - connections.append(socket); - auto buffer = new QByteArray(); QObject::connect(socket, &QObject::destroyed, socket, [buffer]() { @@ -108,8 +106,6 @@ public: delete buffer; }); QObject::connect(socket, &QTcpSocket::disconnected, q, [socket, this]() { - connections.removeAll(socket); - Q_Q(QModbusTcpServer); emit q->modbusClientDisconnected(socket); socket->deleteLater(); @@ -193,7 +189,6 @@ public: } QTcpServer *m_tcpServer { nullptr }; - QList<QTcpSocket *> connections; std::unique_ptr<QModbusTcpConnectionObserver> m_observer; |