Skip to content

Commit

Permalink
Improves ping
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLetterer committed May 13, 2015
1 parent ca92865 commit 3cc783f
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions SPLPing/SPLPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

@interface SPLPing ()

@property (nonatomic, assign) BOOL hasScheduledNextPing;

@property (nonatomic, readonly) NSData *ipv4Address;

@property (nonatomic, assign) CFSocketRef socket;
Expand Down Expand Up @@ -165,7 +167,7 @@ - (instancetype)initWithIPv4Address:(NSString *)ipv4Address configuration:(SPLPi
- (void)dealloc
{
[self stop];

CFRunLoopSourceInvalidate(_socketSource), CFRelease(_socketSource), _socketSource = nil;
CFRelease(_socket), _socket = nil;
}
Expand Down Expand Up @@ -200,10 +202,7 @@ - (void)stop

- (void)socket:(CFSocketRef)socket didReadData:(NSData *)data
{
if (self.timeoutBlock) {
dispatch_block_cancel(self.timeoutBlock);
self.timeoutBlock = nil;
}
NSParameterAssert([NSThread currentThread].isMainThread);

NSData *ipHeaderData = nil, *ipData = nil, *icmpHeaderData = nil, *icmpData = nil;
if (!ICMPExtractResponseFromData(data, &ipHeaderData, &ipData, &icmpHeaderData, &icmpData)) {
Expand All @@ -226,17 +225,6 @@ - (void)socket:(CFSocketRef)socket didReadData:(NSData *)data
uint16_t identifier = OSSwapHostToBigInt16(icmpHeader->identifier);
uint16_t sequenceNumber = OSSwapHostToBigInt16(icmpHeader->sequenceNumber);

if (identifier != self.identifier || sequenceNumber != self.currentSequenceNumber) {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:@{}];
SPLPingResponse *response = [[SPLPingResponse alloc] initWithSequenceNumber:self.currentSequenceNumber duration:[[NSDate date] timeIntervalSinceDate:self.currentStartDate] identifier:self.identifier ipAddress:nil error:error];

if (self.observer) {
self.observer(self, response);
}

return [self _scheduleNextPing];
}

if (self.observer) {
self.observer(self, [[SPLPingResponse alloc] initWithSequenceNumber:sequenceNumber duration:[[NSDate date] timeIntervalSinceDate:self.currentStartDate] identifier:identifier ipAddress:ipString error:nil]);
}
Expand All @@ -248,10 +236,14 @@ - (void)socket:(CFSocketRef)socket didReadData:(NSData *)data

- (void)_sendPing
{
NSParameterAssert([NSThread currentThread].isMainThread);

if (!self.isPinging) {
return;
}

NSParameterAssert(self.timeoutBlock == nil);

self.currentSequenceNumber++;
self.currentStartDate = [NSDate date];

Expand Down Expand Up @@ -281,8 +273,13 @@ - (void)_sendPing
NSParameterAssert(socketError == kCFSocketSuccess);

__weak typeof(self) weakSelf = self;
uint16_t currentSequenceNumber = self.currentSequenceNumber;

self.timeoutBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
__strong typeof(self) self = weakSelf;
if (currentSequenceNumber != self.currentSequenceNumber) {
return;
}

self.timeoutBlock = nil;
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:@{}];
Expand All @@ -299,11 +296,23 @@ - (void)_sendPing

- (void)_scheduleNextPing
{
__weak typeof(self) weakSelf = self;
NSParameterAssert([NSThread currentThread].isMainThread);

if (self.hasScheduledNextPing) {
return;
}

self.hasScheduledNextPing = YES;
if (self.timeoutBlock) {
dispatch_block_cancel(self.timeoutBlock);
self.timeoutBlock = nil;
}

__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.configuration.pingInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong typeof(self) self = weakSelf;
[self _sendPing];
self.hasScheduledNextPing = NO;
});
}

Expand Down

0 comments on commit 3cc783f

Please sign in to comment.