diff --git a/macosx/PortChecker.h b/macosx/PortChecker.h index 8dd41aa11..32e20227d 100644 --- a/macosx/PortChecker.h +++ b/macosx/PortChecker.h @@ -4,6 +4,8 @@ #import +NS_ASSUME_NONNULL_BEGIN + typedef NS_ENUM(unsigned int, port_status_t) { // PORT_STATUS_CHECKING, PORT_STATUS_OPEN, @@ -27,3 +29,5 @@ typedef NS_ENUM(unsigned int, port_status_t) { // - (void)portCheckerDidFinishProbing:(PortChecker*)portChecker; @end + +NS_ASSUME_NONNULL_END diff --git a/macosx/PortChecker.mm b/macosx/PortChecker.mm index a50836ee9..dd068b07b 100644 --- a/macosx/PortChecker.mm +++ b/macosx/PortChecker.mm @@ -11,8 +11,8 @@ static NSTimeInterval const kCheckFireInterval = 3.0; @property(nonatomic, weak) NSObject* fDelegate; @property(nonatomic) port_status_t fStatus; -@property(nonatomic) NSURLConnection* fConnection; -@property(nonatomic) NSMutableData* fPortProbeData; +@property(nonatomic) NSURLSession* fSession; +@property(nonatomic) NSURLSessionDataTask* fTask; @property(nonatomic) NSTimer* fTimer; @@ -24,6 +24,8 @@ static NSTimeInterval const kCheckFireInterval = 3.0; { if ((self = [super init])) { + _fSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.ephemeralSessionConfiguration delegate:nil + delegateQueue:nil]; _fDelegate = delegate; _fStatus = PORT_STATUS_CHECKING; @@ -42,7 +44,7 @@ static NSTimeInterval const kCheckFireInterval = 3.0; - (void)dealloc { - [_fTimer invalidate]; + [self cancelProbe]; } - (port_status_t)status @@ -55,51 +57,7 @@ static NSTimeInterval const kCheckFireInterval = 3.0; [self.fTimer invalidate]; self.fTimer = nil; - [self.fConnection cancel]; -} - -- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response -{ - self.fPortProbeData.length = 0; -} - -- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data -{ - [self.fPortProbeData appendData:data]; -} - -- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error -{ - NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription); - [self callBackWithStatus:PORT_STATUS_ERROR]; -} - -- (void)connectionDidFinishLoading:(NSURLConnection*)connection -{ - NSString* probeString = [[NSString alloc] initWithData:self.fPortProbeData encoding:NSUTF8StringEncoding]; - self.fPortProbeData = nil; - - if (probeString) - { - if ([probeString isEqualToString:@"1"]) - { - [self callBackWithStatus:PORT_STATUS_OPEN]; - } - else if ([probeString isEqualToString:@"0"]) - { - [self callBackWithStatus:PORT_STATUS_CLOSED]; - } - else - { - NSLog(@"Unable to get port status: invalid response (%@)", probeString); - [self callBackWithStatus:PORT_STATUS_ERROR]; - } - } - else - { - NSLog(@"Unable to get port status: invalid data received"); - [self callBackWithStatus:PORT_STATUS_ERROR]; - } + [self.fTask cancel]; } #pragma mark - Private @@ -108,30 +66,48 @@ static NSTimeInterval const kCheckFireInterval = 3.0; { self.fTimer = nil; - NSString* urlString = [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", [timer.userInfo integerValue]]; + NSString* urlString = [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", [(NSNumber*)timer.userInfo integerValue]]; NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15.0]; - if ((self.fConnection = [[NSURLConnection alloc] initWithRequest:portProbeRequest delegate:self])) - { - self.fPortProbeData = [[NSMutableData alloc] init]; - } - else - { - NSLog(@"Unable to get port status: failed to initiate connection"); - [self callBackWithStatus:PORT_STATUS_ERROR]; - } + _fTask = [_fSession dataTaskWithRequest:portProbeRequest + completionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable response, NSError* _Nullable error) { + if (error) + { + NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription); + [self callBackWithStatus:PORT_STATUS_ERROR]; + return; + } + NSString* probeString = [[NSString alloc] initWithData:data ?: NSData.data encoding:NSUTF8StringEncoding]; + if (!probeString) + { + NSLog(@"Unable to get port status: invalid data received"); + [self callBackWithStatus:PORT_STATUS_ERROR]; + } + else if ([probeString isEqualToString:@"1"]) + { + [self callBackWithStatus:PORT_STATUS_OPEN]; + } + else if ([probeString isEqualToString:@"0"]) + { + [self callBackWithStatus:PORT_STATUS_CLOSED]; + } + else + { + NSLog(@"Unable to get port status: invalid response (%@)", probeString); + [self callBackWithStatus:PORT_STATUS_ERROR]; + } + }]; + [_fTask resume]; } - (void)callBackWithStatus:(port_status_t)status { self.fStatus = status; - if (self.fDelegate && [self.fDelegate respondsToSelector:@selector(portCheckerDidFinishProbing:)]) - { - [self.fDelegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO]; - } + NSObject* delegate = self.fDelegate; + [delegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO]; } @end diff --git a/macosx/PrefsController.mm b/macosx/PrefsController.mm index cb62b82eb..a66c6eab9 100644 --- a/macosx/PrefsController.mm +++ b/macosx/PrefsController.mm @@ -495,7 +495,7 @@ static NSString* const kWebUIURLFormat = @"http://localhost:%ld/"; case PORT_STATUS_CHECKING: break; default: - NSAssert1(NO, @"Port checker returned invalid status: %d", self.fPortChecker.status); + NSAssert(NO, @"Port checker returned invalid status: %d", self.fPortChecker.status); break; } self.fPortChecker = nil;