#include "pcaprethread.h" #include "pcap.h" #include "globalfunc.h" PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) { qRegisterMetaType("FuncIntByte"); connect(this, &QThread::finished, this, &QThread::deleteLater); connect(this, &PcapReThread::onMsg, this, [](FuncIntByte callback, int code, const QByteArray data) { callback(code, data); }); } PcapReThread::~PcapReThread() { pcap_close(pcap); } int PcapReThread::sendMsgNet(const byte *msg, int size, int id, qint64 timeout, FuncIntByte callback, WaitingDlg *waitingDlg) { int res; PcapResp *resp; { std::lock_guard lock(mtx); res = pcap_sendpacket(pcapSend, msg, size); if(res==0) { if(timeout==0) timeout = 30000; timeout += std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); resps.append(resp = new PcapResp{callback, id, timeout}); } } if(waitingDlg) { if(res) waitingDlg->close(); else { QObject::connect(waitingDlg, &WaitingDlg::rejected, [=] { std::lock_guard lock(mtx); for(int i=resps.size()-1; i>=0; i--) if(resps[i]==resp) { resp->notCancelled = false; break; } }); waitingDlg->showLater(); } } return res; } void PcapReThread::run() { pcap_pkthdr *header; const u_char *data; int cnt; while((cnt = pcap_next_ex(pcap, &header, &data)) >= 0) { if(status==2) return; if(resps.isEmpty()) continue; { std::lock_guard lock(mtx); if(status==0 && cnt && data[0]==0x55 && data[1]==0x55) { int id = data[2]<<8 | data[3]; for(int i=0; iid==id) { auto resp = resps.takeAt(i); if(resp->notCancelled) emit onMsg(resp->callback, 0, QByteArray((char*)data, header->caplen)); delete resp; break; } } auto now = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); for(int i=0; itimeout <= now && resps[i]->timeout > 0) { auto resp = resps.takeAt(i); if(resp->notCancelled) emit onMsg(resp->callback, 5, QByteArray()); delete resp; } } } emit onError(pcap_geterr(pcap)); }