-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkSpeed.cpp
99 lines (96 loc) · 2.8 KB
/
NetworkSpeed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "NetworkSpeed.h"
#include "ui_NetworkSpeed.h"
#include "Logger.h"
#include "WebsocketServer.h"
#include <QTimer>
#include <QFile>
#include <QtMath>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QJsonObject>
#include <QProcess>
NetworkSpeed::NetworkSpeed(QWidget *parent) :
QWidget(parent),
ui(new Ui::NetworkSpeed)
{
ui->setupUi(this);
//添加webSocket
WebsocketServer::instance()->registerObject("netSpeed",this);
//流量统计置零
uploaded=0;
downloaded=0;
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(calcSpeed()));
timer->start(1000);
}
NetworkSpeed::~NetworkSpeed(){
delete ui;
}
void NetworkSpeed::calcSpeed(){
#if _WIN32
ui->download->setText("Windows不支持流量统计");
ui->upload->setText("Windows不支持流量统计");
ui->dSpeed->setText("Windows不支持流量统计");
ui->uSpeed->setText("Windows不支持流量统计");
timer->stop();
return;
#endif
QFile *fp=new QFile("/proc/net/dev");
if(!fp->open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"获取网络流量失败!";
return;
}
QTextStream in(fp);
for(int i=0;i<2;i++) in.readLine();
QRegExp virtualDev("/^(virbr|br|vnet|tun|tap)[0-9]+/");
quint64 newDown=0;
quint64 newUp=0;
while(!in.atEnd()){
QString intf=in.readLine();
QStringList fields=intf.split(QRegExp("\\W+"),QString::SkipEmptyParts);
if(fields.length()<2) continue;
if(fields[0]=="lo"||virtualDev.exactMatch(fields[0])) continue;
newDown+=fields[1].toULong();
newUp +=fields[9].toULong();
}
fp->close();
delete fp;
quint32 dDown=newDown-downloaded;
quint32 dUp=newUp-uploaded;
ui->download->setText(bitCountToString(newDown));
ui->upload->setText(bitCountToString(newUp));
ui->dSpeed->setText(bitCountToString(dDown)+"/S");
ui->uSpeed->setText(bitCountToString(dUp)+"/S");
dSpeed=dDown;
uSpeed=dUp;
downloaded=newDown;
uploaded=newUp;
}
QString NetworkSpeed::bitCountToString(quint64 sum){
int rate=sum>0?qFloor(qLn(sum)/qLn(1024)):0;
const QString units[]={"B","KB","MB","GB","TB","PB","EB"};
return QString("%1%2").arg(QString::number(sum/qPow(1024,rate),'f',1),units[rate]);
}
QJsonValue NetworkSpeed::getNetworkDev(){
QJsonObject retn;
QJsonObject flow;
QProcess* proc;
flow.insert("up",(qint64)(this->uploaded));
flow.insert("down",(qint64)(this->downloaded));
flow.insert("uSpeed",(qint64)(this->uSpeed));
flow.insert("dSpeed",(qint64)(this->dSpeed));
retn.insert("flow",flow);
proc=new QProcess(this);
proc->start("netstat -nta --inet");
proc->waitForFinished();
int rows=-2;
while(!proc->atEnd()) {
proc->readLine();
rows++;
}
proc->close();
delete proc;
retn.insert("connections",rows);
return retn;
}