qt/ledset/pcaprethread.cpp
2023-03-20 11:30:19 +08:00

32 lines
949 B
C++

#include "pcaprethread.h"
PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) {
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;
if(status==1 || res == 0) continue; //超时
if(data[0]!=0x55 || data[1]!=0x55 ) continue;
int id = data[2]<<8 | data[3];
{
std::lock_guard<std::mutex> lock(mtx);
for(int i=0; i<resps.size(); i++) if(resps[i].id==id) {
auto resp = resps.takeAt(i);
emit onMsg(resp, QByteArray((char*)(data+4), header->caplen));
break;
}
}
}
emit onError(pcap_geterr(pcap));
}