优化线程退出时资源的释放问题

This commit is contained in:
2024-09-12 15:18:09 +08:00
parent 843b2058c3
commit 9d506e1cfc
314 changed files with 2448 additions and 529454 deletions

View File

@@ -4,22 +4,12 @@
#include "ImageEnrollWindow.h"
#include "PasswordEnrollWindow.h"
#if TEST_TCP_MOVE_TO_MAIN
ClientHandler::ClientHandler(QTcpSocket* socket,
QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig,
QJsonArray backBoardDevInfoJson, QJsonArray backBoardUuidJson,
QJsonArray getPicJson, QJsonArray getVideoJson, int clientId, int isBackBoardOrAllBoard, QObject* parent)
: QObject(parent), socket(socket),
#else
ClientHandler::ClientHandler(QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig,
QJsonArray backBoardDevInfoJson, QJsonArray backBoardUuidJson,
QJsonArray getPicJson, QJsonArray getVideoJson, int clientId, int isBackBoardOrAllBoard, QObject* parent)
: QObject(parent),
#endif
frontBoardOneClickTest(frontBoardOneClickTest), frontBoardTest(frontBoardTest),
frontBoardFuncConfig(frontBoardFuncConfig), frontBoardDevInfoJson(frontBoardDevInfoJson),
frontBoardLicenseJson(frontBoardLicenseJson),
@@ -33,15 +23,8 @@ ClientHandler::ClientHandler(QJsonArray frontBoardOneClickTest, QJsonArray front
currentFrontBoardIndex(0), currentLicenseIndex(0), currentUuidIndex(0),
currentBackBoardIndex(0)
{
#if TEST_TCP_MOVE_TO_MAIN
connect(socket, &QTcpSocket::readyRead, this, &ClientHandler::onDataReceived);
connect(socket, &QTcpSocket::disconnected, this, &ClientHandler::onDisconnected);
qint64 bufferSize = socket->socketOption(QAbstractSocket::ReceiveBufferSizeSocketOption).toLongLong();
#endif
}
#if !TEST_TCP_MOVE_TO_MAIN
void ClientHandler::initialize(QTcpSocket* socket)
{
this->socket = socket;
@@ -49,10 +32,10 @@ void ClientHandler::initialize(QTcpSocket* socket)
connect(socket, &QTcpSocket::disconnected, this, &ClientHandler::onDisconnected);
// 其他的 socket 操作都将在这个线程中进行
}
#endif
ClientHandler::~ClientHandler()
{
socket->deleteLater();
qDebug() << "ClientHandler destroyed for clientId:" << clientId;
}
@@ -65,9 +48,9 @@ void ClientHandler::run()
// 重置索引
void ClientHandler::resetCurrentItemIndex()
{
currentItemIndex = 0;
currentItemIndex = 0;
currentFuncItemIndex = 0;
itemsProcessedCount = 0;
itemsProcessedCount = 0;
emit startTimeout(0);
}
@@ -84,7 +67,6 @@ void ClientHandler::onDisconnected() {
qDebug() << "> A device is disconnected. ID:" << clientId;
qDebug() << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
emit clientDisconnected(this);
socket->deleteLater();
}
void ClientHandler::startReadVideoDataTimer(int client_Id)
@@ -475,7 +457,7 @@ void ClientHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemInd
isPowerOnSend = true;
qDebug() << "itemIndex" << itemIndex;
qDebug() << "jsonItem.size()" << jsonItem.size();
currentItem = jsonItem;
QString itemData = QJsonDocument(jsonItem).toJson();
//emit sendData(itemData.toUtf8());
if (isBackBoardOrAllBoard) {
@@ -503,7 +485,7 @@ void ClientHandler::sendDevInfoItem()
currentJson = backBoardDevInfoJson;
currentJsonItem = currentBackBoardIndex;
if (currentBackBoardIndex < backBoardDevInfoJson.size()) {
sendDevInfoJsonItem(backBoardDevInfoJson[currentBackBoardIndex++].toObject(), 1);
sendDevInfoJsonItem(backBoardDevInfoJson[currentBackBoardIndex ++].toObject(), 1);
}
else
isPowerOnSend = false;
@@ -513,8 +495,7 @@ void ClientHandler::sendDevInfoItem()
currentJson = frontBoardDevInfoJson;
currentJsonItem = currentFrontBoardIndex;
if (currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
sendDevInfoJsonItem(frontBoardDevInfoJson[currentFrontBoardIndex].toObject(), 1);
currentFrontBoardIndex++;
sendDevInfoJsonItem(frontBoardDevInfoJson[currentFrontBoardIndex ++].toObject(), 1);
}
else
isPowerOnSend = false;
@@ -632,7 +613,7 @@ bool ClientHandler::RNDISFirstData(QByteArray& data) {
socket->setReadBufferSize(20 * 1024);
controlClientId = clientId;
// 前板有设备连接自动发送获取设备信息
//sendDevInfoItem();
sendDevInfoItem();
break;
default:
qWarning() << "Unhandled client type:" << clientType;
@@ -691,8 +672,6 @@ void ClientHandler::onDataReceived()
//qDebug() << "isClickedSend:" << isClickedSend;
//qDebug() << "isSingleSend:" << isSingleSend;
//qDebug() << "isRecvImgData:" << isRecvImgData;
/*if (!isRecvVideoData &&
(isRecvImgData || isPowerOnSend || isClickedSend || (isSingleSend && (currentItemIndex < frontBoardTest.size())))) {*/
if (!isRecvVideoData &&
(isRecvImgData || isPowerOnSend || isClickedSend || (isSingleSend && (currentItemIndex < currentJson.size())))) {
QByteArray allData;
@@ -800,16 +779,13 @@ void ClientHandler::processPendingData()
/*qDebug() << "--1--Received data size:" << buffer.size() << "clientId:" << clientId
<< "at" << QDateTime::currentDateTime().toString(Qt::ISODate)
<< "bytesAvailable after read:" << bytesAvailableAfter; */
if ((isBackBoardOrAllBoard == 0) && isFirstDataReceived) {
RNDISFirstData(buffer);
isFirstDataReceived = false;
}
if (bytesAvailableAfter > 9 * 1024) {
goto RETRY;
}
// 检查是否bytesAvailable没有变化避免无限循环
if (bytesAvailableBefore == bytesAvailableAfter) {
qCritical() << "Bytes available did not change after read, possible infinite loop for clientId:" << clientId;
@@ -858,7 +834,7 @@ void ClientHandler::onTimeout()
{
QMutexLocker locker(&mutex);
emit statusUpdated(getClientAddress(), false, currentJson, currentJsonItem);
qDebug() << "isSingleSend:" << isSingleSend << "isPowerOnSend:" << isPowerOnSend;
//qDebug() << "isSingleSend:" << isSingleSend << "isPowerOnSend:" << isPowerOnSend;
if (!isSingleSend && !isPowerOnSend) {
currentItemIndex ++;
itemsProcessedCount ++;
@@ -873,7 +849,6 @@ void ClientHandler::onTimeout()
sendNextItem(currentJson);
}
else if (currentItemIndex >= backBoardOneClickTest.size()) {
qDebug() << "All backBoardOneClickTest items processed in onTimeout().";
emit allItemsProcessed(getClientAddress(), itemsProcessedCount);
}
}
@@ -882,7 +857,6 @@ void ClientHandler::onTimeout()
sendNextItem(currentJson);
}
else if (currentItemIndex >= frontBoardOneClickTest.size()) {
qDebug() << "All frontBoardOneClickTest items processed in onTimeout().";
emit allItemsProcessed(getClientAddress(), itemsProcessedCount);
}
}
@@ -893,7 +867,6 @@ void ClientHandler::onTimeout()
sendDevInfoItem();
}
else {
qDebug() << "All items processed in onTimeout.";
emit allItemsProcessed(getClientAddress(), currentBackBoardIndex);
currentFrontBoardIndex = 0;
currentBackBoardIndex = 0;
@@ -905,7 +878,6 @@ void ClientHandler::onTimeout()
sendDevInfoItem();
}
else {
qDebug() << "All items processed in onTimeout.";
emit allItemsProcessed(getClientAddress(), currentFrontBoardIndex);
currentFrontBoardIndex = 0;
currentBackBoardIndex = 0;

View File

@@ -129,6 +129,7 @@ void Server::sendMessageToAll(const Message &message)
{
QByteArray packet;
toPacket(message, packet);
//qDebug() << "packet:" << packet;
qint64 sentBytes = d->ipv4Socket.writeDatagram(packet, MdnsIpv4Address, MdnsPort);
qDebug() << "ipv4Socket sentBytes:" << sentBytes;
sentBytes = d->ipv6Socket.writeDatagram(packet, MdnsIpv6Address, MdnsPort);