qt/ledset/pcaprethread.cpp

126 lines
5.3 KiB
C++
Raw Normal View History

2023-03-20 11:30:19 +08:00
#include "pcaprethread.h"
2023-06-06 12:13:33 +08:00
#include "pcap.h"
#include "globalfunc.h"
2023-03-20 11:30:19 +08:00
PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) {
2023-06-06 12:13:33 +08:00
qRegisterMetaType<FuncIntByte>("FuncIntByte");
2023-03-20 11:30:19 +08:00
connect(this, &QThread::finished, this, &QThread::deleteLater);
2023-06-13 18:00:19 +08:00
connect(this, &PcapReThread::onCallback, this, [](FuncIntByte callback, int code, const QByteArray data) {
2023-06-06 12:13:33 +08:00
callback(code, data);
2023-03-20 11:30:19 +08:00
});
}
2023-06-06 12:13:33 +08:00
PcapReThread::~PcapReThread() {
pcap_close(pcap);
}
2023-09-01 17:25:53 +08:00
int PcapReThread::sendMsgNet(const byte *msg, int size, int id, qint64 timeout, FuncIntByte callback, bool isFront, WaitingDlg *waitingDlg) {
2023-06-06 12:13:33 +08:00
int res;
PcapResp *resp;
{
std::lock_guard<std::mutex> lock(mtx);
res = pcap_sendpacket(pcapSend, msg, size);
if(res==0) {
if(timeout==0) timeout = 30000;
timeout += std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
2023-09-01 17:25:53 +08:00
if(isFront) resps.prepend(resp = new PcapResp{callback, id, timeout});
else resps.append(resp = new PcapResp{callback, id, timeout});
2023-06-06 12:13:33 +08:00
}
}
if(waitingDlg) {
if(res) waitingDlg->close();
else {
QObject::connect(waitingDlg, &WaitingDlg::rejected, [=] {
std::lock_guard<std::mutex> lock(mtx);
for(int i=resps.size()-1; i>=0; i--) if(resps[i]==resp) {
resp->notCancelled = false;
break;
}
});
waitingDlg->showLater();
}
}
return res;
}
2023-09-01 16:17:33 +08:00
void PcapReThread::addMultiCallback(int id, FuncIntByte callback) {
std::lock_guard<std::mutex> lock(mtx);
resps.append(new PcapResp{callback, id, 0, true});
}
2023-03-20 11:30:19 +08:00
void PcapReThread::run() {
pcap_pkthdr *header;
2023-07-28 14:48:41 +08:00
const uchar *data;
2023-06-06 12:13:33 +08:00
int cnt;
while((cnt = pcap_next_ex(pcap, &header, &data)) >= 0) {
2023-06-13 18:00:19 +08:00
if(! resps.isEmpty()) {
2023-03-20 11:30:19 +08:00
std::lock_guard<std::mutex> lock(mtx);
2023-06-13 18:00:19 +08:00
if(cnt && data[0]==0x55 && data[1]==0x55) {
2023-09-01 16:17:33 +08:00
//modified by alahover -s 20230823
if(data[2]==0x00 && data[3]==0x11 && data[4]==0x22 && data[5]==0x33 && data[6]==0x44 && data[7]==0x55 ) {
2023-09-01 17:25:53 +08:00
int id = data[headMap_zrf.protcolFlag]<<16 | data[headMap_zrf.protcolFlag+1]<<8 | data[headMap_zrf.pkgType];
if(id == 0x105B14) for(int i=0; i<resps.size(); i++) if(resps[i]->id==id) {
auto resp = resps[i];
if(resp->notCancelled) emit onCallback(resp->callback, 0, QByteArray((char*)data, header->caplen));
if(! resp->isMulti) {
resps.removeAt(i);
delete resp;
}
break;
}
2023-09-01 16:17:33 +08:00
} else {
int id = data[2]<<8 | data[3];
for(int i=0; i<resps.size(); i++) if(resps[i]->id==id) {
auto resp = resps.takeAt(i);
if(resp->notCancelled) emit onCallback(resp->callback, 0, QByteArray((char*)data, header->caplen));
delete resp;
break;
}
2023-05-27 17:43:57 +08:00
}
}
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
2023-06-06 12:13:33 +08:00
for(int i=0; i<resps.size(); i++) if(resps[i]->timeout <= now && resps[i]->timeout > 0) {
2023-03-20 11:30:19 +08:00
auto resp = resps.takeAt(i);
2023-06-13 18:00:19 +08:00
if(resp->notCancelled) emit onCallback(resp->callback, 5, QByteArray());
2023-06-06 12:13:33 +08:00
delete resp;
2023-03-20 11:30:19 +08:00
}
}
2023-06-13 18:00:19 +08:00
if(status==1) {
2023-07-28 14:48:41 +08:00
if(cnt == 0 || header->caplen < intro.size()) continue;
for(int i=0; i<intro.size(); i++) if((uchar)intro[i]!=data[i]) goto conti;
2023-06-13 18:00:19 +08:00
QString msg;
msg.append(QDateTime::fromSecsSinceEpoch(header->ts.tv_sec).toString("yy-MM-dd HH:mm:ss.")+QString::number(header->ts.tv_usec))
.append(" len:").append(QString::number(header->caplen)).append("/").append(QString::number(header->len)).append("\n");
if(fmt==1) {
char line[LineLen + 1];
int l = 0;
for(uint i=0; i<header->caplen; i++) {
msg.append(QString::asprintf("%.2X ", data[i]));
if(i > 13) {
if(isgraph(data[i]) || data[i] == ' ') line[l] = data[i];
else line[l] = '.';
if(l==15) {
line[16] = 0;
msg.append(" ").append(line).append("\n");
l = 0;
} else l++;
} else if(i == 13) {
msg.append("\n");
}
}
line[l] = 0;
msg.append(" ").append(line);
} else {
for(uint i=0; i<header->caplen; i++) {
if(i>headMap.end) msg.append(' ');
else if(sepas[i]) msg.append(sepas[i]);
msg.append(QString::asprintf("%.2X", data[i]));
}
}
msg.append("\n\n");
emit onMsg(msg);
}
2023-07-28 14:48:41 +08:00
conti:;
2023-03-20 11:30:19 +08:00
}
emit onError(pcap_geterr(pcap));
}