diff --git a/ios/GBPing/GBPing.m b/ios/GBPing/GBPing.m index 8d1a7d2..d0ef870 100755 --- a/ios/GBPing/GBPing.m +++ b/ios/GBPing/GBPing.m @@ -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; @@ -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); @@ -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; @@ -523,7 +531,8 @@ - (void)sendPing packet = [self pingPacketWithType:kICMPv6TypeEchoRequest payload:payload requiresChecksum:NO]; } break; default: { - assert(NO); + err = errno; + return; } break; } @@ -791,7 +800,6 @@ - (BOOL)isValidPingResponsePacket:(NSMutableData *)packet result = [self isValidPing6ResponsePacket:packet]; } break; default: { - assert(NO); result = NO; } break; } @@ -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; diff --git a/ios/RNReactNativePing.m b/ios/RNReactNativePing.m index 8ea69cc..d48e2c7 100644 --- a/ios/RNReactNativePing.m +++ b/ios/RNReactNativePing.m @@ -43,9 +43,11 @@ - (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; } @@ -53,14 +55,21 @@ - (dispatch_queue_t)methodQueue 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; }]; @@ -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); + } }); }]; }