qt/ledset/pcaprethread.cpp

40 lines
1.4 KiB
C++
Raw Normal View History

2023-03-20 11:30:19 +08:00
#include "pcaprethread.h"
PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) {
2023-03-20 13:41:36 +08:00
qRegisterMetaType<Resp>("Resp");
2023-03-20 11:30:19 +08:00
connect(this, &QThread::finished, this, &QThread::deleteLater);
connect(this, &PcapReThread::onMsg, this, [](Resp resp, const QByteArray data) {
resp.callback(data);
});
}
void PcapReThread::run() {
pcap_pkthdr *header;
const u_char *data;
int res;
while((res = pcap_next_ex(pcap, &header, &data)) >= 0) {
if(status==2) return;
2023-05-27 17:43:57 +08:00
if(resps.isEmpty()) continue;
2023-03-20 11:30:19 +08:00
{
std::lock_guard<std::mutex> lock(mtx);
2023-05-27 17:43:57 +08:00
if(status==0 && res && data[0]==0x55 && data[1]==0x55) {
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);
emit onMsg(resp, QByteArray((char*)data, header->caplen));
break;
}
}
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
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-05-27 17:43:57 +08:00
resp.timeout = -5;
emit onMsg(resp, QByteArray());
2023-03-20 11:30:19 +08:00
}
}
}
emit onError(pcap_geterr(pcap));
}