Jelajahi Sumber

Merge pull request #409 from efryntov/master

Call through to delegate when reachability changes
Rod Hynes 8 tahun lalu
induk
melakukan
14ffe39571

+ 6 - 9
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.h

@@ -183,15 +183,12 @@ typedef NS_ENUM(NSInteger, PsiphonConnectionState)
 - (void)onExiting;
 
 /*!
- Called when the device's Internet connection state is interrupted.
- This may mean that it had connectivity and now doesn't, or went from Wi-Fi to
- WWAN or vice versa.
- @note For many/most apps, the response to this callback should be to restart
- the Psiphon tunnel. It will eventually notice and begin reconnecting, but it
- may take much longer, depending on attempts to use the tunnel.
- Swift: @code func onDeviceInternetConnectivityInterrupted() @endcode
- */
-- (void)onDeviceInternetConnectivityInterrupted;
+Called when the device's Internet connection state has changed.
+This may mean that it had connectivity and now doesn't, or went from Wi-Fi to
+WWAN or vice versa or VPN state changed
+Swift: @code func onInternetReachabilityChanged(_ currentReachability: Reachability) @endcode
+*/
+- (void)onInternetReachabilityChanged:(Reachability*_Nonnull)currentReachability;
 
 /*!
  Called when tunnel-core determines which server egress regions are available

+ 7 - 16
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -1056,27 +1056,18 @@
 - (void)internetReachabilityChanged:(NSNotification *)note {
     // Invalidate initialDNSCache.
     __sync_bool_compare_and_swap(&self->useInitialDNS, TRUE, FALSE);
-    
-
-    // If we lose network while connected, we're going to force a reconnect in
-    // order to trigger the waiting-for-network state. The reason we don't wait
-    // for the tunnel to notice the network loss is that it might take 30 seconds.
 
     Reachability* currentReachability = [note object];
-    NetworkStatus networkStatus = [currentReachability currentReachabilityStatus];
 
-    PsiphonConnectionState currentConnectionState = [self getConnectionState];
-
-    if (currentConnectionState == PsiphonConnectionStateConnected &&
-        self->previousNetworkStatus != NotReachable &&
-        self->previousNetworkStatus != networkStatus) {
-        if ([self.tunneledAppDelegate respondsToSelector:@selector(onDeviceInternetConnectivityInterrupted)]) {
-            dispatch_async(self->callbackQueue, ^{
-                [self.tunneledAppDelegate onDeviceInternetConnectivityInterrupted];
-            });
-        }
+    // Just pass current reachability through to the delegate
+    // as soon as we network reachability change is detected
+    if ([self.tunneledAppDelegate respondsToSelector:@selector(onInternetReachabilityChanged:)]) {
+        dispatch_async(self->callbackQueue, ^{
+            [self.tunneledAppDelegate onInternetReachabilityChanged:currentReachability];
+        });
     }
 
+    NetworkStatus networkStatus = [currentReachability currentReachabilityStatus];
     self->previousNetworkStatus = networkStatus;
 }
 

+ 2 - 0
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Reachability/Reachability.h

@@ -53,6 +53,8 @@ extern NSString *kReachabilityChangedNotification;
 - (void)stopNotifier;
 
 - (NetworkStatus)currentReachabilityStatus;
+- (NSString*)currentReachabilityFlagsToString;
+
 
 /*!
  * WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand.

+ 25 - 25
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Reachability/Reachability.m

@@ -25,29 +25,6 @@ NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotifi
 
 #pragma mark - Supporting functions
 
-#define kShouldPrintReachabilityFlags 0
-
-static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
-{
-#if kShouldPrintReachabilityFlags
-
-    NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
-          (flags & kSCNetworkReachabilityFlagsIsWWAN)				? 'W' : '-',
-          (flags & kSCNetworkReachabilityFlagsReachable)            ? 'R' : '-',
-
-          (flags & kSCNetworkReachabilityFlagsTransientConnection)  ? 't' : '-',
-          (flags & kSCNetworkReachabilityFlagsConnectionRequired)   ? 'c' : '-',
-          (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)  ? 'C' : '-',
-          (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
-          (flags & kSCNetworkReachabilityFlagsConnectionOnDemand)   ? 'D' : '-',
-          (flags & kSCNetworkReachabilityFlagsIsLocalAddress)       ? 'l' : '-',
-          (flags & kSCNetworkReachabilityFlagsIsDirect)             ? 'd' : '-',
-          comment
-          );
-#endif
-}
-
-
 static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
 {
 #pragma unused (target, flags)
@@ -132,7 +109,7 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
 
 	if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
 	{
-		if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
+		if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode))
 		{
 			returnValue = YES;
 		}
@@ -165,7 +142,6 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
 
 - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
 {
-	PrintReachabilityFlags(flags, "networkStatusForFlags");
 	if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
 	{
 		// The target host is not reachable.
@@ -238,5 +214,29 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
 	return returnValue;
 }
 
+- (NSString*)currentReachabilityFlagsToString
+{
+	NSAssert(_reachabilityRef != NULL, @"currentReachabilityFlags called with NULL SCNetworkReachabilityRef");
+	SCNetworkReachabilityFlags flags;
+
+	if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) {
+		return [NSString stringWithFormat:@"%c%c %c%c%c%c%c%c%c",
+				(flags & kSCNetworkReachabilityFlagsIsWWAN)               ? 'W' : '-',
+				(flags & kSCNetworkReachabilityFlagsReachable)            ? 'R' : '-',
+
+				(flags & kSCNetworkReachabilityFlagsTransientConnection)  ? 't' : '-',
+				(flags & kSCNetworkReachabilityFlagsConnectionRequired)   ? 'c' : '-',
+				(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)  ? 'C' : '-',
+				(flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
+				(flags & kSCNetworkReachabilityFlagsConnectionOnDemand)   ? 'D' : '-',
+				(flags & kSCNetworkReachabilityFlagsIsLocalAddress)       ? 'l' : '-',
+				(flags & kSCNetworkReachabilityFlagsIsDirect)             ? 'd' : '-'
+				];
+	}
+
+	return @"";
+}
+
+
 
 @end