diff --git a/ledset/imgs/checkbox-check.png b/ledset/imgs/checkbox-check.png index d64dcfd..bfad3a9 100644 Binary files a/ledset/imgs/checkbox-check.png and b/ledset/imgs/checkbox-check.png differ diff --git a/ledset/imgs/checkbox-un.png b/ledset/imgs/checkbox-un.png index bfad3a9..d64dcfd 100644 Binary files a/ledset/imgs/checkbox-un.png and b/ledset/imgs/checkbox-un.png differ diff --git a/ledset/ledset.pro b/ledset/ledset.pro index 073a6cb..ff3b620 100644 --- a/ledset/ledset.pro +++ b/ledset/ledset.pro @@ -36,7 +36,6 @@ SOURCES += \ gqt.cpp \ main.cpp \ mainwin.cpp \ - pcapthread.cpp \ pcapwin.cpp \ screenunit.cpp \ table.cpp \ @@ -53,7 +52,6 @@ HEADERS += \ globalfunc.h \ gqt.h \ mainwin.h \ - pcapthread.h \ pcapwin.h \ screenunit.h \ table.h \ diff --git a/ledset/main.cpp b/ledset/main.cpp index aeb3899..5723e6f 100644 --- a/ledset/main.cpp +++ b/ledset/main.cpp @@ -114,71 +114,6 @@ QSlider::handle:horizontal {width: 8px;} return a.exec(); } -#define HAVE_REMOTE -#include "pcapthread.h" - void test() { - pcap_if_t *devs; - char errbuf[PCAP_ERRBUF_SIZE]; - if(pcap_findalldevs(&devs, errbuf) == -1) { - qDebug() << "pcap_findalldevs" << errbuf; - return; - } - pcap_if_t *device = 0; - for(pcap_if_t *dev = devs; dev; dev = dev->next) { - qDebug() << dev->name << "[desc]" << dev->description << "Loopback" << ((dev->flags & PCAP_IF_LOOPBACK)?"yes":"no"); - pcap_addr_t *a; - for(a=dev->addresses; a; a=a->next) { - auto sa_family = a->addr->sa_family; - if(sa_family==AF_INET) { - u_char *ip = (u_char *) &((sockaddr_in *) a->addr)->sin_addr.s_addr; - qDebug(" INET %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - if(a->netmask) { - u_char *ip = (u_char *) &((sockaddr_in *) a->netmask)->sin_addr.s_addr; - qDebug(" netmask %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - } - if(a->broadaddr) { - u_char *ip = (u_char *) &((sockaddr_in *) a->broadaddr)->sin_addr.s_addr; - qDebug(" Broadcast Address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - } - if(a->dstaddr) { - u_char *ip = (u_char *) &((sockaddr_in *) a->dstaddr)->sin_addr.s_addr; - qDebug(" Destination Address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - } - if(device==0) device = dev; - } else if(sa_family==AF_INET6) { - qDebug() << " INET6"; - //printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str))); - } else qDebug() << " Address Family" << sa_family; - } - qDebug(); - } - auto pcap = pcap_open_live(device->name, 65536, 1, 1000, errbuf); - if(pcap == 0) { - qDebug("Error openning adapter: %s\n", errbuf); - return; - } - //检查链路层的类型 - if (pcap_datalink(pcap) != DLT_EN10MB) { - qDebug("This program only run on Ethernet networks\n"); -// pcap_close(fp); -// pcap_freealldevs(alldevs); - } - auto capthd = new CapThread(pcap); - capthd->start(); - QThread::sleep(1); - u_char data[] = {0x55, 0x55, - 0x9A, 0x20, 0x00, 0x20, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00}; - if(pcap_sendpacket(pcap, data, sizeof(data))) { - qDebug()< #include "table.h" #include "globalfunc.h" -#include #include "basewin.h" #include #include @@ -102,11 +101,8 @@ MainWin::MainWin() { }); btnImg = addImg(imgsBar, QPixmap(":/imgs/video.png").scaledToWidth(128, Qt::SmoothTransformation), tr("协议调试")); connect(btnImg, &ImgBtn::clicked, this, [=](){ - if(pcapRe != 0 && pcapSend != 0) - { - auto ins = new PcapWin(pcapRe, pcapSend, this); - if(ins) ins->show(); - } + auto ins = PcapWin::newIns(net_name, this); + if(ins) ins->show(); }); btnImg = addImg(imgsBar, QPixmap(":/imgs/idea.png").scaledToWidth(128, Qt::SmoothTransformation), tr("模拟同步")); connect(btnImg, &ImgBtn::clicked, this, [=](){ diff --git a/ledset/mainwin.h b/ledset/mainwin.h index 350c6e0..0442dbc 100644 --- a/ledset/mainwin.h +++ b/ledset/mainwin.h @@ -2,9 +2,11 @@ #define MAINWIN_H #include "basewin.h" -#define HAVE_REMOTE -#include "pcapthread.h" #include "table.h" +#define HAVE_REMOTE +#include +#include "pcap.h" +#include class MainPcapThread : public QThread { Q_OBJECT diff --git a/ledset/pcapthread.cpp b/ledset/pcapthread.cpp deleted file mode 100644 index 6851123..0000000 --- a/ledset/pcapthread.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "pcapthread.h" -#include -#include - -CapThread::CapThread(struct pcap *pcap, QObject *parent) : QThread(parent), pcap(pcap) { - connect(this, &QThread::finished, this, &QThread::deleteLater); -} - -void CapThread::run() { - pcap_pkthdr *header; - const u_char *data; - int res; - while((res = pcap_next_ex(pcap, &header, &data)) >= 0) { - if(res == 0) continue; //超时 - packet.time = QDateTime::fromSecsSinceEpoch(header->ts.tv_sec).toString("yy-MM-dd HH:mm:ss.")+QString::number(header->ts.tv_usec); - packet.len = header->len; - packet.caplen = header->caplen; - //输出编号、时间戳和包长度 - qDebug()<caplen<<"/"<len; - QString data_str; - char line[LINE_LEN + 1]; - int l = 0; - for(uint i=0; icaplen; i++) { - data_str.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'; - data_str.append(" "); - data_str.append(line); - data_str.append("\n"); - memset(line, 0, LINE_LEN); - l = 0; - } else l++; - } else if(i == 13) { - data_str.append("\n"); - } - } - packet.data = data_str; - qDebug()< -#include -#include "pcap.h" -#include - -#pragma comment(lib, "wpcap.lib") -#pragma comment(lib, "Ws2_32.lib") - -#define LINE_LEN 16 -#define MAX_ADDR_LEN 16 - -struct Packet { - QString time; - u_int len; - u_int caplen; - QString data; -}; - -class CapThread : public QThread { - Q_OBJECT -public: - explicit CapThread(pcap *pcap, QObject *parent = nullptr); - pcap *pcap{0}; - Packet packet; -protected: - void run(); - -signals: - void onMsg(); -}; - -#endif // PCAPTHREAD_H diff --git a/ledset/pcapwin.cpp b/ledset/pcapwin.cpp index bb8effe..402fc22 100644 --- a/ledset/pcapwin.cpp +++ b/ledset/pcapwin.cpp @@ -7,22 +7,22 @@ #include #include #include "globalfunc.h" -//PcapWin *PcapWin::newIns(QWidget *parent) { -// auto name = getNetDev(parent); -// if(name.isEmpty()) return 0; -// char errbuf[PCAP_ERRBUF_SIZE]{'\0'}; -// auto pcapRe = pcap_open_live(name.data(), 65536, PCAP_OPENFLAG_PROMISCUOUS, 50, errbuf); -// if(pcapRe == 0) { -// QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf); -// return 0; -// } -// auto pcapSend = pcap_open_live(name.data(), 65536, 0, 50, errbuf); -// if(pcapSend == 0) { -// QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf); -// return 0; -// } -// return new PcapWin(pcapRe, pcapSend, parent); -//} + +PcapWin *PcapWin::newIns(QByteArray &name, QWidget *parent) { + if(name.isEmpty()) return 0; + char errbuf[PCAP_ERRBUF_SIZE]{'\0'}; + auto pcapRe = pcap_open_live(name.data(), 65536, PCAP_OPENFLAG_PROMISCUOUS, 50, errbuf); + if(pcapRe == 0) { + QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf); + return 0; + } + auto pcapSend = pcap_open_live(name.data(), 65536, 0, 50, errbuf); + if(pcapSend == 0) { + QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf); + return 0; + } + return new PcapWin(pcapRe, pcapSend, parent); +} PcapWin::PcapWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWin{parent}, pcap(pcapSend) { setWindowModality(Qt::WindowModal); diff --git a/ledset/pcapwin.h b/ledset/pcapwin.h index fb086fe..82269d3 100644 --- a/ledset/pcapwin.h +++ b/ledset/pcapwin.h @@ -3,14 +3,18 @@ #include "basewin.h" #define HAVE_REMOTE -#include "pcapthread.h" +#include +#include "pcap.h" +#include + +#define LINE_LEN 16 class PcapThread : public QThread { Q_OBJECT public: explicit PcapThread(pcap *pcap); ~PcapThread() { - //pcap_close(pcap); + pcap_close(pcap); } pcap *pcap; std::atomic status{0}; @@ -24,11 +28,11 @@ signals: class PcapWin : public BaseWin { Q_OBJECT public: -// static PcapWin *newIns(QWidget *); + static PcapWin *newIns(QByteArray &, QWidget *); explicit PcapWin(pcap *, pcap *, QWidget *parent = nullptr); ~PcapWin() { - //pcap_close(pcap); + pcap_close(pcap); thd->status = 2; } PcapThread *thd{0}; diff --git a/ledset/videowin.cpp b/ledset/videowin.cpp index c1241e0..11ab6c1 100644 --- a/ledset/videowin.cpp +++ b/ledset/videowin.cpp @@ -63,9 +63,9 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi hBox->addWidget(fdMsecCapScr); hBox->addSpacing(20); - fdShowImg = new QRadioButton("关闭显示"); - fdShowImg->setChecked(showImg); - hBox->addWidget(fdShowImg); + fdNoReview = new QCheckBox("关闭显示"); + fdNoReview->setChecked(noReview); + hBox->addWidget(fdNoReview); hBox->addSpacing(20); @@ -122,11 +122,10 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi auto fdCanvas = new Canvas; vBox->addWidget(fdCanvas, 1); - connect(fdShowImg, &QRadioButton::toggled, [=](bool checked) { - qDebug()<<"fdShowImg"<showImg = checked; - if(! checked) { + connect(fdNoReview, &QRadioButton::toggled, [=](bool checked) { + noReview = checked; + if(thdRece) thdRece->noReview = checked; + if(noReview) { fdCanvas->img = QImage(); fdCanvas->update(); } @@ -137,7 +136,7 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi thdRece = 0; }); connect(thdRece, &VideoRecThread::onMsg, fdCanvas, [this, fdCanvas](QImage img, int lostTimes, int lostPkts) { - if(! showImg) return; + if(noReview) return; fdCanvas->img = img; fdCanvas->update(); if(lostPkts==0) return; @@ -295,7 +294,7 @@ void VideoRecThread::run() { int lostTimes{0}, lostPkts{0}; while((res = pcap_next_ex(pcap, &header, &data)) >= 0) { if(status==2) return; - if(status==1 || res == 0 || !showImg) continue; //超时 + if(status==1 || res == 0 || noReview) continue; //超时 if(header->caplen<24) continue; if(data[0]!=0x55 || data[1]!=0x55) continue; if(data[2]==0x9a && data[3]==0x3d) { diff --git a/ledset/videowin.h b/ledset/videowin.h index c7fcd77..fcedd48 100644 --- a/ledset/videowin.h +++ b/ledset/videowin.h @@ -40,7 +40,7 @@ public: } pcap *pcap; std::atomic status{0}; - bool showImg{true}; + bool noReview{false}; protected: void run(); signals: @@ -70,8 +70,8 @@ public: QScreen *screen; int timerId{0}; QSpinBox *fdWidth, *fdHeight, *fdMsecCapScr,*fdYxj; - QRadioButton *fdShowImg; - bool showImg{true}; + QCheckBox *fdNoReview; + bool noReview{false}; QRadioButton *fdEnd; QLabel *fdInfo, *fdLostTimes, *fdLostPkts; QString info;