iOS 直播-網速監控

来源:http://www.cnblogs.com/xubaoaichiyu/archive/2016/08/29/5819385.html
-Advertisement-
Play Games

iOS 直播-網速監控 CXNetworkSpeed.h CXNetworkSpeed.m ...


iOS 直播-網速監控

CXNetworkSpeed.h

 1 //
 2 //  CXNetworkSpeed.h
 3 //  CXNetworkSpeedDemo
 4 //
 5 //  Created by xubaoaichiyu on 16/08/16.
 6 //  Copyright © 2016年 xubaoaichiyu All rights reserved.
 7 //
 8 
 9 
10 
11 #import <Foundation/Foundation.h>
12 
13 
14 @interface CXNetworkSpeed : NSObject
15 
16 @property (nonatomic, copy, readonly) NSString * receivedNetworkSpeed;
17 
18 @property (nonatomic, copy, readonly) NSString * sendNetworkSpeed;
19 
20 + (instancetype)shareNetworkSpeed;
21 
22 - (void)startMonitoringNetworkSpeed;
23 
24 - (void)stopMonitoringNetworkSpeed;
25 
26 @end
27 
28 
29 
30 /**
31  *  @{@"received":@"100kB/s"}
32  */
33 FOUNDATION_EXTERN NSString *const kNetworkReceivedSpeedNotification;
34 
35 /**
36  *  @{@"send":@"100kB/s"}
37  */
38 FOUNDATION_EXTERN NSString *const kNetworkSendSpeedNotification;

CXNetworkSpeed.m

  1 //
  2 //  CXNetworkSpeed.m
  3 //  CXNetworkSpeedDemo
  4 //
  5 //  Created by xubaoaichiyu on 16/08/16.
  6 //  Copyright © 2016年 xubaoaichiyu All rights reserved.
  7 //
  8 
  9 #import "CXNetworkSpeed.h"
 10 #include <arpa/inet.h>
 11 #include <net/if.h>
 12 #include <ifaddrs.h>
 13 #include <net/if_dl.h>
 14 
 15 /**
 16  *  @{@"received":@"100kB/s"}
 17  */
 18 NSString *const kNetworkReceivedSpeedNotification = @"kNetworkReceivedSpeedNotification";
 19 
 20 /**
 21  *  @{@"send":@"100kB/s"}
 22  */
 23 NSString *const kNetworkSendSpeedNotification = @"kNetworkSendSpeedNotification";
 24 
 25 @interface CXNetworkSpeed ()
 26 {
 27     uint32_t _iBytes;
 28     uint32_t _oBytes;
 29     uint32_t _allFlow;
 30     uint32_t _wifiIBytes;
 31     uint32_t _wifiOBytes;
 32     uint32_t _wifiFlow;
 33     uint32_t _wwanIBytes;
 34     uint32_t _wwanOBytes;
 35     uint32_t _wwanFlow;
 36 }
 37 
 38 @property (nonatomic, copy) NSString * receivedNetworkSpeed;
 39 
 40 @property (nonatomic, copy) NSString * sendNetworkSpeed;
 41 
 42 @property (nonatomic, strong) NSTimer * timer;
 43 
 44 @end
 45 
 46 @implementation CXNetworkSpeed
 47 
 48 static CXNetworkSpeed * instance = nil;
 49 
 50 + (instancetype)shareNetworkSpeed{
 51     if(instance == nil){
 52     static dispatch_once_t onceToken ;
 53     dispatch_once(&onceToken, ^{
 54         instance = [[self alloc] init] ;
 55     }) ;
 56     }
 57     return instance;
 58     
 59 }
 60 
 61 + (instancetype)allocWithZone:(struct _NSZone *)zone{
 62     
 63     if(instance == nil){
 64     static dispatch_once_t onceToken;
 65     dispatch_once(&onceToken, ^{
 66         
 67         instance = [super allocWithZone:zone];
 68         
 69     });
 70     }
 71     return instance;
 72 }
 73 
 74 -(instancetype)init{
 75     
 76     static dispatch_once_t onceToken;
 77     dispatch_once(&onceToken, ^{
 78         instance = [super init];
 79         _iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = 0;
 80     });
 81     return instance;
 82     
 83 }
 84 
 85 - (void)startMonitoringNetworkSpeed{
 86     if(_timer)
 87         [self stopMonitoringNetworkSpeed];
 88     _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(netSpeedNotification) userInfo:nil repeats:YES];
 89 }
 90 
 91 - (void)stopMonitoringNetworkSpeed{
 92     if ([_timer isValid]) {
 93         [_timer invalidate];
 94     }
 95 }
 96 
 97 - (void)netSpeedNotification{
 98     [self checkNetworkflow];
 99 }
100 
101 -(NSString *)bytesToAvaiUnit:(int)bytes
102 {
103     if(bytes < 10)
104     {
105         return [NSString stringWithFormat:@"0KB"];
106     }
107     else if(bytes >= 10 && bytes < 1024 * 1024) // KB
108     {
109         return [NSString stringWithFormat:@"%.1fKB", (double)bytes / 1024];
110     }
111     else if(bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024)   // MB
112     {
113         return [NSString stringWithFormat:@"%.1fMB", (double)bytes / (1024 * 1024)];
114     }
115     else    // GB
116     {
117         return [NSString stringWithFormat:@"%.1fGB", (double)bytes / (1024 * 1024 * 1024)];
118     }
119 }
120 
121 
122 -(void)checkNetworkflow
123 {
124     struct ifaddrs *ifa_list = 0, *ifa;
125     if (getifaddrs(&ifa_list) == -1)
126     {
127         return ;
128     }
129     
130     uint32_t iBytes     = 0;
131     uint32_t oBytes     = 0;
132     uint32_t allFlow    = 0;
133     uint32_t wifiIBytes = 0;
134     uint32_t wifiOBytes = 0;
135     uint32_t wifiFlow   = 0;
136     uint32_t wwanIBytes = 0;
137     uint32_t wwanOBytes = 0;
138     uint32_t wwanFlow   = 0;
139 //    struct timeval32 time;
140     
141     for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
142     {
143         if (AF_LINK != ifa->ifa_addr->sa_family)
144             continue;
145         
146         if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
147             continue;
148         
149         if (ifa->ifa_data == 0)
150             continue;
151         
152         // network flow
153         if (strncmp(ifa->ifa_name, "lo", 2))
154         {
155             struct if_data *if_data = (struct if_data *)ifa->ifa_data;
156             iBytes += if_data->ifi_ibytes;
157             oBytes += if_data->ifi_obytes;
158             allFlow = iBytes + oBytes;
159         }
160         
161         //wifi flow
162         if (!strcmp(ifa->ifa_name, "en0"))
163         {
164             struct if_data *if_data = (struct if_data *)ifa->ifa_data;
165             wifiIBytes += if_data->ifi_ibytes;
166             wifiOBytes += if_data->ifi_obytes;
167             wifiFlow    = wifiIBytes + wifiOBytes;
168         }
169         
170         //3G and gprs flow
171         if (!strcmp(ifa->ifa_name, "pdp_ip0"))
172         {
173             struct if_data *if_data = (struct if_data *)ifa->ifa_data;
174             wwanIBytes += if_data->ifi_ibytes;
175             wwanOBytes += if_data->ifi_obytes;
176             wwanFlow    = wwanIBytes + wwanOBytes;
177         }
178     }
179     freeifaddrs(ifa_list);
180     
181 
182     if (_iBytes != 0) {
183         self.receivedNetworkSpeed = [[self bytesToAvaiUnit:iBytes - _iBytes] stringByAppendingString:@"/s"];
184         [[NSNotificationCenter defaultCenter] postNotificationName:kNetworkReceivedSpeedNotification object:@{@"received":self.receivedNetworkSpeed}];
185     }
186     
187     _iBytes = iBytes;
188     
189     if (_oBytes != 0) {
190         self.sendNetworkSpeed = [[self bytesToAvaiUnit:oBytes - _oBytes] stringByAppendingString:@"/s"];
191         [[NSNotificationCenter defaultCenter] postNotificationName:kNetworkSendSpeedNotification object:@{@"send":self.sendNetworkSpeed}];
192     }
193     _oBytes = oBytes;
194 }
195 @end

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 簡單記錄下網站首頁的搭建過程。 背景 自從網站功能變數名稱備案成功下來,一直以來都沒想好首頁應該怎麼寫。其實不是沒想好,而是沒有準備好首頁的多張大背景圖片應該存放在那,畢竟是最廉價的雲伺服器,應該本著勤儉持家的理念,能省就省嘛。不過還好, "bing中文搜索" 官網的背景圖片每天都會更新,於是萌生出了用no ...
  • [1]特征 [2]子節點 [3]特性操作 [4]attributes屬性 ...
  • --> 簡單的 頁面跳轉 和 點擊事件 的實現... --> AndroidManifest.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/ ...
  • 準備工作: new -> file -> other -> Empty ,在 Save As: 中隨便起個名字尾碼為 .xml 拷貝下麵 完整代碼 ...
  • 在某種場景下,可能我們需要獲取app的圖標名稱和啟動圖片的名稱。比如說app在前臺時,收到了遠程通知但是通知欄是不會有通知提醒的,這時我想做個模擬通知提示,需要用到icon名稱;再比如在載入某個控制器時,想設置該控制器的背景圖片為啟動圖片,需要用到啟動圖片名稱。 而事實上icon圖片放在系統AppI ...
  • 1. 數組的常用處理方式 // 不可變數組 //1.數組的創建 NSString *s1 = @"zhangsan"; NSString *s2 = @"lisi"; NSString *s3 = @"wangwu"; //(1) NSArray *array1 = [[NSArray alloc] ...
  • 一:首先查看一下關於UIScrollView的定義 UIScrollView用於顯示超出屏幕大小內容,一般需要配合其他控制項來使用,如添加一個UIImageView子控制項,可以用來顯示更大的圖片; UITableView、UICollectionView以及UITextView這些可以滑動顯示更多內容 ...
  • 發現android studio是真的可愛啊,上一秒還沒問題可以build運行,下一秒就出錯。。。好,你任性,你牛逼。。 說下今天又遇到的兩個問題:Failed to apply plugin [id 'com.android.application']和Could not find com.and ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...