qt/ledset/pcaprethread.cpp
2023-06-06 12:13:33 +08:00

74 lines
2.6 KiB
C++

#include "pcaprethread.h"
#include "pcap.h"
#include "globalfunc.h"
PcapReThread::PcapReThread(pcap_t *pcap) : pcap(pcap) {
qRegisterMetaType<FuncIntByte>("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<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();
resps.append(resp = new PcapResp{callback, id, timeout});
}
}
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;
}
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<std::mutex> lock(mtx);
if(status==0 && cnt && 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);
if(resp->notCancelled) emit onMsg(resp->callback, 0, QByteArray((char*)data, header->caplen));
delete resp;
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) {
auto resp = resps.takeAt(i);
if(resp->notCancelled) emit onMsg(resp->callback, 5, QByteArray());
delete resp;
}
}
}
emit onError(pcap_geterr(pcap));
}