blob: 8888fd4182a870a267cd9523ac67489edd3851fa (
plain) (
tree)
|
|
#include "networkspeed.h"
NetworkSpeed::NetworkSpeed() :
_lastBytes(0),
_lastMs(0),
_file("/sys/class/net/eth0/statistics/rx_bytes")
{
_timer.start();
}
NetworkSpeed::~NetworkSpeed()
{
// TODO Auto-generated destructor stub
}
qint64 NetworkSpeed::read()
{
char buffer[500];
qint64 ret, now;
if (!_file.open(QIODevice::ReadOnly)) {
return -1;
}
now = _timer.elapsed();
_file.readLine(buffer, sizeof(buffer));
_file.close();
QString line(buffer);
const qint64 counter = (qint64)line.toLongLong();
if (_lastBytes == 0) {
ret = 0;
} else {
ret = counter - _lastBytes;
}
if (_lastMs < now) {
ret = (ret * 1000) / (now - _lastMs);
}
_lastMs = now;
_lastBytes = counter;
return ret;
}
const QList<qint64>& NetworkSpeed::getBars() const
{
static QList<qint64> list = QList<qint64>() << ((qint64)1024 * 1024 * 10);
return list;
}
qint64 NetworkSpeed::getMaximum()
{
return ((qint64)1024 * 1024 * 100);
}
|