33 lines
988 B
C++
33 lines
988 B
C++
#include "pcaprethread.h"
|
|
|
|
PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) {
|
|
qRegisterMetaType<Resp>("Resp");
|
|
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-4));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
emit onError(pcap_geterr(pcap));
|
|
}
|
|
|
|
|