From b7e24c664b770fa4e336fd77f5b4e0ac7a90e494 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 7 Apr 2017 21:31:10 -0400 Subject: [PATCH] Rework socket manager. // FREEBIE --- src/Network/WebSockets/TSSocketManager.h | 2 ++ src/Network/WebSockets/TSSocketManager.m | 40 +----------------------- 2 files changed, 3 insertions(+), 39 deletions(-) diff --git a/src/Network/WebSockets/TSSocketManager.h b/src/Network/WebSockets/TSSocketManager.h index cc9123a40..d6909a26c 100644 --- a/src/Network/WebSockets/TSSocketManager.h +++ b/src/Network/WebSockets/TSSocketManager.h @@ -22,6 +22,8 @@ extern NSString *const SocketConnectingNotification; // open or connecting _and_ keep it open for at least N seconds. // If the app is in the background and the socket is already open or connecting this // might prolong how long we keep the socket open. +// +// This method can be called from any thread. + (void)requestSocketOpen; + (void)sendNotification; diff --git a/src/Network/WebSockets/TSSocketManager.m b/src/Network/WebSockets/TSSocketManager.m index 660b4f268..29710c846 100644 --- a/src/Network/WebSockets/TSSocketManager.m +++ b/src/Network/WebSockets/TSSocketManager.m @@ -463,12 +463,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; - (void)setFetchingTaskIdentifier:(UIBackgroundTaskIdentifier)fetchingTaskIdentifier { - DDLogWarn(@"%s %lu (%d) -> %lu (%d)", - __PRETTY_FUNCTION__, - (unsigned long)_fetchingTaskIdentifier, - _fetchingTaskIdentifier != UIBackgroundTaskInvalid, - (unsigned long)fetchingTaskIdentifier, - fetchingTaskIdentifier != UIBackgroundTaskInvalid); _fetchingTaskIdentifier = fetchingTaskIdentifier; } @@ -518,9 +512,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; return NO; } - DDLogInfo( - @"%s shouldSocketBeOpen?: %f", __PRETTY_FUNCTION__, [self.backgroundKeepAliveUntilDate timeIntervalSinceNow]); - if (self.appIsActive) { // If app is active, keep web socket alive. return YES; @@ -552,11 +543,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; // Set up state used to keep socket alive in background. self.backgroundKeepAliveUntilDate = [NSDate dateWithTimeIntervalSinceNow:durationSeconds]; - DDLogInfo(@"%s creating socket background expiration[%f]: %f", - __PRETTY_FUNCTION__, - durationSeconds, - [self.backgroundKeepAliveUntilDate timeIntervalSinceNow]); - // To be defensive, clean up any existing backgroundKeepAliveTimer. [self.backgroundKeepAliveTimer invalidate]; // Start a new timer that will fire every second while the socket is open in the background. @@ -573,7 +559,7 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ OWSAssert([NSThread isMainThread]); - DDLogWarn(@"%s background task expired", __PRETTY_FUNCTION__); + DDLogInfo(@"%s background task expired", __PRETTY_FUNCTION__); [self clearBackgroundState]; [self applyDesiredSocketState]; @@ -585,18 +571,7 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; OWSAssert([self.backgroundKeepAliveTimer isValid]); OWSAssert(self.fetchingTaskIdentifier != UIBackgroundTaskInvalid); - DDLogInfo(@"%s postponing socket background expiration[%f]?: %f -> %f", - __PRETTY_FUNCTION__, - durationSeconds, - [self.backgroundKeepAliveUntilDate timeIntervalSinceNow], - [[NSDate dateWithTimeIntervalSinceNow:durationSeconds] timeIntervalSinceNow]); if ([self.backgroundKeepAliveUntilDate timeIntervalSinceNow] < durationSeconds) { - DDLogInfo(@"%s postponing socket background expiration[%f]: %f -> %f", - __PRETTY_FUNCTION__, - durationSeconds, - [self.backgroundKeepAliveUntilDate timeIntervalSinceNow], - [[NSDate dateWithTimeIntervalSinceNow:durationSeconds] timeIntervalSinceNow]); - // Update state used to keep socket alive in background. self.backgroundKeepAliveUntilDate = [NSDate dateWithTimeIntervalSinceNow:durationSeconds]; } @@ -614,14 +589,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; + (void)requestSocketOpen { - DDLogWarn(@"%s, ", __PRETTY_FUNCTION__); - DDLogWarn(@"%s active: %d, inactive: %d, background: %d, ", - __PRETTY_FUNCTION__, - [UIApplication sharedApplication].applicationState == UIApplicationStateActive, - [UIApplication sharedApplication].applicationState == UIApplicationStateInactive, - [UIApplication sharedApplication].applicationState == UIApplicationStateBackground); - [DDLog flushLog]; - DispatchMainThreadSafe(^{ [[self sharedManager] ensureSocketAliveForSeconds:kBackgroundOpenSocketDurationSeconds]; }); @@ -652,8 +619,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; { OWSAssert([NSThread isMainThread]); - DDLogWarn(@"%s", __PRETTY_FUNCTION__); - self.backgroundKeepAliveUntilDate = nil; [self.backgroundKeepAliveTimer invalidate]; self.backgroundKeepAliveTimer = nil; @@ -698,7 +663,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; - (void)applicationDidBecomeActive:(NSNotification *)notification { OWSAssert([NSThread isMainThread]); - DDLogWarn(@"%s", __PRETTY_FUNCTION__); self.appIsActive = YES; [self applyDesiredSocketState]; @@ -707,7 +671,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; - (void)applicationWillResignActive:(NSNotification *)notification { OWSAssert([NSThread isMainThread]); - DDLogWarn(@"%s", __PRETTY_FUNCTION__); self.appIsActive = NO; // TODO: It might be nice to use `ensureSocketAliveForSeconds:` to @@ -719,7 +682,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; - (void)registrationStateDidChange:(NSNotification *)notification { OWSAssert([NSThread isMainThread]); - DDLogWarn(@"%s", __PRETTY_FUNCTION__); [self applyDesiredSocketState]; }