-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedmonitor.h
More file actions
111 lines (94 loc) · 2.55 KB
/
Copy pathspeedmonitor.h
File metadata and controls
111 lines (94 loc) · 2.55 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef SPEEDMONITOR_H
#define SPEEDMONITOR_H
#include <QObject>
#include <QTimer>
#include <QString>
/**
* @brief SpeedMonitor - 接收速度监控组件
*
* 负责统计和计算数据接收速率,提供格式化显示功能。
* 使用定时器每秒计算速度并发出更新信号。
*
* Requirements: 1.1, 2.1, 3.1
*/
class SpeedMonitor : public QObject
{
Q_OBJECT
public:
/**
* @brief 构造函数
* @param parent 父对象
*/
explicit SpeedMonitor(QObject *parent = nullptr);
/**
* @brief 析构函数
*/
~SpeedMonitor();
/**
* @brief 记录接收到的字节数
* @param bytes 接收到的字节数(负数将被忽略)
* Requirements: 3.1
*/
void recordBytes(qint64 bytes);
/**
* @brief 重置所有计数器
* Requirements: 3.2, 3.3
*/
void reset();
/**
* @brief 启动速度监控
* Requirements: 1.1
*/
void start();
/**
* @brief 停止速度监控
* Requirements: 1.3
*/
void stop();
/**
* @brief 获取累计总字节数
* @return 累计接收的总字节数
*/
qint64 totalBytes() const;
/**
* @brief 获取当前速度
* @return 当前速度(bytes/s)
*/
double currentSpeed() const;
/**
* @brief 格式化速度显示
* @param bytesPerSecond 速度值(bytes/s)
* @return 格式化后的字符串(如 "512 bytes/s" 或 "1.5 KB/s")
* Requirements: 1.2
*/
static QString formatSpeed(double bytesPerSecond);
/**
* @brief 格式化字节数显示
* @param bytes 字节数
* @return 格式化后的字符串(如 "512 B", "1.5 KB", "2.1 MB")
* Requirements: 3.4
*/
static QString formatBytes(qint64 bytes);
signals:
/**
* @brief 速度更新信号(每秒发出)
* @param bytesPerSecond 当前速度(bytes/s)
* @param totalBytes 累计总字节数
* Requirements: 2.1
*/
void speedUpdated(double bytesPerSecond, qint64 totalBytes);
private slots:
/**
* @brief 定时器超时处理
*
* 计算当前间隔内的速度并发出 speedUpdated 信号。
*/
void onTimeout();
private:
QTimer *m_timer; ///< 定时器(1秒间隔)
qint64 m_totalBytes; ///< 累计总字节数
qint64 m_intervalBytes; ///< 当前间隔内的字节数
double m_currentSpeed; ///< 当前速度 (bytes/s)
static constexpr int UPDATE_INTERVAL_MS = 1000; ///< 更新间隔(毫秒)
};
#endif // SPEEDMONITOR_H