Skip to content

Commit c171afd

Browse files
committed
修正当监控时间周期设置得过小时,显示的网速可能会大于实际网速问题 #1263 #1674
1 parent aa1971d commit c171afd

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

TrafficMonitor/Common.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,24 @@ SYSTEMTIME CCommon::CompareSystemTime(SYSTEMTIME a, SYSTEMTIME b)
498498
return result;
499499
}
500500

501+
ULONGLONG CCommon::GetCurrentTimeSinceEpochMilliseconds()
502+
{
503+
FILETIME fileTime;
504+
GetSystemTimeAsFileTime(&fileTime); // 获取当前系统时间
505+
506+
// 将FILETIME转换为ULARGE_INTEGER以便计算
507+
ULARGE_INTEGER uli;
508+
uli.LowPart = fileTime.dwLowDateTime;
509+
uli.HighPart = fileTime.dwHighDateTime;
510+
511+
// 从1601年1月1日到1970年1月1日的100纳秒间隔数
512+
const ULONGLONG EPOCH_OFFSET = 116444736000000000ULL;
513+
514+
// 转换为从1970年1月1日开始的毫秒数
515+
ULONGLONG millisecondsSince1970 = (uli.QuadPart - EPOCH_OFFSET) / 10000;
516+
return millisecondsSince1970;
517+
}
518+
501519
wstring CCommon::GetModuleDir()
502520
{
503521
wchar_t path[MAX_PATH];

TrafficMonitor/Common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class CCommon
178178
//计算两个SYSTEMTIME结构时间的差(a-b,只保留时、分、秒)
179179
static SYSTEMTIME CompareSystemTime(SYSTEMTIME a, SYSTEMTIME b);
180180

181+
//获取从1970年1月1日到现在的毫秒数
182+
static ULONGLONG GetCurrentTimeSinceEpochMilliseconds();
183+
181184
//获取当前程序的目录
182185
static wstring GetModuleDir();
183186

TrafficMonitor/TrafficMonitorDlg.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,17 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
12351235
//if (cur_out_speed > 1073741824)
12361236
// cur_out_speed = 0;
12371237

1238+
//计算两次获取网速的时间间隔
1239+
static ULONGLONG last_net_speed_time = 0;
1240+
ULONGLONG net_speed_time = CCommon::GetCurrentTimeSinceEpochMilliseconds();
1241+
int time_span = theApp.m_general_data.monitor_time_span;
1242+
if (last_net_speed_time != 0)
1243+
time_span = static_cast<int>(net_speed_time - last_net_speed_time);
1244+
last_net_speed_time = net_speed_time;
1245+
12381246
//将当前监控时间间隔的流量转换成每秒时间间隔内的流量
1239-
theApp.m_in_speed = static_cast<unsigned __int64>(cur_in_speed * 1000 / theApp.m_general_data.monitor_time_span);
1240-
theApp.m_out_speed = static_cast<unsigned __int64>(cur_out_speed * 1000 / theApp.m_general_data.monitor_time_span);
1247+
theApp.m_in_speed = static_cast<unsigned __int64>(cur_in_speed * 1000 / time_span);
1248+
theApp.m_out_speed = static_cast<unsigned __int64>(cur_out_speed * 1000 / time_span);
12411249

12421250
pThis->m_connection_change_flag = false; //清除连接发生变化的标志
12431251

0 commit comments

Comments
 (0)