summaryrefslogblamecommitdiffstats
path: root/src/datasource/networkspeed.cpp
blob: 60d397dc191b92a12b6bd8ec72ff8d833570c0d9 (plain) (tree)
1
2
3
4
5
6
7




                              
                      
 




                                                                     






                                      



                             


                                     





                           
                                                                    


                               

                                                
















                                                         
                                                                                  




                                 
                                           
 
#include "networkspeed.h"

NetworkSpeed::NetworkSpeed() :
	_lastBytes(0),
	_lastMs(0),
	_file(nullptr)
{
	QStringList list = QStringList()
			<< "/sys/class/net/boot0/statistics/rx_bytes"
			<< "/sys/class/net/eth0/statistics/rx_bytes"
			<< "/sys/class/net/br0/statistics/rx_bytes";
	for (QString s : list) {
		if (!QFile::exists(s))
			continue;
		_file = new QFile(s);
	}
	if (_file != nullptr) {
		_timer.start();
	}
}

NetworkSpeed::~NetworkSpeed()
{
	if (_file != nullptr) {
		_file->deleteLater();
	}
}

qint64 NetworkSpeed::read()
{
	char buffer[500];
	qint64 ret, now;
	if (_file == nullptr || !_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 * 12);
	return list;
}

qint64 NetworkSpeed::getMaximum()
{
	return ((qint64)1024 * 1024 * 120);
}