forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureNet.cpp
493 lines (423 loc) · 11.6 KB
/
MeasureNet.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Copyright (C) 2001 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeasureNet.h"
#include "Rainmeter.h"
#include "System.h"
#include "../Common/NetworkUtil.h"
std::vector<ULONG64> MeasureNet::c_StatValues;
std::vector<ULONG64> MeasureNet::c_OldStatValues;
MeasureNet::MeasureNet(Skin* skin, const WCHAR* name, NET type) : Measure(skin, name),
m_Net(type),
m_Interface(0U),
m_Octets(0Ui64),
m_FirstTime(true),
m_Cumulative(false),
m_UseBits(false)
{
}
MeasureNet::~MeasureNet()
{
}
/*
** Reads the tables for all net interfaces
**
*/
void MeasureNet::UpdateIFTable()
{
const ULONG oldCount = NetworkUtil::GetInterfaceCount();
if (!NetworkUtil::UpdateInterfaceTable()) return;
MIB_IF_ROW2* table = NetworkUtil::GetInterfaceTable();
const ULONG newCount = NetworkUtil::GetInterfaceCount();
if (table && GetRainmeter().GetDebug() && oldCount != newCount)
{
LogDebug(L"------------------------------");
LogDebugF(L"* NETWORK-INTERFACE: Count=%i", newCount);
for (size_t i = 0ULL; i < newCount; ++i)
{
LPCWSTR type = NetworkUtil::GetInterfaceTypeString(table[i].Type);
LPCWSTR state = NetworkUtil::GetInterfaceMediaConnectionString(table[i].MediaConnectState);
LPCWSTR status = NetworkUtil::GetInterfaceOperStatusString(table[i].OperStatus);
LogDebugF(L"%3i: Name: %s", (int)i + 1, table[i].Description);
LogDebugF(L" Alias: %s", table[i].Alias);
WCHAR guid[64] = { 0 };
if (StringFromGUID2(table[i].InterfaceGuid, guid, 64) > 0)
{
LogDebugF(L" GUID: %s", guid);
}
LogDebugF(L" Type=%s(%i), Hardware=%s, Filter=%s",
type, table[i].Type,
(table[i].InterfaceAndOperStatusFlags.HardwareInterface == 1) ? L"Yes" : L"No",
(table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) ? L"Yes" : L"No");
LogDebugF(L" IfIndex=%i, State=%s, Status=%s(%i)",
table[i].InterfaceIndex,
state,
status, table[i].OperStatus);
}
LogDebug(L"------------------------------");
}
}
/*
** Reads the amount of octets. This is the same for in, out and total.
** the net-parameter informs which inherited class called this method.
**
*/
ULONG64 MeasureNet::GetNetOctets(NET net)
{
ULONG64 value = 0;
MIB_IF_ROW2* table = NetworkUtil::GetInterfaceTable();
if (!table) return value;
const ULONG interfaceCount = NetworkUtil::GetInterfaceCount();
if (m_Interface == 0)
{
// Get all interfaces
for (ULONG i = 0UL; i < interfaceCount; ++i)
{
// Ignore the loopback and filter interfaces
if (table[i].Type == IF_TYPE_SOFTWARE_LOOPBACK ||
table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) continue;
switch (net)
{
case NET_IN:
value += table[i].InOctets;
break;
case NET_OUT:
value += table[i].OutOctets;
break;
case NET_TOTAL:
value += table[i].InOctets;
value += table[i].OutOctets;
break;
}
}
}
else
{
if (m_Interface <= interfaceCount)
{
ULONG index = NetworkUtil::GetIndexFromIfIndex(m_Interface);
switch (net)
{
case NET_IN:
value += table[index].InOctets;
break;
case NET_OUT:
value += table[index].OutOctets;
break;
case NET_TOTAL:
value += table[index].InOctets;
value += table[index].OutOctets;
break;
}
}
}
return value;
}
/*
** Returns the stats value of the interface
**
*/
ULONG64 MeasureNet::GetNetStatsValue(NET net)
{
ULONG64 value = 0;
size_t statsSize = c_StatValues.size() / 2;
MIB_IF_ROW2* table = NetworkUtil::GetInterfaceTable();
if (!table) return value;
const ULONG interfaceCount = NetworkUtil::GetInterfaceCount();
if (m_Interface == 0UL)
{
// Get all interfaces
for (size_t i = 0ULL; i < statsSize; ++i)
{
// Ignore the loopback and filter interfaces
if (interfaceCount == statsSize)
{
if (table[i].Type == IF_TYPE_SOFTWARE_LOOPBACK ||
table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) continue;
}
size_t index = i * 2ULL;
switch (net)
{
case NET_IN:
value += c_StatValues[index];
break;
case NET_OUT:
++index;
value += c_StatValues[index];
break;
case NET_TOTAL:
value += c_StatValues[index];
++index;
value += c_StatValues[index];
break;
}
}
}
else
{
// Get the selected interface
if (m_Interface <= statsSize)
{
ULONG index = NetworkUtil::GetIndexFromIfIndex(m_Interface) * 2UL;
switch (net)
{
case NET_IN:
value += c_StatValues[index];
break;
case NET_OUT:
++index;
value += c_StatValues[index];
break;
case NET_TOTAL:
value += c_StatValues[index];
++index;
value += c_StatValues[index];
break;
}
}
}
return value;
}
void MeasureNet::UpdateValue()
{
if (!NetworkUtil::GetInterfaceTable()) return;
const ULONG64 bits = m_UseBits ? 8ULL : 1ULL;
if (m_Cumulative)
{
m_Value = (double)(__int64)(GetNetStatsValue(m_Net) * bits);
}
else
{
ULONG64 value = 0ULL;
if (!m_FirstTime)
{
value = GetNetOctets(m_Net);
if (value > m_Octets)
{
ULONG64 tmpValue = value;
value -= m_Octets;
m_Octets = tmpValue;
}
else
{
m_Octets = value;
value = 0ULL;
}
}
else
{
m_Octets = GetNetOctets(m_Net);
m_FirstTime = false;
}
m_Value = (double)(__int64)(value * bits);
}
}
void MeasureNet::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);
double value = 0.0;
const WCHAR* netName = nullptr;
if (m_Net == NET_IN)
{
netName = L"NetInSpeed";
value = GetRainmeter().GetGlobalOptions().netInSpeed;
}
else if (m_Net == NET_OUT)
{
netName = L"NetOutSpeed";
value = GetRainmeter().GetGlobalOptions().netOutSpeed;
}
else // if (m_Net == NET_TOTAL)
{
netName = L"NetTotalSpeed";
value = GetRainmeter().GetGlobalOptions().netInSpeed + GetRainmeter().GetGlobalOptions().netOutSpeed;
}
double maxValue = parser.ReadFloat(section, L"MaxValue", -1.0);
if (maxValue == -1.0)
{
maxValue = parser.ReadFloat(section, netName, -1.0);
if (maxValue == -1.0)
{
maxValue = value;
}
}
// Option 'Interface' represents either the number of the interface in the 'iftable',
// or the name of the interface (ie. its Description). Optionally, if 'Interface=Best',
// there will be an attempt to find the best interface.
std::wstring iface = parser.ReadString(section, L"Interface", L"BEST");
if (!iface.empty() && !std::all_of(iface.begin(), iface.end(), iswdigit))
{
m_Interface = NetworkUtil::FindBestInterface(iface.c_str());
if (GetRainmeter().GetDebug())
{
MIB_IF_ROW2* table = NetworkUtil::GetInterfaceTable();
for (size_t i = 0ULL; i < NetworkUtil::GetInterfaceCount(); ++i)
{
if (table[i].InterfaceIndex == m_Interface)
{
LogDebugF(this, L"Using network interface: %s (IfIndex=%i)", table[i].Description, m_Interface);
break;
}
}
}
}
else
{
m_Interface = parser.ReadUInt(section, L"Interface", 0U);
}
m_Cumulative = parser.ReadBool(section, L"Cumulative", false);
if (m_Cumulative)
{
GetRainmeter().SetNetworkStatisticsTimer();
}
m_UseBits = parser.ReadBool(section, L"UseBits", false);
if (maxValue == 0.0)
{
if (!m_LogMaxValue)
{
m_MaxValue = 1.0;
m_LogMaxValue = true;
m_MedianValues.clear();
}
}
else
{
m_MaxValue = maxValue / (m_UseBits ? 1.0 : 8.0);
m_LogMaxValue = false;
}
}
void MeasureNet::UpdateStats()
{
MIB_IF_ROW2* table = NetworkUtil::GetInterfaceTable();
if (!table) return;
const ULONG interfaceCount = NetworkUtil::GetInterfaceCount();
size_t statsSize = (size_t)interfaceCount * 2ULL;
// Fill the vectors
if (c_StatValues.size() < statsSize)
{
c_StatValues.resize(statsSize, 0ULL);
}
if (c_OldStatValues.size() < statsSize)
{
c_OldStatValues.resize(statsSize, 0ULL);
}
for (size_t i = 0ULL; i < interfaceCount; ++i)
{
ULONG64 in = table[i].InOctets;
ULONG64 out = table[i].OutOctets;
if (c_OldStatValues[i * 2 + 0] != 0ULL)
{
if (in > c_OldStatValues[i * 2 + 0])
{
c_StatValues[i * 2 + 0] += in - c_OldStatValues[i * 2 + 0];
}
}
if (c_OldStatValues[i * 2 + 1] != 0ULL)
{
if (out > c_OldStatValues[i * 2 + 1])
{
c_StatValues[i * 2 + 1] += out - c_OldStatValues[i * 2 + 1];
}
}
c_OldStatValues[i * 2 + 0] = in;
c_OldStatValues[i * 2 + 1] = out;
}
}
void MeasureNet::ResetStats()
{
c_StatValues.clear();
}
void MeasureNet::ReadStats(const std::wstring& iniFile, std::wstring& statsDate)
{
WCHAR buffer[48];
ConfigParser parser;
parser.Initialize(iniFile, nullptr, L"Statistics");
const std::wstring& date = parser.ReadString(L"Statistics", L"Since", L"", false);
if (!date.empty())
{
statsDate = date;
}
uint32_t count = parser.ReadUInt(L"Statistics", L"Count", 0U);
if (parser.GetLastDefaultUsed())
{
count = parser.ReadUInt(L"Statistics", L"NetStatsCount", 0U);
}
c_StatValues.clear();
c_StatValues.reserve((ULONG64)count * 2ULL);
for (uint32_t i = 1U; i <= count; ++i)
{
ULARGE_INTEGER value = { 0 };
_snwprintf_s(buffer, _TRUNCATE, L"In%u", i);
value.QuadPart = parser.ReadUInt64(L"Statistics", buffer, 0Ui64);
if (parser.GetLastDefaultUsed())
{
_snwprintf_s(buffer, _TRUNCATE, L"NetStatsInHigh%u", i);
value.HighPart = parser.ReadUInt(L"Statistics", buffer, 0U);
_snwprintf_s(buffer, _TRUNCATE, L"NetStatsInLow%u", i);
value.LowPart = parser.ReadUInt(L"Statistics", buffer, 0U);
}
c_StatValues.push_back(value.QuadPart);
_snwprintf_s(buffer, _TRUNCATE, L"Out%u", i);
value.QuadPart = parser.ReadUInt64(L"Statistics", buffer, 0Ui64);
if (parser.GetLastDefaultUsed())
{
_snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutHigh%u", i);
value.HighPart = parser.ReadUInt(L"Statistics", buffer, 0U);
_snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutLow%u", i);
value.LowPart = parser.ReadUInt(L"Statistics", buffer, 0U);
}
c_StatValues.push_back(value.QuadPart);
}
}
void MeasureNet::WriteStats(const WCHAR* iniFile, const std::wstring& statsDate)
{
WCHAR buffer[48] = { 0 };
int len = 0;
uint32_t count = (uint32_t)c_StatValues.size() / 2;
// Reserve sufficient buffer for statistics
std::wstring data;
data.reserve(48 * (2 + count));
// Add date
data = L"Since=";
data += statsDate;
data += L'\0';
auto appendStatsValue = [&]()
{
data.append(buffer, len);
data += L'\0';
};
// Add stats count
len = _snwprintf_s(buffer, _TRUNCATE, L"Count=%u", count);
appendStatsValue();
// Add stats
for (uint32_t i = 0U; i < count; ++i)
{
if (c_StatValues[i * 2] > 0ULL)
{
len = _snwprintf_s(buffer, _TRUNCATE, L"In%u=%llu", i + 1U, c_StatValues[i * 2]);
appendStatsValue();
}
if (c_StatValues[i * 2 + 1] > 0ULL)
{
len = _snwprintf_s(buffer, _TRUNCATE, L"Out%u=%llu", i + 1U, c_StatValues[i * 2 + 1]);
appendStatsValue();
}
}
// Write statistics
WritePrivateProfileSection(L"Statistics", data.c_str(), iniFile);
}
void MeasureNet::InitializeStatic()
{
if (GetRainmeter().GetDebug())
{
UpdateIFTable();
}
}
void MeasureNet::FinalizeStatic()
{
NetworkUtil::Finalize();
}