iOS 直播-网速监控

iOS 直播-网速监控

CXNetworkSpeed.h

 //
// CXNetworkSpeed.h
// CXNetworkSpeedDemo
//
// Created by xubaoaichiyu on 16/08/16.
// Copyright © 2016年 xubaoaichiyu All rights reserved.
// #import <Foundation/Foundation.h> @interface CXNetworkSpeed : NSObject @property (nonatomic, copy, readonly) NSString * receivedNetworkSpeed; @property (nonatomic, copy, readonly) NSString * sendNetworkSpeed; + (instancetype)shareNetworkSpeed; - (void)startMonitoringNetworkSpeed; - (void)stopMonitoringNetworkSpeed; @end /**
* @{@"received":@"100kB/s"}
*/
FOUNDATION_EXTERN NSString *const kNetworkReceivedSpeedNotification; /**
* @{@"send":@"100kB/s"}
*/
FOUNDATION_EXTERN NSString *const kNetworkSendSpeedNotification;

CXNetworkSpeed.m

 //
// CXNetworkSpeed.m
// CXNetworkSpeedDemo
//
// Created by xubaoaichiyu on 16/08/16.
// Copyright © 2016年 xubaoaichiyu All rights reserved.
// #import "CXNetworkSpeed.h"
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h> /**
* @{@"received":@"100kB/s"}
*/
NSString *const kNetworkReceivedSpeedNotification = @"kNetworkReceivedSpeedNotification"; /**
* @{@"send":@"100kB/s"}
*/
NSString *const kNetworkSendSpeedNotification = @"kNetworkSendSpeedNotification"; @interface CXNetworkSpeed ()
{
uint32_t _iBytes;
uint32_t _oBytes;
uint32_t _allFlow;
uint32_t _wifiIBytes;
uint32_t _wifiOBytes;
uint32_t _wifiFlow;
uint32_t _wwanIBytes;
uint32_t _wwanOBytes;
uint32_t _wwanFlow;
} @property (nonatomic, copy) NSString * receivedNetworkSpeed; @property (nonatomic, copy) NSString * sendNetworkSpeed; @property (nonatomic, strong) NSTimer * timer; @end @implementation CXNetworkSpeed static CXNetworkSpeed * instance = nil; + (instancetype)shareNetworkSpeed{
if(instance == nil){
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init] ;
}) ;
}
return instance; } + (instancetype)allocWithZone:(struct _NSZone *)zone{ if(instance == nil){
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [super allocWithZone:zone]; });
}
return instance;
} -(instancetype)init{ static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [super init];
_iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = ;
});
return instance; } - (void)startMonitoringNetworkSpeed{
if(_timer)
[self stopMonitoringNetworkSpeed];
_timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(netSpeedNotification) userInfo:nil repeats:YES];
} - (void)stopMonitoringNetworkSpeed{
if ([_timer isValid]) {
[_timer invalidate];
}
} - (void)netSpeedNotification{
[self checkNetworkflow];
} -(NSString *)bytesToAvaiUnit:(int)bytes
{
if(bytes < )
{
return [NSString stringWithFormat:@"0KB"];
}
else if(bytes >= && bytes < * ) // KB
{
return [NSString stringWithFormat:@"%.1fKB", (double)bytes / ];
}
else if(bytes >= * && bytes < * * ) // MB
{
return [NSString stringWithFormat:@"%.1fMB", (double)bytes / ( * )];
}
else // GB
{
return [NSString stringWithFormat:@"%.1fGB", (double)bytes / ( * * )];
}
} -(void)checkNetworkflow
{
struct ifaddrs *ifa_list = , *ifa;
if (getifaddrs(&ifa_list) == -)
{
return ;
} uint32_t iBytes = ;
uint32_t oBytes = ;
uint32_t allFlow = ;
uint32_t wifiIBytes = ;
uint32_t wifiOBytes = ;
uint32_t wifiFlow = ;
uint32_t wwanIBytes = ;
uint32_t wwanOBytes = ;
uint32_t wwanFlow = ;
// struct timeval32 time; for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
{
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 == )
continue; // network flow
if (strncmp(ifa->ifa_name, "lo", ))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
iBytes += if_data->ifi_ibytes;
oBytes += if_data->ifi_obytes;
allFlow = iBytes + oBytes;
} //wifi flow
if (!strcmp(ifa->ifa_name, "en0"))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
wifiIBytes += if_data->ifi_ibytes;
wifiOBytes += if_data->ifi_obytes;
wifiFlow = wifiIBytes + wifiOBytes;
} //3G and gprs flow
if (!strcmp(ifa->ifa_name, "pdp_ip0"))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
wwanIBytes += if_data->ifi_ibytes;
wwanOBytes += if_data->ifi_obytes;
wwanFlow = wwanIBytes + wwanOBytes;
}
}
freeifaddrs(ifa_list); if (_iBytes != ) {
self.receivedNetworkSpeed = [[self bytesToAvaiUnit:iBytes - _iBytes] stringByAppendingString:@"/s"];
[[NSNotificationCenter defaultCenter] postNotificationName:kNetworkReceivedSpeedNotification object:@{@"received":self.receivedNetworkSpeed}];
} _iBytes = iBytes; if (_oBytes != ) {
self.sendNetworkSpeed = [[self bytesToAvaiUnit:oBytes - _oBytes] stringByAppendingString:@"/s"];
[[NSNotificationCenter defaultCenter] postNotificationName:kNetworkSendSpeedNotification object:@{@"send":self.sendNetworkSpeed}];
}
_oBytes = oBytes;
}
@end
上一篇:集合之HashSet(含JDK1.8源码分析)


下一篇:linux系统中的时间