发送线程中的对象从指针改为实例,方便释放内存

main
Jinhuan 2 weeks ago
parent a8d4a359c4
commit 150db6c345

@ -11,8 +11,8 @@ void threadReceive::stop()
{ {
isLoop = false; isLoop = false;
BOOL bDontLinger = FALSE; BOOL bDontLinger = FALSE;
mSocket->abort(); mSocket.abort();
filemSocket->abort(); filemSocket.abort();
// wait(); // wait();
} }
@ -20,27 +20,25 @@ void threadReceive::init(std::string IP, int port)
{ {
ip = QString::fromStdString(IP); ip = QString::fromStdString(IP);
mSocket = new QUdpSocket(this); mSocket.bind(port + NumberOfSupportedCameras * 2 + 1, QUdpSocket::ShareAddress);
mSocket->bind(port + NumberOfSupportedCameras * 2 + 1, QUdpSocket::ShareAddress); connect(&mSocket, &QUdpSocket::readyRead, this, &threadReceive::processPendingDatagram);
connect(mSocket, &QUdpSocket::readyRead, this, &threadReceive::processPendingDatagram);
filemSocket = new QUdpSocket(this); filemSocket.bind(port + NumberOfSupportedCameras * 2 + 2, QUdpSocket::ShareAddress);
filemSocket->bind(port + NumberOfSupportedCameras * 2 + 2, QUdpSocket::ShareAddress); connect(&filemSocket, &QUdpSocket::readyRead, this, &threadReceive::fileprocessPendingDatagram);
connect(filemSocket, &QUdpSocket::readyRead, this, &threadReceive::fileprocessPendingDatagram);
} }
void threadReceive::processPendingDatagram() void threadReceive::processPendingDatagram()
{ {
// 拥有等待的数据报 // 拥有等待的数据报
while (mSocket->hasPendingDatagrams()) while (mSocket.hasPendingDatagrams())
{ {
QByteArray datagram; QByteArray datagram;
// 让datagram的大小为等待处理的数据报的大小这样才能接收到完整的数据 // 让datagram的大小为等待处理的数据报的大小这样才能接收到完整的数据
datagram.resize(mSocket->pendingDatagramSize()); datagram.resize(mSocket.pendingDatagramSize());
// 接收数据报将其存放到datagram中 // 接收数据报将其存放到datagram中
mSocket->readDatagram(datagram.data(), datagram.size()); mSocket.readDatagram(datagram.data(), datagram.size());
QString data = datagram; QString data = datagram;
emit sendMsgToCigratte(data); emit sendMsgToCigratte(data);
@ -52,14 +50,14 @@ void threadReceive::fileprocessPendingDatagram()
QString filename; QString filename;
std::fstream file; std::fstream file;
// 拥有等待的数据报 // 拥有等待的数据报
while (filemSocket->hasPendingDatagrams()) while (filemSocket.hasPendingDatagrams())
{ {
QByteArray datagram; QByteArray datagram;
// 让datagram的大小为等待处理的数据报的大小这样才能接收到完整的数据 // 让datagram的大小为等待处理的数据报的大小这样才能接收到完整的数据
datagram.resize(filemSocket->pendingDatagramSize()); datagram.resize(filemSocket.pendingDatagramSize());
// 接收数据报将其存放到datagram中 // 接收数据报将其存放到datagram中
filemSocket->readDatagram(datagram.data(), datagram.size()); filemSocket.readDatagram(datagram.data(), datagram.size());
QString data = datagram; QString data = datagram;
QString str = data.mid(0, 4); QString str = data.mid(0, 4);

@ -40,6 +40,6 @@ signals:
public: public:
std::atomic_bool isLoop = { 0 }; std::atomic_bool isLoop = { 0 };
QUdpSocket* mSocket; QUdpSocket mSocket;
QUdpSocket* filemSocket; QUdpSocket filemSocket;
}; };

@ -1,8 +1,5 @@
#include "threadSendMqtt.h" #include "threadSendMqtt.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h> #include <stdio.h>
#include <qtcpsocket.h>
// 相关头文件 // 相关头文件
#include <QFile> // 文件操作 #include <QFile> // 文件操作
@ -27,25 +24,19 @@ void threadSendMqtt::stop()
{ {
isLoop = false; isLoop = false;
wait(); wait();
delete m_client;
} }
bool threadSendMqtt::connectTCP() { bool threadSendMqtt::connectMqtt() {
connect(&m_client, &QMqttClient::connected, this, [this](void) {
m_client = new QMqttClient(this); qDebug() << "Mqtt connected";
m_client->setHostname(ip); });
m_client->setPort(port); connect(&m_client, &QMqttClient::disconnected, this, [this](void) {
//m_client->setUsername(userName); qDebug() << "Mqtt disconnected";
//m_client->setPassword(userPassword); });
m_client->connectToHost(); connect(&m_client, &QMqttClient::errorChanged, this, [this](QMqttClient::ClientError error) {
//connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange); qDebug() << error;
});
connect(m_client, &QMqttClient::connected, this, [this](void) {qDebug() << "Mqtt connected";}); connect(&m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
connect(m_client, &QMqttClient::disconnected, this, [this](void) {qDebug() << "Mqtt disconnected"; });
connect(m_client, SIGNAL(messageSent(qint32)), this, SLOT(MQTT_DATASEND_SUCCESS(qint32)));//消息发送成功提示的槽函数绑定
connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
const QString content = QDateTime::currentDateTime().toString() const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" Received Topic: ") + QLatin1String(" Received Topic: ")
+ topic.name() + topic.name()
@ -54,30 +45,40 @@ bool threadSendMqtt::connectTCP() {
+ QLatin1Char('\n'); + QLatin1Char('\n');
//ui->editLog->insertPlainText(content); //ui->editLog->insertPlainText(content);
}); });
connect(&m_client, &QMqttClient::pingResponseReceived, this, [this]() {
connect(m_client, &QMqttClient::pingResponseReceived, this, [this]() {
const QString content = QDateTime::currentDateTime().toString() const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" PingResponse") + QLatin1String(" PingResponse")
+ QLatin1Char('\n'); + QLatin1Char('\n');
//ui->editLog->insertPlainText(content); //ui->editLog->insertPlainText(content);
}); });
qDebug() << m_client.state();
//connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname); if (m_client.state() == QMqttClient::Disconnected)
//connect(ui->spinBoxPort, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::setClientPort); {
m_client.setHostname("192.168.3.6");
m_client.setClientId("123");
m_client.setPort(1883);
//m_client.setUsername(userName);
//m_client.setPassword(userPassword);
m_client.connectToHost();
}
return true; return true;
} }
void threadSendMqtt::run() void threadSendMqtt::run()
{ {
if (!connectTCP()) qDebug() << m_client.state();
if (!connectMqtt())
qDebug() << "Mqtt connect error!"; qDebug() << "Mqtt connect error!";
while (isLoop) { while (isLoop) {
_MqttSendInfo TCPSendInfo; qDebug() << m_client.error();
qDebug() << m_client.state();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
/*_MqttSendInfo TCPSendInfo;
Local_MQTT_Info_queue->take(TCPSendInfo); Local_MQTT_Info_queue->take(TCPSendInfo);
num++; num++;
sendData(&TCPSendInfo, num); sendData(&TCPSendInfo, num);*/
} }
} }
@ -89,9 +90,9 @@ void threadSendMqtt::sendData(_MqttSendInfo* TCPSendInfo, int Num) {
addressObject["country"] = "USA"; addressObject["country"] = "USA";
if (m_client->state() == QMqttClient::Connected) { if (m_client.state() == QMqttClient::Connected) {
QJsonDocument jsonDoc(addressObject); QJsonDocument jsonDoc(addressObject);
QByteArray jsonBytes = jsonDoc.toJson(); QByteArray jsonBytes = jsonDoc.toJson();
auto result = m_client->publish(QMqttTopicName("topic"), jsonBytes, 0, true); auto result = m_client.publish(QMqttTopicName("topic"), jsonBytes, 0, true);
} }
} }

@ -39,7 +39,7 @@ public:
wait(); wait();
} }
void stop(); void stop();
bool connectTCP(); bool connectMqtt();
protected: protected:
void run(); void run();
@ -52,5 +52,5 @@ public:
public: public:
SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue; SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue;
std::atomic_bool isLoop = { 0 }; std::atomic_bool isLoop = { 0 };
QMqttClient* m_client = NULL; QMqttClient m_client;
}; };

Loading…
Cancel
Save