Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些优化 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions LDNetDiagnoService/LDNetDiagnoService.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ - (void)startNetDiagnosis
if (_isRunning) {
//开始诊断traceRoute
[self recordStepInfo:@"\n开始traceroute..."];
_traceRouter = [[LDNetTraceRoute alloc] initWithMaxTTL:TRACEROUTE_MAX_TTL
timeout:TRACEROUTE_TIMEOUT
maxAttempts:TRACEROUTE_ATTEMPTS
port:TRACEROUTE_PORT];
_traceRouter = [[LDNetTraceRoute alloc] init];
_traceRouter.delegate = self;
if (_traceRouter) {
[NSThread detachNewThreadSelector:@selector(doTraceRoute:)
Expand Down Expand Up @@ -342,8 +339,8 @@ - (void)pingDialogsis:(BOOL)pingLocal
}

//不管服务器解析DNS是否可达,均需要ping指定ip地址
//[pingAdd addObject:kPingOpenServerIP];
//[pingInfo addObject:@"开放服务器"];
[pingAdd addObject:[_hostAddress objectAtIndex:0]];
[pingInfo addObject:@"开放服务器"];

[self recordStepInfo:@"\n开始ping..."];
_netPinger = [[LDNetPing alloc] init];
Expand Down Expand Up @@ -371,22 +368,44 @@ - (void)appendPingLog:(NSString *)pingLog

- (void)netPingDidEnd
{
// net
if(_netPinger != nil){
HTPingResult *pingResult = [_netPinger getPingResult];
NSString *log = [NSString stringWithFormat:@"ip=%@\nsendCount=%d\nreceiveCount=%d\nttl=%d\nlossRate=%f\nrttMin=%f\nrttAvg=%f\nrttMax=%f\nrttMDev=%f\n",
pingResult.ip,
pingResult.sendCount,
pingResult.receiveCount,
pingResult.ttl,
pingResult.lossRate,
pingResult.rttMin,
pingResult.rttAvg,
pingResult.rttMax,
pingResult.rttMDev];

[self recordStepInfo:log];


// NSMutableArray *pingInfos = [_netPinger getPingInfos];
//
// if(pingInfos != nil){
// for(int i = 0; i < pingInfos.count; i++){
// HTIPInfo *pingInfo = [pingInfos objectAtIndex:i];
// NSString *log = [NSString stringWithFormat:@"ip=%@, ttl=%u, time=%ld",
// pingInfo.ip, pingInfo.ttl, pingInfo.time];
// [self recordStepInfo:log];
// }
// }
}
}

#pragma mark - traceRouteDelegate
- (void)appendRouteLog:(NSString *)routeLog
{
[self recordStepInfo:routeLog];
}

- (void)traceRouteDidEnd
- (void)traceRecord:(HTTraceRouteRecord *)record
{
_isRunning = NO;
[self recordStepInfo:@"\n网络诊断结束\n"];
if (self.delegate && [self.delegate respondsToSelector:@selector(netDiagnosisDidEnd:)]) {
[self.delegate netDiagnosisDidEnd:_logInfo];
if(record == nil) return;
NSString *recordLog = @"******";
if(record.ip != nil){
recordLog = [NSString stringWithFormat:@"%d\t%@\t%fms\n", record.ttl, record.ip, record.avgTime];
}
[self recordStepInfo:recordLog];
}

#pragma mark - connectDelegate
Expand Down
38 changes: 35 additions & 3 deletions LDNetDiagnoService/LDNetPing.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/
@protocol LDNetPingDelegate <NSObject>
- (void)appendPingLog:(NSString *)pingLog;
//- (void)appendPingLog:(NSString *)pingLog;
- (void)netPingDidEnd;
@end

Expand All @@ -26,8 +26,36 @@
* 连续执行五次,因为每次的速度不一致,可以观察其平均速度来判断网络情况
*/
@protocol LDSimplePingDelegate;
@interface LDNetPing : NSObject <LDSimplePingDelegate> {
}

@interface HTPingResult : NSObject
@property (nonatomic, copy, readwrite) NSString *ip;
@property (nonatomic, assign, readwrite) int sendCount;
@property (nonatomic, assign, readwrite) int receiveCount;
@property (nonatomic, assign, readwrite) int ttl;
/**
* max is 1.0
*/
@property (nonatomic, assign, readwrite) float lossRate;
/**
* rtt is ms
*/
@property (nonatomic, assign, readwrite) float rttMin;
@property (nonatomic, assign, readwrite) float rttAvg;
@property (nonatomic, assign, readwrite) float rttMax;
@property (nonatomic, assign, readwrite) float rttMDev;

@property (nonatomic, copy, readwrite) NSMutableArray *pingInfos;
@end

@interface HTPingInfo : NSObject
@property (nonatomic, assign, readwrite) BOOL isSuccess;
@property (nonatomic, copy, readwrite) NSString *errorText;
@property (nonatomic, copy, readwrite) NSString *ip;
@property (nonatomic, assign, readwrite) unsigned int ttl;
@property (nonatomic, assign, readwrite) long time;
@end

@interface LDNetPing : NSObject <LDSimplePingDelegate>

@property (nonatomic, weak, readwrite) id<LDNetPingDelegate> delegate;

Expand All @@ -41,4 +69,8 @@
*/
- (void)stopPing;

- (HTPingResult*)getPingResult;

@end


144 changes: 115 additions & 29 deletions LDNetDiagnoService/LDNetPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "LDNetPing.h"
#import "LDNetTimer.h"

#define MAXCOUNT_PING 4
#define MAXCOUNT_PING 10

@interface LDNetPing () {
BOOL _isStartSuccess; //监测第一次ping是否成功
Expand All @@ -20,6 +20,7 @@ @interface LDNetPing () {
NSString *_hostAddress; //目标域名的IP地址
BOOL _isLargePing;
NSTimer *timer;
NSMutableArray *pingInfoArray;
}

@property (nonatomic, strong, readwrite) LDSimplePing *pinger;
Expand All @@ -36,6 +37,61 @@ - (void)dealloc
[self->_pinger stop];
}

-(HTPingResult*)getPingResult
{
HTPingResult *pingResult = [[HTPingResult alloc] init];
HTPingInfo *successPingInfo = nil;
int successCount = 0;
long rttMin = LONG_MAX;
long rttMax = 0;
long rttTotal = 0;
for(int i = 0; i < pingInfoArray.count; i++){
HTPingInfo *item = [pingInfoArray objectAtIndex:i];
if(item != nil){
if(item.isSuccess && successPingInfo == nil){
successPingInfo = item;
}
if(item.isSuccess){
successCount++;

if(item.time > rttMax){
rttMax = item.time;
}
if(item.time < rttMin){
rttMin = item.time;
}
rttTotal += item.time;
}
}
}
if(successPingInfo != nil){
pingResult.ip = successPingInfo.ip;
pingResult.ttl = successPingInfo.ttl;
}
pingResult.sendCount = (int)pingInfoArray.count;
pingResult.receiveCount = successCount;
pingResult.lossRate = (float)(pingResult.sendCount - pingResult.receiveCount) / pingResult.sendCount;

pingResult.rttMin = rttMin / 1000.0;
pingResult.rttMax = rttMax / 1000.0;

long rttAvg = rttTotal / successCount;

pingResult.rttAvg = rttAvg / 1000.0;

float totalDiff = 0;
for(int i = 0; i < pingInfoArray.count; i++){
HTPingInfo *item = [pingInfoArray objectAtIndex:i];
if(item != nil && item.isSuccess){
totalDiff += ABS(item.time - rttAvg);
}
}
pingResult.rttMDev = totalDiff / successCount / 1000.0;
pingResult.pingInfos = pingInfoArray;

return pingResult;
}


/**
* 停止当前ping动作
Expand All @@ -53,6 +109,12 @@ - (void)stopPing
* @param hostName 指定域名
*/
- (void)runWithHostName:(NSString *)hostName normalPing:(BOOL)normalPing{

if(pingInfoArray == nil){
pingInfoArray = [[NSMutableArray alloc] init];
}
[pingInfoArray removeAllObjects];

assert(self.pinger == nil);
self.pinger = [LDSimplePing simplePingWithHostName:hostName];
assert(self.pinger != nil);
Expand Down Expand Up @@ -115,9 +177,14 @@ - (void)pingTimeout:(NSTimer *)index
_sendCount > 1) {
NSString *timeoutLog =
[NSString stringWithFormat:@"ping: cannot resolve %@: TimeOut", _hostAddress];
if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
[self.delegate appendPingLog:timeoutLog];
}

HTPingInfo *ipInfo = [[HTPingInfo alloc] init];
ipInfo.isSuccess = NO;
ipInfo.errorText = timeoutLog;
ipInfo.ip = _hostAddress;
[pingInfoArray addObject:ipInfo];


[self sendPing];
}
}
Expand Down Expand Up @@ -150,11 +217,10 @@ - (void)simplePing:(LDSimplePing *)pinger didFailWithError:(NSError *)error
#pragma unused(pinger)
assert(pinger == self.pinger);
#pragma unused(error)
NSString *failCreateLog = [NSString stringWithFormat:@"#%u try create failed: %@", _sendCount,
[self shortErrorFromError:error]];
if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
[self.delegate appendPingLog:failCreateLog];
}

NSLog(@"#%u try create failed: %@", _sendCount,
[self shortErrorFromError:error]);


//如果不是创建套接字失败,都是发送数据过程中的错误,可以继续try发送数据
if (_isStartSuccess) {
Expand Down Expand Up @@ -195,10 +261,11 @@ - (void)simplePing:(LDSimplePing *)pinger
(unsigned int)OSSwapBigToHostInt16(
((const ICMPHeader *)[packet bytes])->sequenceNumber),
[self shortErrorFromError:error]];
//记录
if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
[self.delegate appendPingLog:sendFailLog];
}
HTPingInfo *ipInfo = [[HTPingInfo alloc] init];
ipInfo.isSuccess = NO;
ipInfo.errorText = sendFailLog;
ipInfo.ip = _hostAddress;
[pingInfoArray addObject:ipInfo];

[self sendPing];
}
Expand All @@ -213,18 +280,26 @@ - (void)simplePing:(LDSimplePing *)pinger didReceivePingResponsePacket:(NSData *
#pragma unused(pinger)
assert(pinger == self.pinger);
#pragma unused(packet)

NSString *successLog = [NSString
stringWithFormat:@"%lu bytes from %@ icmp_seq=#%u ttl=%d time=%ldms",
(unsigned long)[packet length], _hostAddress,
(unsigned int)OSSwapBigToHostInt16(
[LDSimplePing icmpInPacket:packet]->sequenceNumber),
(unsigned int)([LDSimplePing ipHeaderInPacket:packet]->timeToLive),
[LDNetTimer computeDurationSince:_startTime] / 1000];
//记录ping成功的数据
if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
[self.delegate appendPingLog:successLog];
}

//add pinginfo to array
HTPingInfo *ipInfo = [[HTPingInfo alloc] init];
ipInfo.isSuccess = YES;
ipInfo.ip = _hostAddress;
ipInfo.ttl = (unsigned int)([LDSimplePing ipHeaderInPacket:packet]->timeToLive);
ipInfo.time = [LDNetTimer computeDurationSince:_startTime];
[pingInfoArray addObject:ipInfo];

// NSString *successLog = [NSString
// stringWithFormat:@"%lu bytes from %@ icmp_seq=#%u ttl=%d time=%ldms",
// (unsigned long)[packet length], _hostAddress,
// (unsigned int)OSSwapBigToHostInt16(
// [LDSimplePing icmpInPacket:packet]->sequenceNumber),
// (unsigned int)([LDSimplePing ipHeaderInPacket:packet]->timeToLive),
// [LDNetTimer computeDurationSince:_startTime] / 1000];
// //记录ping成功的数据
// if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
// [self.delegate appendPingLog:successLog];
// }

[self sendPing];
}
Expand All @@ -250,10 +325,12 @@ - (void)simplePing:(LDSimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)p
errorLog = [NSString stringWithFormat:@"#%u try unexpected packet size=%zu", _sendCount,
(size_t)[packet length]];
}
//记录
if (self.delegate && [self.delegate respondsToSelector:@selector(appendPingLog:)]) {
[self.delegate appendPingLog:errorLog];
}

HTPingInfo *ipInfo = [[HTPingInfo alloc] init];
ipInfo.isSuccess = NO;
ipInfo.errorText = errorLog;
ipInfo.ip = _hostAddress;
[pingInfoArray addObject:ipInfo];
}

//当检测到错误数据的时候,再次发送
Expand Down Expand Up @@ -331,4 +408,13 @@ - (NSString *)shortErrorFromError:(NSError *)error
return result;
}

@end

@implementation HTPingResult

@end

@implementation HTPingInfo


@end
Loading