Skip to content

Commit

Permalink
feat: cycle flush view
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Aug 23, 2024
1 parent 8f63eec commit 5dbad7a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class MainWindow : public QMainWindow {
void tableItemClicked(const QModelIndex& index);
void toggleStartBtn();
void initWidgets();
void updateCaptureStatusLabel() const;
void initInterfaceList();
void about();
void activateStatsWindow() const;
void saveAsPcap();
void initMenus();
void updateMajorView(size_t, size_t) const;

private:
Ui::MainWindow* ui;
Expand Down
1 change: 1 addition & 0 deletions include/packetsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class PacketSource final : public QObject {
void listen_started(PACKETSOURCE_STATE) const;
void listen_stopped(PACKETSOURCE_STATE) const;
void captured(size_t, Packet*);
void capture_cycle_flush(size_t, size_t);

public:
explicit PacketSource();
Expand Down
3 changes: 3 additions & 0 deletions src/packet/packetsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ void PacketSource::consume_queue()
emit this->captured(history.size() - 1, p);
dump_flush(p->get_header(), p->get_payload());
}

// 一个捕获周期结束
emit this->capture_cycle_flush(period_average, history.size() - 1);
}
}

Expand Down
48 changes: 29 additions & 19 deletions src/window/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ void MainWindow::captureInterfaceStarted(packetsource_state state)

ui->layerTree->clear();
interfaceStatusLabel->setText(state.interface_name.append(": ").append(state.state).c_str());
updateCaptureStatusLabel();
}

void MainWindow::captureInterfaceStopped(packetsource_state state) const
Expand All @@ -145,8 +144,6 @@ void MainWindow::captureInterfaceStopped(packetsource_state state) const
ui->resetBtn->setDisabled(true);
ui->startBtn->setText("Start");
interfaceStatusLabel->setText(state.interface_name.append(": ").append(state.state).c_str());

updateCaptureStatusLabel();
}

void MainWindow::resetCapture()
Expand Down Expand Up @@ -262,17 +259,6 @@ void MainWindow::initWidgets()
}
}

void MainWindow::updateCaptureStatusLabel() const
{
size_t count = packetSource->packet_count();
if (count == 0) {
captureStatusLabel->setText("");
return;
}

captureStatusLabel->setText("packets: " + QString::number(count) + "/" + QString::number(count));
}

void MainWindow::acceptPacket(const int row, Packet* packet) const
{
string src = packet->get_host_src();
Expand Down Expand Up @@ -310,11 +296,6 @@ void MainWindow::acceptPacket(const int row, Packet* packet) const
ui->packetsTable->setItem(row, 4, item4);
ui->packetsTable->setItem(row, 5, item5);
ui->packetsTable->setItem(row, 6, item6);

// 滚动到最底部,目前会导致表格卡顿和程序崩溃
// ui->packetsTable->verticalScrollBar()->setValue(ui->packetsTable->verticalScrollBar()->maximum());

updateCaptureStatusLabel();
}

void MainWindow::initSlots()
Expand All @@ -325,6 +306,7 @@ void MainWindow::initSlots()
connect(packetSource, &PacketSource::listen_started, this, &MainWindow::captureInterfaceStarted);
connect(packetSource, &PacketSource::listen_stopped, this, &MainWindow::captureInterfaceStopped);
connect(packetSource, &PacketSource::captured, this, &MainWindow::acceptPacket);
connect(packetSource, &PacketSource::capture_cycle_flush, this, &MainWindow::updateMajorView);
connect(ui->packetsTable, &QTableWidget::clicked, this, &MainWindow::tableItemClicked);
connect(ui->loadFileBtn, &QPushButton::clicked, this, &MainWindow::loadOfflineFile);
}
Expand All @@ -350,6 +332,34 @@ void MainWindow::loadOfflineFile() const
packetSource->start_on_interface(nullptr, interface);
}

void MainWindow::updateMajorView(size_t period_average, size_t sum_capture) const
{
// 滚动到最底部,大流量会导致表格卡顿和程序崩溃
ui->packetsTable->verticalScrollBar()->setValue(ui->packetsTable->verticalScrollBar()->maximum());

size_t count = packetSource->packet_count();
if (count == 0) {
captureStatusLabel->setText("");
return;
}

pcap_stat stats {};
pcap_stats(packetSource->get_interface(), &stats);

string msg = std::format("captured: {} droped: {}", count, stats.ps_drop);
captureStatusLabel->setText(msg.c_str());

// 悬浮气泡
std::ostringstream tip;
auto duration = std::chrono::high_resolution_clock::now() - time_start;
tip << count << " captured in " << std::chrono::duration_cast<std::chrono::seconds>(duration).count() << " seconds" << std::endl;
tip << stats.ps_recv << " received by filter" << std::endl;
tip << stats.ps_ifdrop << " dropped by interface" << std::endl;
tip << stats.ps_drop << " dropped by kernel" << std::endl;

captureStatusLabel->setToolTip(tip.str().c_str());
}

void MainWindow::tableItemClicked(const QModelIndex& index)
{
auto packet = packetSource->peek(index.row());
Expand Down

0 comments on commit 5dbad7a

Please sign in to comment.