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

Fix of the crash in the ARTOSReachability #1453

Merged
merged 4 commits into from
Aug 1, 2022
Merged
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
39 changes: 9 additions & 30 deletions Source/Private/ARTOSReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@

#import "ARTOSReachability.h"

NSNotificationName const kARTOSReachabilityNetworkIsReachableNotification = @"ARTOSReachabilityNetworkIsReachableNotification";
NSNotificationName const kARTOSReachabilityNetworkIsDownNotification = @"ARTOSReachabilityNetworkIsDownNotification";
@interface ARTOSReachability ()
- (void)internalCallback:(BOOL)reachable;
@end

/// Global callback for network state changes
static void ARTOSReachability_Callback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
// Post a notification to notify the instance that the network reachability changed.
ARTOSReachability *reachability = (__bridge ARTOSReachability *)info;
BOOL reachable = flags & kSCNetworkReachabilityFlagsReachable;
if (reachable) {
[NSNotificationCenter.defaultCenter postNotificationName:kARTOSReachabilityNetworkIsReachableNotification object:nil];
}
else {
[NSNotificationCenter.defaultCenter postNotificationName:kARTOSReachabilityNetworkIsDownNotification object:nil];
}
[reachability internalCallback:reachable];
}

@implementation ARTOSReachability {
Expand All @@ -46,9 +42,7 @@ - (void)listenForHost:(NSString *)host callback:(void (^)(BOOL))callback {

_reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [host UTF8String]);
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(networkIsReachable) name:kARTOSReachabilityNetworkIsReachableNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(networkIsDown) name:kARTOSReachabilityNetworkIsDownNotification object:nil];

if (SCNetworkReachabilitySetCallback(_reachabilityRef, ARTOSReachability_Callback, &context)) {
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
[_logger info:@"Reachability: started listening for host %@", _host];
Expand All @@ -57,31 +51,19 @@ - (void)listenForHost:(NSString *)host callback:(void (^)(BOOL))callback {
[_logger warn:@"Reachability: failed starting listener for host %@", _host];
}
}
else {
[NSNotificationCenter.defaultCenter removeObserver:self name:kARTOSReachabilityNetworkIsReachableNotification object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self name:kARTOSReachabilityNetworkIsDownNotification object:nil];
}
}

- (void)off {
if (_reachabilityRef != NULL) {
[NSNotificationCenter.defaultCenter removeObserver:self name:kARTOSReachabilityNetworkIsReachableNotification object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self name:kARTOSReachabilityNetworkIsDownNotification object:nil];
SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
[_logger info:@"Reachability: stopped listening for host %@", _host];
SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(_reachabilityRef);
_reachabilityRef = NULL;
}
_callback = nil;
_host = nil;
}

- (void)networkIsReachable {
[self internalCallback:true];
}

- (void)networkIsDown {
[self internalCallback:false];
}

- (void)internalCallback:(BOOL)reachable {
[_logger info:@"Reachability: host %@ is reachable: %@", _host, reachable ? @"true" : @"false"];
dispatch_async(_queue, ^{
Expand All @@ -91,9 +73,6 @@ - (void)internalCallback:(BOOL)reachable {

- (void)dealloc {
[self off];
if (_reachabilityRef != NULL) {
CFRelease(_reachabilityRef);
}
}

@end