Skip to content

Commit

Permalink
Increased stability (#114)
Browse files Browse the repository at this point in the history
* Update RNReactNativePing.m

prevent calling callback multiple times

* Update package.json

version update

* update ping folder

* update version

* update
  • Loading branch information
jeromediaz authored Nov 13, 2023
1 parent 2e79116 commit 2b84ab8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 14 additions & 6 deletions ios/GBPing/GBPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ - (void)listenOnce
enum { kBufferSize = 65535 };

buffer = malloc(kBufferSize);
assert(buffer);

if (buffer == nil) {
err = errno;
return;
}

//set socket timeout
struct timeval tv;
Expand All @@ -381,7 +385,7 @@ - (void)listenOnce
if (setsockopt(self.socket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
NSLog(@"Set Timeput Error");
}

//read the data.
addrLen = sizeof(addr);
bytesRead = recvfrom(self.socket, buffer, kBufferSize, 0, (struct sockaddr *)&addr, &addrLen);
Expand All @@ -402,7 +406,11 @@ - (void)listenOnce
NSMutableData *packet;

packet = [NSMutableData dataWithBytes:buffer length:(NSUInteger)bytesRead];
assert(packet);

if (packet == nil) {
err = errno;
return;
}

//complete the ping summary
const struct ICMPHeader *headerPointer;
Expand Down Expand Up @@ -523,7 +531,8 @@ - (void)sendPing
packet = [self pingPacketWithType:kICMPv6TypeEchoRequest payload:payload requiresChecksum:NO];
} break;
default: {
assert(NO);
err = errno;
return;
} break;
}

Expand Down Expand Up @@ -791,7 +800,6 @@ - (BOOL)isValidPingResponsePacket:(NSMutableData *)packet
result = [self isValidPing6ResponsePacket:packet];
} break;
default: {
assert(NO);
result = NO;
} break;
}
Expand Down Expand Up @@ -882,7 +890,7 @@ - (NSData *)pingPacketWithType:(uint8_t)type payload:(NSData *)payload requiresC
ICMPHeader *icmpPtr;

packet = [NSMutableData dataWithLength:sizeof(*icmpPtr) + payload.length];
assert(packet != nil);
if (packet == nil) { return nil; }

icmpPtr = packet.mutableBytes;
icmpPtr->type = type;
Expand Down
19 changes: 16 additions & 3 deletions ios/RNReactNativePing.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,33 @@ - (dispatch_queue_t)methodQueue
unsigned long long payloadSize = nsPayloadSize.unsignedLongLongValue;
ping.payloadSize = payloadSize;
}
__block BOOL callbackCalled = NO;

[ping setupWithBlock:^(BOOL success, NSError *_Nullable err) {
if (!success) {
callbackCalled = YES;
reject(@(err.code).stringValue,err.domain,err);
return;
}
[ping startPingingWithBlock:^(GBPingSummary *summary) {
if (!ping) {
return;
}
resolve(@(@(summary.rtt * 1000).intValue));
if (!callbackCalled) {
callbackCalled = YES;
resolve(@(@(summary.rtt * 1000).intValue));
}

[ping stop];
ping = nil;
} fail:^(NSError *_Nonnull error) {
if (!ping) {
return;
}
reject(@(error.code).stringValue,error.domain,error);
if (!callbackCalled) {
callbackCalled = YES;
reject(@(error.code).stringValue,error.domain,error);
}
[ping stop];
ping = nil;
}];
Expand All @@ -70,7 +79,11 @@ - (dispatch_queue_t)methodQueue
}
ping = nil;
DEFINE_NSError(timeoutError,PingUtil_Message_Timeout)
reject(@(timeoutError.code).stringValue,timeoutError.domain,timeoutError);

if (!callbackCalled) {
callbackCalled = YES;
reject(@(timeoutError.code).stringValue,timeoutError.domain,timeoutError);
}
});
}];
}
Expand Down

0 comments on commit 2b84ab8

Please sign in to comment.