You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB
C++

#include "udpdatathread.h"
#include <QHostAddress>
#include "common.h"
#include <QDebug>
#define MAXSIZES 1024
UdpDataThread::UdpDataThread(QObject *parent)
: QObject(parent)
, m_workerThread(new QThread())
, m_udpWorker(nullptr)
, m_checkConnectTimer(new QTimer(this))
, m_connectStatus(false)
, m_isSending(false)
{
auto ipAddr = Common::GetInstance()->getHostAddress();
auto recvPort = Common::GetInstance()->getSrcPort();
m_hostAddr.setAddress(ipAddr);
m_udpWorker = new UdpSocketWorker(m_hostAddr, recvPort);
m_udpWorker->moveToThread(m_workerThread);
m_workerThread->start();
connect(m_udpWorker, &UdpSocketWorker::dataReceived, this, &UdpDataThread::recvData);
connect(m_checkConnectTimer, &QTimer::timeout, this, &UdpDataThread::checkConnectSlot);
m_checkConnectTimer->start(1000);
}
UdpDataThread::~UdpDataThread()
{
}
void UdpDataThread::sendSnId(const QString &data)
{
m_udpWorker->startSending("send_sn", data.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
}
void UdpDataThread::sendStop()
{
m_isSending = false;
}
void UdpDataThread::sendClose()
{
QString msg = QString("%1%2").arg(CONSTHEAD).arg("0301");
m_udpWorker->startSending("iecs_close", msg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
}
void UdpDataThread::stopClose()
{
m_udpWorker->stopSending("iecs_close");
}
void UdpDataThread::recvData(const QByteArray &data)
{
if (data.size() == 0)
return;
QString dataStr(data);
if (dataStr.mid(0, 2).toInt() != CONSTHEAD)
return;
int msgType = dataStr.mid(2, 2).toInt();
switch(msgType)
{
case SIGNAL_BEATSRC:
{
m_connectStatus = true;
int type = dataStr.mid(4, 2).toInt();
Common::GetInstance()->setCurrentWorkMode(type);
emit connectStatus(true);
break;
}
default:
break;
}
}
void UdpDataThread::checkConnectSlot()
{
static int times = 0;
if (!m_connectStatus)
{
times++;
}
else
{
times = 0;
}
if (times >= 3)
{
emit connectStatus(false);
qDebug()<<"disconnect";
times = 0;
}
m_connectStatus = false;
}