From 31f040ed28a6b58e278fe36b249209ee20fb40bf Mon Sep 17 00:00:00 2001 From: spx Date: Sun, 7 Aug 2011 09:40:08 +0000 Subject: [PATCH] Fixed that Net measure Cumulative=1 doesn't work correctly because network statistics are gathered only once per minute. Code cleanup. --- Library/MeasureNet.cpp | 54 ++++++++++++++++++++--------------------- Library/MeasureNet.h | 5 ++-- Library/MeterWindow.cpp | 7 +++++- Library/System.cpp | 2 -- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Library/MeasureNet.cpp b/Library/MeasureNet.cpp index 9459c000..7df4e1a4 100644 --- a/Library/MeasureNet.cpp +++ b/Library/MeasureNet.cpp @@ -26,9 +26,8 @@ UINT CMeasureNet::c_NumOfTables = 0; std::vector CMeasureNet::c_StatValues; std::vector CMeasureNet::c_OldStatValues; -FPGETIFTABLE2EX CMeasureNet::c_GetIfTable2Ex = NULL; +FPGETIFTABLE2 CMeasureNet::c_GetIfTable2 = NULL; FPFREEMIBTABLE CMeasureNet::c_FreeMibTable = NULL; -bool CMeasureNet::c_UseNewApi = false; extern CRainmeter* Rainmeter; @@ -93,7 +92,7 @@ void CMeasureNet::UpdateIFTable() { bool logging = false; - if (c_UseNewApi) + if (c_GetIfTable2) { if (c_Table) { @@ -101,7 +100,7 @@ void CMeasureNet::UpdateIFTable() c_Table = NULL; } - if (c_GetIfTable2Ex(MibIfTableNormal, (MIB_IF_TABLE2**)&c_Table) == NO_ERROR) + if (c_GetIfTable2((MIB_IF_TABLE2**)&c_Table) == NO_ERROR) { MIB_IF_TABLE2* ifTable = (MIB_IF_TABLE2*)c_Table; @@ -271,7 +270,7 @@ ULONG64 CMeasureNet::GetNetOctets(NET net) { ULONG64 value = 0; - if (c_UseNewApi) + if (c_GetIfTable2) { MIB_IF_ROW2* table = (MIB_IF_ROW2*)((MIB_IF_TABLE2*)c_Table)->Table; @@ -399,7 +398,7 @@ ULONG64 CMeasureNet::GetNetStatsValue(NET net) // Ignore the loopback and filter interfaces if (c_NumOfTables == statsSize) { - if (c_UseNewApi) + if (c_GetIfTable2) { if (((MIB_IF_TABLE2*)c_Table)->Table[i].Type == IF_TYPE_SOFTWARE_LOOPBACK || ((MIB_IF_TABLE2*)c_Table)->Table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) continue; @@ -521,24 +520,24 @@ void CMeasureNet::UpdateStats() size_t statsSize = c_NumOfTables * 2; // Fill the vectors - while (c_StatValues.size() < statsSize) + if (c_StatValues.size() < statsSize) { - c_StatValues.push_back(0); + c_StatValues.resize(statsSize, 0); } - while (c_OldStatValues.size() < statsSize) + if (c_OldStatValues.size() < statsSize) { - c_OldStatValues.push_back(0); + c_OldStatValues.resize(statsSize, 0); } for (UINT i = 0; i < c_NumOfTables; ++i) { ULONG64 in, out; - if (c_UseNewApi) + if (c_GetIfTable2) { - in = (DWORD)((MIB_IF_TABLE2*)c_Table)->Table[i].InOctets; - out = (DWORD)((MIB_IF_TABLE2*)c_Table)->Table[i].OutOctets; + in = ((MIB_IF_TABLE2*)c_Table)->Table[i].InOctets; + out = ((MIB_IF_TABLE2*)c_Table)->Table[i].OutOctets; } else { @@ -546,12 +545,16 @@ void CMeasureNet::UpdateStats() out = ((MIB_IFTABLE*)c_Table)->table[i].dwOutOctets; } - if (c_OldStatValues[i * 2 + 0] != 0 && c_OldStatValues[i * 2 + 1] != 0) + if (c_OldStatValues[i * 2 + 0] != 0) { if (in > c_OldStatValues[i * 2 + 0]) { c_StatValues[i * 2 + 0] += in - c_OldStatValues[i * 2 + 0]; } + } + + if (c_OldStatValues[i * 2 + 1] != 0) + { if (out > c_OldStatValues[i * 2 + 1]) { c_StatValues[i * 2 + 1] += out - c_OldStatValues[i * 2 + 1]; @@ -597,24 +600,25 @@ void CMeasureNet::ReadStats(const WCHAR* iniFile, std::wstring& statsDate) int count = parser.ReadInt(L"Statistics", L"NetStatsCount", 0); c_StatValues.clear(); + c_StatValues.reserve(count * 2); for (int i = 1; i <= count; ++i) { ULARGE_INTEGER value; _snwprintf_s(buffer, _TRUNCATE, L"NetStatsInHigh%i", i); - value.HighPart = (DWORD)parser.ReadUInt(L"Statistics", buffer, 0); + value.HighPart = parser.ReadUInt(L"Statistics", buffer, 0); _snwprintf_s(buffer, _TRUNCATE, L"NetStatsInLow%i", i); - value.LowPart = (DWORD)parser.ReadUInt(L"Statistics", buffer, 0); + value.LowPart = parser.ReadUInt(L"Statistics", buffer, 0); c_StatValues.push_back(value.QuadPart); _snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutHigh%i", i); - value.HighPart = (DWORD)parser.ReadUInt(L"Statistics", buffer, 0); + value.HighPart = parser.ReadUInt(L"Statistics", buffer, 0); _snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutLow%i", i); - value.LowPart = (DWORD)parser.ReadUInt(L"Statistics", buffer, 0); + value.LowPart = parser.ReadUInt(L"Statistics", buffer, 0); c_StatValues.push_back(value.QuadPart); } @@ -701,15 +705,13 @@ void CMeasureNet::InitializeNewApi() HMODULE IpHlpApiLibrary = GetModuleHandle(L"IpHlpApi.dll"); if (IpHlpApiLibrary) { - c_GetIfTable2Ex = (FPGETIFTABLE2EX)GetProcAddress(IpHlpApiLibrary, "GetIfTable2Ex"); + c_GetIfTable2 = (FPGETIFTABLE2)GetProcAddress(IpHlpApiLibrary, "GetIfTable2"); c_FreeMibTable = (FPFREEMIBTABLE)GetProcAddress(IpHlpApiLibrary, "FreeMibTable"); } - c_UseNewApi = (IpHlpApiLibrary && c_GetIfTable2Ex && c_FreeMibTable); - - if (!c_UseNewApi) + if (!c_GetIfTable2 || !c_FreeMibTable) { - c_GetIfTable2Ex = NULL; + c_GetIfTable2 = NULL; c_FreeMibTable = NULL; } } @@ -728,14 +730,14 @@ void CMeasureNet::InitializeNewApi() */ void CMeasureNet::FinalizeNewApi() { - if (c_UseNewApi) + if (c_GetIfTable2) { if (c_Table) { c_FreeMibTable(c_Table); } - c_GetIfTable2Ex = NULL; + c_GetIfTable2 = NULL; c_FreeMibTable = NULL; } else @@ -744,6 +746,4 @@ void CMeasureNet::FinalizeNewApi() } c_Table = NULL; c_NumOfTables = 0; - - c_UseNewApi = false; } diff --git a/Library/MeasureNet.h b/Library/MeasureNet.h index af745443..74909799 100644 --- a/Library/MeasureNet.h +++ b/Library/MeasureNet.h @@ -24,7 +24,7 @@ #include #include "Measure.h" -typedef NETIO_STATUS (NETIOAPI_API_ * FPGETIFTABLE2EX)(MIB_IF_TABLE_LEVEL Level, PMIB_IF_TABLE2* Table); +typedef NETIO_STATUS (NETIOAPI_API_ * FPGETIFTABLE2)(PMIB_IF_TABLE2* Table); typedef VOID (NETIOAPI_API_ * FPFREEMIBTABLE)(PVOID Memory); class CMeasureNet : public CMeasure @@ -69,9 +69,8 @@ protected: static BYTE* c_Table; static UINT c_NumOfTables; - static FPGETIFTABLE2EX c_GetIfTable2Ex; + static FPGETIFTABLE2 c_GetIfTable2; static FPFREEMIBTABLE c_FreeMibTable; - static bool c_UseNewApi; }; #endif diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index 4b8e4cfe..da5f746b 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -1432,6 +1432,7 @@ void CMeterWindow::UpdateMeasure(const WCHAR* name, bool group) if (bNetStats && dynamic_cast(*i) != NULL) { CMeasureNet::UpdateIFTable(); + CMeasureNet::UpdateStats(); bNetStats = false; } @@ -2975,7 +2976,11 @@ void CMeterWindow::Update(bool nodraw) if (!m_Measures.empty()) { // Pre-updates - if (m_HasNetMeasures) CMeasureNet::UpdateIFTable(); + if (m_HasNetMeasures) + { + CMeasureNet::UpdateIFTable(); + CMeasureNet::UpdateStats(); + } CMeasureCalc::UpdateVariableMap(*this); // Update all measures diff --git a/Library/System.cpp b/Library/System.cpp index 7d1bacd3..04aae850 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -888,8 +888,6 @@ LRESULT CALLBACK CSystem::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lP case TIMER_NETSTATS: CMeasureNet::UpdateIFTable(); - - // Statistics CMeasureNet::UpdateStats(); if (Rainmeter) Rainmeter->WriteStats(false);