refactor: replace per-torrent callbacks with per-session ones. (#3495)

This commit is contained in:
Charles Kerr
2022-07-23 01:04:34 -05:00
committed by GitHub
parent 445aad56a0
commit 47fe7c47d2
12 changed files with 209 additions and 188 deletions

View File

@@ -359,6 +359,61 @@ static void removeKeRangerRansomware()
}
}
void onStartQueue(tr_session* session, tr_torrent* tor, void* vself)
{
auto* controller = (__bridge Controller*)(vself);
auto const hashstr = @(tr_torrentView(tor).hash_string);
dispatch_async(dispatch_get_main_queue(), ^{
auto* const torrent = [controller torrentForHash:hashstr];
[torrent startQueue];
});
}
void onIdleLimitHit(tr_session* session, tr_torrent* tor, void* vself)
{
auto* const controller = (__bridge Controller*)(vself);
auto const hashstr = @(tr_torrentView(tor).hash_string);
dispatch_async(dispatch_get_main_queue(), ^{
auto* const torrent = [controller torrentForHash:hashstr];
[torrent idleLimitHit];
});
}
void onRatioLimitHit(tr_session* session, tr_torrent* tor, void* vself)
{
auto* const controller = (__bridge Controller*)(vself);
auto const hashstr = @(tr_torrentView(tor).hash_string);
dispatch_async(dispatch_get_main_queue(), ^{
auto* const torrent = [controller torrentForHash:hashstr];
[torrent ratioLimitHit];
});
}
void onMetadataCompleted(tr_session* session, tr_torrent* tor, void* vself)
{
auto* const controller = (__bridge Controller*)(vself);
auto const hashstr = @(tr_torrentView(tor).hash_string);
dispatch_async(dispatch_get_main_queue(), ^{
auto* const torrent = [controller torrentForHash:hashstr];
[torrent metadataRetrieved];
});
}
void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool wasRunning, void* vself)
{
auto* const controller = (__bridge Controller*)(vself);
auto const hashstr = @(tr_torrentView(tor).hash_string);
dispatch_async(dispatch_get_main_queue(), ^{
auto* const torrent = [controller torrentForHash:hashstr];
[torrent completenessChange:status wasRunning:wasRunning];
});
}
- (instancetype)init
{
if ((self = [super init]))
@@ -514,6 +569,12 @@ static void removeKeRangerRansomware()
_fConfigDirectory = @(default_config_dir);
tr_free(default_config_dir);
tr_sessionSetIdleLimitHitCallback(_fLib, onIdleLimitHit, (__bridge void*)(self));
tr_sessionSetQueueStartCallback(_fLib, onStartQueue, (__bridge void*)(self));
tr_sessionSetRatioLimitHitCallback(_fLib, onRatioLimitHit, (__bridge void*)(self));
tr_sessionSetMetadataCallback(_fLib, onMetadataCompleted, (__bridge void*)(self));
tr_sessionSetCompletenessCallback(_fLib, onTorrentCompletenessChanged, (__bridge void*)(self));
NSApp.delegate = self;
//register for magnet URLs (has to be in init)

View File

@@ -44,8 +44,13 @@ typedef NS_ENUM(unsigned int, TorrentDeterminationType) {
- (void)startTransferNoQueue;
- (void)startTransfer;
- (void)stopTransfer;
- (void)startQueue;
- (void)sleep;
- (void)wakeUp;
- (void)idleLimitHit;
- (void)ratioLimitHit;
- (void)metadataRetrieved;
- (void)completenessChange:(tr_completeness)status wasRunning:(BOOL)wasRunning;
@property(nonatomic) NSUInteger queuePosition;

View File

@@ -64,7 +64,6 @@
- (void)sortFileList:(NSMutableArray<FileListNode*>*)fileNodes;
- (void)startQueue;
- (void)completenessChange:(tr_completeness)status wasRunning:(BOOL)wasRunning;
- (void)ratioLimitHit;
- (void)idleLimitHit;
- (void)metadataRetrieved;
@@ -81,41 +80,6 @@
@end
void startQueueCallback(tr_torrent* torrent, void* torrentData)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge Torrent*)torrentData startQueue];
});
}
void completenessChangeCallback(tr_torrent* torrent, tr_completeness status, bool wasRunning, void* torrentData)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge Torrent*)torrentData completenessChange:status wasRunning:wasRunning];
});
}
void ratioLimitHitCallback(tr_torrent* torrent, void* torrentData)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge Torrent*)torrentData ratioLimitHit];
});
}
void idleLimitHitCallback(tr_torrent* torrent, void* torrentData)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge Torrent*)torrentData idleLimitHit];
});
}
void metadataCallback(tr_torrent* torrent, void* torrentData)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge Torrent*)torrentData metadataRetrieved];
});
}
void renameCallback(tr_torrent* torrent, char const* oldPathCharString, char const* newNameCharString, int error, void* contextInfo)
{
@autoreleasepool
@@ -1811,12 +1775,6 @@ bool trashDataFile(char const* filename, tr_error** error)
}
}
tr_torrentSetQueueStartCallback(self.fHandle, startQueueCallback, (__bridge void*)(self));
tr_torrentSetCompletenessCallback(self.fHandle, completenessChangeCallback, (__bridge void*)(self));
tr_torrentSetRatioLimitHitCallback(self.fHandle, ratioLimitHitCallback, (__bridge void*)(self));
tr_torrentSetIdleLimitHitCallback(self.fHandle, idleLimitHitCallback, (__bridge void*)(self));
tr_torrentSetMetadataCallback(self.fHandle, metadataCallback, (__bridge void*)(self));
_fResumeOnWake = NO;
//don't do after this point - it messes with auto-group functionality