Repository navigation
Expand file tree
/
Copy pathNetworkManager.cpp
More file actions
85 lines (69 loc) · 1.85 KB
/
Copy pathNetworkManager.cpp
File metadata and controls
85 lines (69 loc) · 1.85 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
#include "NetworkManager.h"
NetworkManager::NetworkManager()
{
}
NetworkManager::~NetworkManager()
{
}
//PURPOSE: Find currently active adaptor, cache it's position, then track the different between the previous In/Out octets vs the current
std::tuple<double, double> NetworkManager::GetAdaptorInfo(HWND hWnd, PMIB_IF_TABLE2* interfaces)
{
double dl = -1.0;
double ul = -1.0;
int adapterIndex = -1;
if (GetIfTable2(interfaces) == NO_ERROR)
{
MIB_IF_ROW2* row;
for (int i = 0; i < interfaces[0]->NumEntries; i++)
{
row = &interfaces[0]->Table[i];
if (row)
{
//Find any interface that has incoming data, is marked as connected and not set as Unspecified medium type
if (row->InOctets <= 0 || row->MediaConnectState != MediaConnectStateConnected || row->PhysicalMediumType == NdisPhysicalMediumUnspecified)
{
continue;
}
adapterIndex = i;
break;
}
}
if (adapterIndex != -1)
{
row = &interfaces[0]->Table[adapterIndex];
bool newAdapter = false;
if(memcmp(row->PermanentPhysicalAddress, currentPhysicalAddress, IF_MAX_PHYS_ADDRESS_LENGTH))
{
newAdapter = true;
memcpy(currentPhysicalAddress, row->PermanentPhysicalAddress, IF_MAX_PHYS_ADDRESS_LENGTH);
}
if (row)
{
//Convert to bits
double dlBits = (row->InOctets * 8.0);
if (newAdapter) //Fix massive delta when first running program/switching adapters
{
lastDlCount = dlBits;
}
dl = dlBits - lastDlCount;
lastDlCount = dlBits;
double ulBits = (row->OutOctets * 8.0);
if (newAdapter)
{
lastUlCount = ulBits;
}
ul = ulBits - lastUlCount;
lastUlCount = ulBits;
}
}
else
{
//No adapters available, just return 0
dl = 0;
ul = 0;
}
}
//GetIfTable2 allocates memory for the tables, which we must delete
FreeMibTable(interfaces[0]);
return std::make_tuple(dl, ul);
}