mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-08 23:30:22 +00:00
124 lines
2.3 KiB
Objective-C
124 lines
2.3 KiB
Objective-C
/*
|
|
* This is the source code of Telegram for iOS v. 1.1
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
*
|
|
* Copyright Peter Iakovlev, 2013.
|
|
*/
|
|
|
|
#import <MTProtoKit/MTHttpWorkerBehaviour.h>
|
|
|
|
#import <MTProtoKit/MTQueue.h>
|
|
#import <MTProtoKit/MTTimer.h>
|
|
|
|
@interface MTHttpWorkerBehaviour ()
|
|
{
|
|
MTQueue *_queue;
|
|
MTTimer *_backoffTimer;
|
|
NSInteger _backoffCount;
|
|
|
|
bool _workersNeeded;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation MTHttpWorkerBehaviour
|
|
|
|
- (instancetype)initWithQueue:(MTQueue *)queue
|
|
{
|
|
self = [super init];
|
|
if (self != nil)
|
|
{
|
|
_queue = queue;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
[self invalidateTimer];
|
|
}
|
|
|
|
- (void)clearBackoff
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount = 0;
|
|
}];
|
|
}
|
|
|
|
- (void)setWorkersNeeded
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_workersNeeded = true;
|
|
|
|
if (_backoffCount <= 1)
|
|
[self timerEvent];
|
|
else
|
|
[self startTimer:1.0];
|
|
}];
|
|
}
|
|
|
|
- (void)workerConnected
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount = 0;
|
|
|
|
if (_workersNeeded)
|
|
[self timerEvent];
|
|
}];
|
|
}
|
|
|
|
- (void)workerDisconnectedWithError
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount++;
|
|
}];
|
|
}
|
|
|
|
- (void)invalidateTimer
|
|
{
|
|
MTTimer *backoffTimer = _backoffTimer;
|
|
_backoffTimer = nil;
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
[backoffTimer invalidate];
|
|
}];
|
|
}
|
|
|
|
- (void)startTimer:(NSTimeInterval)timeout
|
|
{
|
|
[self invalidateTimer];
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
__weak MTHttpWorkerBehaviour *weakSelf = self;
|
|
_backoffTimer = [[MTTimer alloc] initWithTimeout:timeout repeat:false completion:^
|
|
{
|
|
__strong MTHttpWorkerBehaviour *strongSelf = weakSelf;
|
|
[strongSelf timerEvent];
|
|
} queue:[_queue nativeQueue]];
|
|
[_backoffTimer start];
|
|
}];
|
|
}
|
|
|
|
- (void)timerEvent
|
|
{
|
|
[self invalidateTimer];
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_workersNeeded = false;
|
|
|
|
id<MTHttpWorkerBehaviourDelegate> delegate = _delegate;
|
|
if ([delegate respondsToSelector:@selector(httpWorkerBehaviourAllowsNewWorkerCreation:)])
|
|
[delegate httpWorkerBehaviourAllowsNewWorkerCreation:self];
|
|
}];
|
|
}
|
|
|
|
@end
|