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

main
Jinhuan 2 weeks ago
parent a8d4a359c4
commit 150db6c345

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

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

@ -1,8 +1,5 @@
#include "threadSendMqtt.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <qtcpsocket.h>
// 相关头文件
#include <QFile> // 文件操作
@ -27,25 +24,19 @@ void threadSendMqtt::stop()
{
isLoop = false;
wait();
delete m_client;
}
bool threadSendMqtt::connectTCP() {
m_client = new QMqttClient(this);
m_client->setHostname(ip);
m_client->setPort(port);
//m_client->setUsername(userName);
//m_client->setPassword(userPassword);
m_client->connectToHost();
//connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
connect(m_client, &QMqttClient::connected, this, [this](void) {qDebug() << "Mqtt connected";});
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) {
bool threadSendMqtt::connectMqtt() {
connect(&m_client, &QMqttClient::connected, this, [this](void) {
qDebug() << "Mqtt connected";
});
connect(&m_client, &QMqttClient::disconnected, this, [this](void) {
qDebug() << "Mqtt disconnected";
});
connect(&m_client, &QMqttClient::errorChanged, this, [this](QMqttClient::ClientError error) {
qDebug() << error;
});
connect(&m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" Received Topic: ")
+ topic.name()
@ -54,30 +45,40 @@ bool threadSendMqtt::connectTCP() {
+ QLatin1Char('\n');
//ui->editLog->insertPlainText(content);
});
connect(m_client, &QMqttClient::pingResponseReceived, this, [this]() {
connect(&m_client, &QMqttClient::pingResponseReceived, this, [this]() {
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" PingResponse")
+ QLatin1Char('\n');
//ui->editLog->insertPlainText(content);
});
//connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname);
//connect(ui->spinBoxPort, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::setClientPort);
qDebug() << m_client.state();
if (m_client.state() == QMqttClient::Disconnected)
{
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;
}
void threadSendMqtt::run()
{
if (!connectTCP())
qDebug() << m_client.state();
if (!connectMqtt())
qDebug() << "Mqtt connect error!";
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);
num++;
sendData(&TCPSendInfo, num);
sendData(&TCPSendInfo, num);*/
}
}
@ -89,9 +90,9 @@ void threadSendMqtt::sendData(_MqttSendInfo* TCPSendInfo, int Num) {
addressObject["country"] = "USA";
if (m_client->state() == QMqttClient::Connected) {
if (m_client.state() == QMqttClient::Connected) {
QJsonDocument jsonDoc(addressObject);
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();
}
void stop();
bool connectTCP();
bool connectMqtt();
protected:
void run();
@ -52,5 +52,5 @@ public:
public:
SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue;
std::atomic_bool isLoop = { 0 };
QMqttClient* m_client = NULL;
QMqttClient m_client;
};

Loading…
Cancel
Save