Addressing issues with managing socket connections in background.

pull/1/head
Frederic Jacobs 11 years ago
parent 0f04132b81
commit bb1a4c1800

@ -150,7 +150,6 @@
} else{
NSString *threadId = [notification.userInfo objectForKey:Signal_Thread_UserInfo_Key];
[Environment messageThreadId:threadId];
completionHandler();
}
}

@ -45,8 +45,6 @@
#import <CocoaLumberjack/DDLog.h>
#import <YapDatabase/YapDatabaseSecondaryIndex.h>
#define ddLogLevel LOG_LEVEL_VERBOSE
@interface TSMessagesManager ()
@property SystemSoundID newMessageSound;

@ -21,8 +21,8 @@
#define kWebSocketHeartBeat 30
#define kWebSocketReconnectTry 5
#define kBackgroundConnectTimer 120
#define kBackgroundConnectKeepAlive 20
#define kBackgroundConnectTimer 10
#define kBackgroundConnectKeepAlive 15
NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
NSString * const SocketClosedNotification = @"SocketClosedNotification";
@ -31,7 +31,7 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
@interface TSSocketManager ()
@property (nonatomic, retain) NSTimer *pingTimer;
@property (nonatomic, retain) NSTimer *reconnectTimer;
@property (nonatomic, retain) NSTimer *backgroundKeepAliveTimer;
@property (nonatomic, retain) SRWebSocket *websocket;
@property (nonatomic) SocketStatus status;
@ -39,6 +39,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
@property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier;
@property BOOL didFetchInBackground;
@property (nonatomic, retain) NSTimer *backgroundKeepAliveTimer;
@property (nonatomic, retain) NSTimer *backgroundConnectTimer;
@end
@ -61,7 +63,7 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
sharedMyManager.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
sharedMyManager.didFetchInBackground = FALSE;
sharedMyManager.didFetchInBackground = NO;
});
return sharedMyManager;
}
@ -104,8 +106,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
socket = [[SRWebSocket alloc] initWithURLRequest:request];
socket.delegate = [self sharedManager];
[socket open];
[[self sharedManager] setWebsocket:socket];
[socket open];
}
+ (void)resignActivity{
@ -127,13 +129,13 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
forMode:NSDefaultRunLoopMode];
self.status = kSocketStatusOpen;
[self.reconnectTimer invalidate];
self.reconnectTimer = nil;
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
DDLogError(@"Error connecting to socket %@", error);
[self.pingTimer invalidate];
self.status = kSocketStatusClosed;
[self scheduleRetry];
@ -177,6 +179,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
}
- (void)keepAliveBackground {
[self.backgroundConnectTimer invalidate];
if (self.fetchingTaskIdentifier) {
[self.backgroundKeepAliveTimer invalidate];
@ -215,11 +219,13 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
DDLogVerbose(@"WebSocket did close");
[self.pingTimer invalidate];
self.status = kSocketStatusClosed;
if (!wasClean && [UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
[self scheduleRetry];
}
}
- (void)webSocketHeartBeat {
@try {
@ -231,7 +237,9 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
}
- (NSString*)webSocketAuthenticationString {
return [NSString stringWithFormat:@"?login=%@&password=%@", [[TSAccountManager registeredNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"],[TSStorageManager serverAuthToken]];
return [NSString stringWithFormat:@"?login=%@&password=%@",
[[TSAccountManager registeredNumber] stringByReplacingOccurrencesOfString:@"+"
withString:@"%2B"],[TSStorageManager serverAuthToken]];
}
- (void)scheduleRetry {
@ -265,11 +273,24 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
TSSocketManager *sharedInstance = [TSSocketManager sharedManager];
if (sharedInstance.fetchingTaskIdentifier == UIBackgroundTaskInvalid) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sharedInstance.backgroundConnectTimer = [NSTimer scheduledTimerWithTimeInterval:kBackgroundConnectTimer
target:sharedInstance
selector:@selector(backgroundConnectTimerExpired)
userInfo:nil
repeats:NO];
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[loop addTimer:[TSSocketManager sharedManager].backgroundConnectTimer
forMode:NSDefaultRunLoopMode];
[loop run];
});
[sharedInstance.backgroundKeepAliveTimer invalidate];
sharedInstance.didFetchInBackground = NO;
sharedInstance.fetchingTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[TSSocketManager resignActivity];
[sharedInstance closeBackgroundTask];
[[TSSocketManager sharedManager] closeBackgroundTask];
}];
[self becomeActive];
@ -278,21 +299,25 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
}
}
- (void)backgroundConnectTimerExpired {
[self backgroundTimeExpired];
}
- (void)backgroundTimeExpired {
[[self class] resignActivity];
[self closeBackgroundTask];
}
- (void)closeBackgroundTask {
UIBackgroundTaskIdentifier identifier = self.fetchingTaskIdentifier;
self.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
[self.backgroundKeepAliveTimer invalidate];
[self.backgroundConnectTimer invalidate];
if (!self.didFetchInBackground) {
[self backgroundConnectTimedOut];
}
[[UIApplication sharedApplication] endBackgroundTask:identifier];
[[UIApplication sharedApplication] endBackgroundTask:self.fetchingTaskIdentifier];
self.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
}
- (void)backgroundConnectTimedOut {

Loading…
Cancel
Save