transmission/macosx/PortChecker.mm

114 lines
3.9 KiB
Plaintext
Raw Normal View History

// This file Copyright © Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
2007-09-16 01:02:06 +00:00
#import "PortChecker.h"
static NSTimeInterval const kCheckFireInterval = 3.0;
2007-12-19 20:46:00 +00:00
@interface PortChecker ()
@property(nonatomic, weak) NSObject<PortCheckerDelegate>* fDelegate;
@property(nonatomic) PortStatus fStatus;
@property(nonatomic) NSURLSession* fSession;
@property(nonatomic) NSURLSessionDataTask* fTask;
@property(nonatomic) NSTimer* fTimer;
@end
2007-09-16 01:02:06 +00:00
@implementation PortChecker
- (instancetype)initForPort:(NSInteger)portNumber delay:(BOOL)delay withDelegate:(NSObject<PortCheckerDelegate>*)delegate
2007-09-16 01:02:06 +00:00
{
if ((self = [super init]))
{
_fSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.ephemeralSessionConfiguration delegate:nil
delegateQueue:nil];
_fDelegate = delegate;
_fStatus = PortStatusChecking;
_fTimer = [NSTimer scheduledTimerWithTimeInterval:kCheckFireInterval target:self selector:@selector(startProbe:)
userInfo:@(portNumber)
repeats:NO];
if (!delay)
{
[_fTimer fire];
}
2007-09-16 01:02:06 +00:00
}
2007-09-16 01:02:06 +00:00
return self;
}
- (void)dealloc
{
[self cancelProbe];
}
- (PortStatus)status
2007-09-16 01:02:06 +00:00
{
return self.fStatus;
2007-09-16 01:02:06 +00:00
}
- (void)cancelProbe
{
[self.fTimer invalidate];
self.fTimer = nil;
[self.fTask cancel];
2007-09-16 01:02:06 +00:00
}
#pragma mark - Private
- (void)startProbe:(NSTimer*)timer
{
self.fTimer = nil;
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];
_fTask = [_fSession dataTaskWithRequest:portProbeRequest
chore: fix warnings when compiling macOS client with either Xcode or CMake (#6676) * chore: fix CGFloat comparison warnings in macOS code. There are 2 cases: 1. Speed comparisons. The lowest significant value displayed in UI is 0.1 bytes per sec. See [NSString stringForSpeed] and [NSString stringForSpeedCompact]. 2. Ratio limit comparison. The lowest significant value displayed in UI is 0.01 (no unit). This is based on maximumFractionDigits=2 set in related XIB file. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix warning about shadowed variable CGFloat const difference was used twice in the same scope Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: fix unused block parameter warnings Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable GCC_WARN_64_TO_32_BIT_CONVERSION for libtransmission This warnings are not reported with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable CLANG_WARN_STRICT_PROTOTYPES for dht This is third party target, warning is not enabled with CMake build. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> * chore: disable '-Wexit-time-destructors' warning with CMake. There are two cases when this is reported in libtransmission: 1. `log_state` in anonymous namespace in `log.cc`. 2. static inline `dh_pool_mutex` in `tr_handshake` in `handshake.h`. I don't see how this may be improved or how this affects correctness, so don't nag about that. Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com> --------- Signed-off-by: Dzmitry Neviadomski <nevack.d@gmail.com>
2024-03-15 05:16:26 +00:00
completionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable, NSError* _Nullable error) {
if (error)
{
NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
[self callBackWithStatus:PortStatusError];
return;
}
NSString* probeString = [[NSString alloc] initWithData:data ?: NSData.data encoding:NSUTF8StringEncoding];
if (!probeString)
{
NSLog(@"Unable to get port status: invalid data received");
[self callBackWithStatus:PortStatusError];
}
else if ([probeString isEqualToString:@"1"])
{
[self callBackWithStatus:PortStatusOpen];
}
else if ([probeString isEqualToString:@"0"])
{
[self callBackWithStatus:PortStatusClosed];
}
else
{
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
[self callBackWithStatus:PortStatusError];
}
}];
[_fTask resume];
}
- (void)callBackWithStatus:(PortStatus)status
2008-09-07 16:08:29 +00:00
{
self.fStatus = status;
NSObject<PortCheckerDelegate>* delegate = self.fDelegate;
[delegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
2008-09-07 16:08:29 +00:00
}
@end