qt/ledset/pcaprethread.cpp
2023-07-28 14:48:41 +08:00

108 lines
4.2 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::onCallback, 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 uchar *data;
int cnt;
while((cnt = pcap_next_ex(pcap, &header, &data)) >= 0) {
if(! resps.isEmpty()) {
std::lock_guard<std::mutex> lock(mtx);
if(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 onCallback(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 onCallback(resp->callback, 5, QByteArray());
delete resp;
}
}
if(status==1) {
if(cnt == 0 || header->caplen < intro.size()) continue;
for(int i=0; i<intro.size(); i++) if((uchar)intro[i]!=data[i]) goto conti;
QString msg;
msg.append(QDateTime::fromSecsSinceEpoch(header->ts.tv_sec).toString("yy-MM-dd HH:mm:ss.")+QString::number(header->ts.tv_usec))
.append(" len:").append(QString::number(header->caplen)).append("/").append(QString::number(header->len)).append("\n");
if(fmt==1) {
char line[LineLen + 1];
int l = 0;
for(uint i=0; i<header->caplen; i++) {
msg.append(QString::asprintf("%.2X ", data[i]));
if(i > 13) {
if(isgraph(data[i]) || data[i] == ' ') line[l] = data[i];
else line[l] = '.';
if(l==15) {
line[16] = 0;
msg.append(" ").append(line).append("\n");
l = 0;
} else l++;
} else if(i == 13) {
msg.append("\n");
}
}
line[l] = 0;
msg.append(" ").append(line);
} else {
for(uint i=0; i<header->caplen; i++) {
if(i>headMap.end) msg.append(' ');
else if(sepas[i]) msg.append(sepas[i]);
msg.append(QString::asprintf("%.2X", data[i]));
}
}
msg.append("\n\n");
emit onMsg(msg);
}
conti:;
}
emit onError(pcap_geterr(pcap));
}