qt/LedOK/communication/network.cpp
2022-08-25 18:37:24 +08:00

127 lines
5.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "network.h"
#include <QMessageBox>
#include <QTime>
#include "string"
#include <QJsonObject>
#include <QJsonDocument>
#include <QMetaEnum>
#include <QNetworkDatagram>
NetComm::NetComm(QObject *p) : QObject(p) {
}
STREAM_PACKET NetComm::LedOkMakeStreamPacket(int iComType,int iLen,unsigned char *ucBuf,QString str)
{
STREAM_PACKET Temp;
memset(&Temp,0x00,sizeof(STREAM_PACKET));
if(iLen>2048)
return Temp;
memset(Temp.SyncHead,STREAM_SYNC_HEAD,STREAM_SYNC_HEAD_NUM);
memset(Temp.ucSerialCode,0x00,20);
char* ch;
QByteArray ba = str.toLatin1(); // must
ch=ba.data();
memcpy(Temp.ucSerialCode,ch,ba.size());
Temp.iLength=iLen;
Temp.ucCommType=iComType;
memcpy(Temp.pDataBuffer,ucBuf,iLen);
Temp.pDataBuffer[Temp.iLength]=
GetCheckCodeIn8(&Temp.ucCommType,Temp.iLength+sizeof(Temp.ucCommType)+sizeof(char)*20+sizeof(Temp.iLength));
return Temp;
}
void NetComm::RestoreLedCardIpByUdpCmd() {
//UDP多网卡广播
QList<QNetworkInterface> networkinterfaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interfaces, networkinterfaces) //networkinterfaces负责提供主机的IP地址和网络接口的列表
{
{
foreach (QNetworkAddressEntry entry, interfaces.addressEntries())//QNetworkAddressEntry存储了一个IP地址子网掩码和广播地址
{
entry.setBroadcast(QHostAddress::Broadcast);
QHostAddress broadcastAddress("255.255.255.255");
{
entry.setBroadcast(QHostAddress::Broadcast);
RESTORE_IP stTempIp;
memset(&stTempIp,0x00,sizeof(RESTORE_IP));
QString strNew=entry.ip().toString();
int itempOffset = strNew.lastIndexOf(".");
QString sstrNew=strNew.left(itempOffset)+".254";
if(sstrNew.length()<=20)
memcpy(stTempIp.cNewIp,sstrNew.toLatin1().data(),sstrNew.length());
QString strMask=entry.netmask().toString();
if(strMask.length()<=20)
memcpy(stTempIp.cMask,strMask.toLatin1().data(),strMask.length());
QString strGateway=entry.ip().toString();
if(strGateway.length()<=20)
memcpy(stTempIp.cGateway,strGateway.toLatin1().data(),strGateway.length());
if(strGateway.length()<=20)
memcpy(stTempIp.cDns,strGateway.toLatin1().data(),strGateway.length());
STREAM_PACKET tempStreadPacket=LedOkMakeStreamPacket(COMMAND_ONLY_RESET_CARD_IP,80,(unsigned char *)&stTempIp,"Broadcast!");
int iLenPacket=tempStreadPacket.iLength+sizeof(int)+STREAM_SYNC_HEAD_NUM*sizeof(unsigned char)+sizeof(char)+20*sizeof(char)+sizeof(char);/////除正文外的协议结构大小;
QUdpSocket *tempUdpSocket= new QUdpSocket(this);
if(!tempUdpSocket->bind(entry.ip())) break;
tempUdpSocket->writeDatagram((char *)&tempStreadPacket,iLenPacket,broadcastAddress,m_port);
}
}
}
}
}
void NetComm::JieXiQJsonObjectOfLedCardInfoOld(const QString &strId,const QString &strIp){
LedCard *pLedCard = new LedCard(this);//alahover 没有释放
pLedCard->m_strCardId=strId;
pLedCard->m_strCardIp=strIp;
for(int i=0;i<LedCardList.count();i++) if(LedCardList.at(i)->m_strCardId==pLedCard->m_strCardId ) {//如果已经存在在列表中
auto pOldLedCard = LedCardList.at(i);//获取到旧的对象指针
pOldLedCard->m_bOnLine = true;
if(pOldLedCard->CompareHTTPGetParam(pLedCard)==false) {//如果ip地址变化了
pOldLedCard->copyLedCardHTTPGetParam(pLedCard);
emit addLedCard(pOldLedCard);//发送信号到界面
} else {
//post失败的概率比较小所以这里是否需要平凡的更新不需要刻意屏蔽该操作改为在其他点击操作中进行
emit ClearHeartbeatFlagByUdp(pOldLedCard);
}
delete pLedCard;
return;
}
LedCardList.append(pLedCard);
emit addLedCard(pLedCard);//发送信号到界面
}
void NetComm::JieXiQJsonObjectOfLedCardInfo(const QJsonObject &subObj, const QString &strIp){
LedCard *pLedCard = new LedCard(this);//alahover 没有释放
pLedCard->m_strCardId=subObj["cardId"].toString();
if(! strIp.isEmpty()) pLedCard->m_strCardIp = strIp;
else {
pLedCard->m_strCardIp = subObj["newIp"].toString();
if(pLedCard->m_strCardIp.isEmpty()) pLedCard->m_strCardIp = subObj["ip"].toString();
}
pLedCard->m_iWidth=subObj["width"].toInt();
pLedCard->m_iHeight=subObj["height"].toInt();
pLedCard->starterversionName=subObj["versionName"].toString();
pLedCard->starterPort=subObj["starterPort"].toInt();
pLedCard->androidVersion=subObj["androidVersion"].toString();
pLedCard->iHttpGetId=subObj["id"].toInt();
pLedCard->androidHeight=subObj["androidHeight"].toInt();
pLedCard->androidWidth=subObj["androidWidth"].toInt();
pLedCard->lastActive=subObj["lastActive"].toInt();
for(int i=0;i<LedCardList.count();i++) if(LedCardList.at(i)->m_strCardId==pLedCard->m_strCardId ) { //如果已经存在在列表中
auto pOldLedCard = LedCardList.at(i);//获取到旧的对象指针
pOldLedCard->m_bOnLine = true;
if(! pOldLedCard->CompareHTTPGetParam(pLedCard)) {//如果ip地址变化了
pOldLedCard->copyLedCardHTTPGetParam(pLedCard);
emit addLedCard(pOldLedCard);//发送信号到界面
} else {
//post失败的概率比较小所以这里是否需要平凡的更新不需要刻意屏蔽该操作改为在其他点击操作中进行
emit ClearHeartbeatFlagByUdp(pOldLedCard);
}
delete pLedCard;
return;
}
LedCardList.append(pLedCard);
emit addLedCard(pLedCard);//发送信号到界面
}