适用于 iOS 的高度准确的 ICMP Ping 控制器(不基于 Apple 示例代码,请参阅“详情”部分)
详情
此代码是一个低级 ping 库,在不受 UI 和其他主线程处理的影响下提供极精确的往返时延结果。这与其他大多数 ping 库不同,如典型的 Apple SimplePing,这些库作为单线程类嵌入在执行主线程中,导致它们遭受各种不确定错误。这个库是在 BSD 套接字和 GCD 之上构建的多线程类,无论系统资源状态或设备性能如何,都能提供最佳可能的时延精度。
使用方法
首先导入头文件
#import <GBPing/GBPing.h>
基本使用
self.ping = [[GBPing alloc] init];
self.ping.host = @"google.com";
self.ping.delegate = self;
self.ping.timeout = 1.0;
self.ping.pingPeriod = 0.9;
[self.ping setupWithBlock:^(BOOL success, NSError *error) { //necessary to resolve hostname
if (success) {
//start pinging
[self.ping startPinging];
//stop it after 5 seconds
[NSTimer scheduledTimerWithTimeInterval:5 repeats:NO withBlock:^{
NSLog(@"stop it");
[self.ping stop];
self.ping = nil;
}];
}
else {
NSLog(@"failed to start");
}
}];
实现可选的委托方法
-(void)ping:(GBPing *)pinger didReceiveReplyWithSummary:(GBPingSummary *)summary {
NSLog(@"REPLY> %@", summary);
}
-(void)ping:(GBPing *)pinger didReceiveUnexpectedReplyWithSummary:(GBPingSummary *)summary {
NSLog(@"BREPLY> %@", summary);
}
-(void)ping:(GBPing *)pinger didSendPingWithSummary:(GBPingSummary *)summary {
NSLog(@"SENT> %@", summary);
}
-(void)ping:(GBPing *)pinger didTimeoutWithSummary:(GBPingSummary *)summary {
NSLog(@"TIMOUT> %@", summary);
}
-(void)ping:(GBPing *)pinger didFailWithError:(NSError *)error {
NSLog(@"FAIL> %@", error);
}
-(void)ping:(GBPing *)pinger didFailToSendPingWithSummary:(GBPingSummary *)summary error:(NSError *)error {
NSLog(@"FSENT> %@, %@", summary, error);
}
演示项目
参见:github.com/lmirosevic/GBPingDemo
特性
GBPing提供了以下信息(在GBPingSummaryObject中以属性的形式公开)
- NSUInteger sequenceNumber;
- NSUInteger payloadSize;
- NSUInteger ttl;
- NSString *host;
- NSDate *sendDate;
- NSDate *receiveDate;
- NSTimeInterval rtt;
- GBPingStatus status;
依赖项
无
版权 & 许可
版权 2015 Luka Mirosevic
根据Apache License,版本 2.0(以下简称“许可”)许可;除非符合许可规定或以书面形式达成协议,否则不得使用本作品。您可以在LICENSE文件或以下位置获取许可副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则在许可下分发的软件按“现状”分发,不提供任何明示或暗示的保证或条件。有关许可具体规定、权限和限制,请参阅许可。