ZFNetworkSpeedMonitor.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // ZFNetworkSpeedMonitor.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFNetworkSpeedMonitor.h"
  25. #if __has_include(<ZFPlayer/ZFPlayerLogManager.h>)
  26. #import <ZFPlayer/ZFPlayerLogManager.h>
  27. #else
  28. #import "ZFPlayerLogManager.h"
  29. #endif
  30. #include <arpa/inet.h>
  31. #include <ifaddrs.h>
  32. #include <net/if.h>
  33. #include <net/if_dl.h>
  34. NSString *const ZFDownloadNetworkSpeedNotificationKey = @"ZFDownloadNetworkSpeedNotificationKey";
  35. NSString *const ZFUploadNetworkSpeedNotificationKey = @"ZFUploadNetworkSpeedNotificationKey";
  36. NSString *const ZFNetworkSpeedNotificationKey = @"ZFNetworkSpeedNotificationKey";
  37. @interface ZFNetworkSpeedMonitor () {
  38. // 总网速
  39. uint32_t _iBytes;
  40. uint32_t _oBytes;
  41. uint32_t _allFlow;
  42. // wifi网速
  43. uint32_t _wifiIBytes;
  44. uint32_t _wifiOBytes;
  45. uint32_t _wifiFlow;
  46. // 3G网速
  47. uint32_t _wwanIBytes;
  48. uint32_t _wwanOBytes;
  49. uint32_t _wwanFlow;
  50. }
  51. @property (nonatomic, strong) NSTimer *timer;
  52. @end
  53. @implementation ZFNetworkSpeedMonitor
  54. - (instancetype)init {
  55. if (self = [super init]) {
  56. _iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = 0;
  57. }
  58. return self;
  59. }
  60. // 开始监听网速
  61. - (void)startNetworkSpeedMonitor {
  62. if (!_timer) {
  63. _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkNetworkSpeed) userInfo:nil repeats:YES];
  64. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  65. [_timer fire];
  66. }
  67. }
  68. // 停止监听网速
  69. - (void)stopNetworkSpeedMonitor {
  70. if ([_timer isValid]) {
  71. [_timer invalidate];
  72. _timer = nil;
  73. }
  74. }
  75. - (NSString *)stringWithbytes:(int)bytes {
  76. if (bytes < 1024) { // B
  77. return [NSString stringWithFormat:@"%dB", bytes];
  78. } else if (bytes >= 1024 && bytes < 1024 * 1024) { // KB
  79. return [NSString stringWithFormat:@"%.0fKB", (double)bytes / 1024];
  80. } else if (bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) { // MB
  81. return [NSString stringWithFormat:@"%.1fMB", (double)bytes / (1024 * 1024)];
  82. } else { // GB
  83. return [NSString stringWithFormat:@"%.1fGB", (double)bytes / (1024 * 1024 * 1024)];
  84. }
  85. }
  86. - (void)checkNetworkSpeed {
  87. struct ifaddrs *ifa_list = 0, *ifa;
  88. if (getifaddrs(&ifa_list) == -1) return;
  89. uint32_t iBytes = 0;
  90. uint32_t oBytes = 0;
  91. uint32_t allFlow = 0;
  92. uint32_t wifiIBytes = 0;
  93. uint32_t wifiOBytes = 0;
  94. uint32_t wifiFlow = 0;
  95. uint32_t wwanIBytes = 0;
  96. uint32_t wwanOBytes = 0;
  97. uint32_t wwanFlow = 0;
  98. for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
  99. if (AF_LINK != ifa->ifa_addr->sa_family) continue;
  100. if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) continue;
  101. if (ifa->ifa_data == 0) continue;
  102. // network
  103. if (strncmp(ifa->ifa_name, "lo", 2)) {
  104. struct if_data* if_data = (struct if_data*)ifa->ifa_data;
  105. iBytes += if_data->ifi_ibytes;
  106. oBytes += if_data->ifi_obytes;
  107. allFlow = iBytes + oBytes;
  108. }
  109. //wifi
  110. if (!strcmp(ifa->ifa_name, "en0")) {
  111. struct if_data* if_data = (struct if_data*)ifa->ifa_data;
  112. wifiIBytes += if_data->ifi_ibytes;
  113. wifiOBytes += if_data->ifi_obytes;
  114. wifiFlow = wifiIBytes + wifiOBytes;
  115. }
  116. //3G or gprs
  117. if (!strcmp(ifa->ifa_name, "pdp_ip0")) {
  118. struct if_data* if_data = (struct if_data*)ifa->ifa_data;
  119. wwanIBytes += if_data->ifi_ibytes;
  120. wwanOBytes += if_data->ifi_obytes;
  121. wwanFlow = wwanIBytes + wwanOBytes;
  122. }
  123. }
  124. freeifaddrs(ifa_list);
  125. if (_iBytes != 0) {
  126. _downloadNetworkSpeed = [[self stringWithbytes:iBytes - _iBytes] stringByAppendingString:@"/s"];
  127. [[NSNotificationCenter defaultCenter] postNotificationName:ZFDownloadNetworkSpeedNotificationKey object:nil userInfo:@{ZFNetworkSpeedNotificationKey:_downloadNetworkSpeed}];
  128. ZFPlayerLog(@"downloadNetworkSpeed : %@",_downloadNetworkSpeed);
  129. }
  130. _iBytes = iBytes;
  131. if (_oBytes != 0) {
  132. _uploadNetworkSpeed = [[self stringWithbytes:oBytes - _oBytes] stringByAppendingString:@"/s"];
  133. [[NSNotificationCenter defaultCenter] postNotificationName:ZFUploadNetworkSpeedNotificationKey object:nil userInfo:@{ZFNetworkSpeedNotificationKey:_uploadNetworkSpeed}];
  134. ZFPlayerLog(@"uploadNetworkSpeed :%@",_uploadNetworkSpeed);
  135. }
  136. _oBytes = oBytes;
  137. }
  138. @end