forked from BlakeBoxberger/Nitty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtSpeed.m
More file actions
79 lines (73 loc) · 1.87 KB
/
NtSpeed.m
File metadata and controls
79 lines (73 loc) · 1.87 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
// Julioverne's code for NtSpeed
static __strong NSString* kBs = @"%ldB/s";
static __strong NSString* kKs = @"%.0fK/s";
static __strong NSString* kMs = @"%.0fM/s";
static __strong NSString* kGs = @"%.0fG/s";
static NSString *bytesFormat(long bytes)
{
@autoreleasepool {
if(bytes < 1024) {
return [NSString stringWithFormat:kBs, bytes];
} else if(bytes >= 1024 && bytes < 1024 * 1024) {
return [NSString stringWithFormat:kKs, round((double)bytes / 1024)];
} else if(bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) {
return [NSString stringWithFormat:kMs, round((double)bytes / (1024 * 1024))];
} else {
return [NSString stringWithFormat:kGs, round((double)bytes / (1024 * 1024 * 1024))];
}
}
}
static long getBytesTotal()
{
@autoreleasepool {
uint32_t iBytes = 0;
uint32_t oBytes = 0;
struct ifaddrs *ifa_list = NULL, *ifa;
if ((getifaddrs(&ifa_list) < 0) || !ifa_list || ifa_list==0) {
return 0;
}
for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) {
continue;
}
if (AF_LINK != ifa->ifa_addr->sa_family) {
continue;
}
if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) {
continue;
}
if (ifa->ifa_data == NULL || ifa->ifa_data == 0) {
continue;
}
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
iBytes += if_data->ifi_ibytes;
oBytes += if_data->ifi_obytes;
}
if(ifa_list) {
freeifaddrs(ifa_list);
}
return iBytes + oBytes;
}
}
static long oldSpeed = 0;
// Combination of my code and Julioverne's
static NSString *getNetworkSpeedString()
{
@autoreleasepool {
try_again:
long nowData = 0;
getBytesTotal();
while(nowData <= 0) {
nowData = getBytesTotal();
}
if(!oldSpeed) {
oldSpeed = nowData;
}
long speed = nowData-oldSpeed;
oldSpeed = nowData;
if(oldSpeed <= 0) {
goto try_again;
}
return bytesFormat(speed);
}
}