Swiftgram/submodules/MtProtoKit/MTProtoKit/MTTcpConnectionBehaviour.m
Peter 373769682e Add 'submodules/MtProtoKit/' from commit '14ab734b977fd4f1686a2a13415f6a4c9b9fdd6d'
git-subtree-dir: submodules/MtProtoKit
git-subtree-mainline: 3b155750f5a4894ff3dedf1860a37e94e0ea9571
git-subtree-split: 14ab734b977fd4f1686a2a13415f6a4c9b9fdd6d
2019-06-11 18:55:34 +01:00

127 lines
2.3 KiB
Objective-C

#import <Foundation/Foundation.h>
#import "MTTcpConnectionBehaviour.h"
#import "MTTimer.h"
#import "MTQueue.h"
@interface MTTcpConnectionBehaviour ()
{
MTTimer *_backoffTimer;
NSInteger _backoffCount;
}
@end
@implementation MTTcpConnectionBehaviour
- (instancetype)initWithQueue:(MTQueue *)queue
{
self = [super init];
if (self != nil)
{
_queue = queue;
_needsReconnection = true;
}
return self;
}
- (void)dealloc
{
[self invalidateTimer];
}
- (void)requestConnection
{
if (_backoffTimer == nil) {
[self timerEvent:false];
}
}
- (void)connectionOpened
{
//_backoffCount = 0;
//[self invalidateTimer];
}
- (void)connectionValidDataReceived
{
_backoffCount = 0;
[self invalidateTimer];
}
- (void)connectionClosed
{
if (_needsReconnection)
{
_backoffCount++;
if (_backoffCount == 1)
[self timerEvent:true];
else
{
NSTimeInterval delay = 1.0;
if (_backoffCount <= 5)
delay = 1.0;
else if (_backoffCount <= 20)
delay = 4.0;
else
delay = 8.0;
[self startTimer:delay];
}
}
}
- (void)clearBackoff
{
_backoffCount = 0;
}
- (void)invalidateTimer
{
MTTimer *reconnectionTimer = _backoffTimer;
_backoffTimer = nil;
[_queue dispatchOnQueue:^
{
[reconnectionTimer invalidate];
}];
}
- (void)startTimer:(NSTimeInterval)timeout
{
[self invalidateTimer];
[_queue dispatchOnQueue:^
{
__weak MTTcpConnectionBehaviour *weakSelf = self;
_backoffTimer = [[MTTimer alloc] initWithTimeout:timeout repeat:false completion:^
{
__strong MTTcpConnectionBehaviour *strongSelf = weakSelf;
[strongSelf timerEvent:true];
} queue:[_queue nativeQueue]];
[_backoffTimer start];
}];
}
- (void)timerEvent:(bool)error
{
[self invalidateTimer];
[_queue dispatchOnQueue:^
{
id<MTTcpConnectionBehaviourDelegate> delegate = _delegate;
if ([delegate respondsToSelector:@selector(tcpConnectionBehaviourRequestsReconnection:error:)])
[delegate tcpConnectionBehaviourRequestsReconnection:self error:error];
}];
}
@end