mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 16:30:41 +00:00
Revert "NSURLSession Reimplementation (#411)"
This reverts commit 07233534e6
.
This commit is contained in:
parent
07233534e6
commit
3fedf31c2d
50 changed files with 7197 additions and 5391 deletions
76
Source/GSTimeoutSource.m
Normal file
76
Source/GSTimeoutSource.m
Normal file
|
@ -0,0 +1,76 @@
|
|||
#import "GSTimeoutSource.h"
|
||||
|
||||
@implementation GSTimeoutSource
|
||||
|
||||
- (instancetype) initWithQueue: (dispatch_queue_t)queue
|
||||
handler: (dispatch_block_t)handler
|
||||
{
|
||||
if (nil != (self = [super init]))
|
||||
{
|
||||
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
||||
dispatch_source_set_event_handler(timer, handler);
|
||||
dispatch_source_set_cancel_handler(timer, ^{
|
||||
dispatch_release(timer);
|
||||
});
|
||||
|
||||
_timer = timer;
|
||||
_timeoutMs = -1;
|
||||
_isSuspended = YES;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[self cancel];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSInteger) timeout
|
||||
{
|
||||
return _timeoutMs;
|
||||
}
|
||||
|
||||
- (void) setTimeout: (NSInteger)timeoutMs
|
||||
{
|
||||
if (timeoutMs >= 0)
|
||||
{
|
||||
_timeoutMs = timeoutMs;
|
||||
|
||||
dispatch_source_set_timer(_timer,
|
||||
dispatch_time(DISPATCH_TIME_NOW, timeoutMs * NSEC_PER_MSEC),
|
||||
DISPATCH_TIME_FOREVER, // don't repeat
|
||||
timeoutMs * 0.05); // 5% leeway
|
||||
|
||||
if (_isSuspended)
|
||||
{
|
||||
_isSuspended = NO;
|
||||
dispatch_resume(_timer);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
[self suspend];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)suspend
|
||||
{
|
||||
if (!_isSuspended)
|
||||
{
|
||||
_isSuspended = YES;
|
||||
_timeoutMs = -1;
|
||||
dispatch_suspend(_timer);
|
||||
}
|
||||
}
|
||||
|
||||
- (void) cancel
|
||||
{
|
||||
if (_timer)
|
||||
{
|
||||
dispatch_source_cancel(_timer);
|
||||
_timer = NULL; // released in cancel handler
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
Loading…
Add table
Add a link
Reference in a new issue