Преглед изворни кода

Add setSleeping callback

- Allows app to inform library when
  device sleeps/wakes.

- Sleep state overrides hasNetworkConnectivity
  state check, indicating no network when
  in sleep state.

- Logs sleep/wake event diagnostics.
Rod Hynes пре 7 година
родитељ
комит
0cc35a214e

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

@@ -356,6 +356,12 @@ Swift: @code func onInternetReachabilityChanged(_ currentReachability: Reachabil
  */
  */
 - (void)stop;
 - (void)stop;
 
 
+/*!
+ Indicate if the device is sleeping. This logs a diagnostic message and forces hasNetworkConnectivity to false when sleeping.
+ Swift: @code func setSleeping(_ isSleeping: Bool) @endcode
+ */
+- (void)setSleeping:(BOOL)isSleeping;
+
 /*!
 /*!
  Returns the current tunnel connection state.
  Returns the current tunnel connection state.
  @return  The current connection state.
  @return  The current connection state.

+ 14 - 0
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -53,6 +53,8 @@
     _Atomic NSInteger localSocksProxyPort;
     _Atomic NSInteger localSocksProxyPort;
     _Atomic NSInteger localHttpProxyPort;
     _Atomic NSInteger localHttpProxyPort;
 
 
+    _Atomic BOOL isSleeping;
+
     Reachability* reachability;
     Reachability* reachability;
     _Atomic NetworkStatus currentNetworkStatus;
     _Atomic NetworkStatus currentNetworkStatus;
 
 
@@ -81,6 +83,7 @@
     atomic_init(&self->connectionState, PsiphonConnectionStateDisconnected);
     atomic_init(&self->connectionState, PsiphonConnectionStateDisconnected);
     atomic_init(&self->localSocksProxyPort, 0);
     atomic_init(&self->localSocksProxyPort, 0);
     atomic_init(&self->localHttpProxyPort, 0);
     atomic_init(&self->localHttpProxyPort, 0);
+    atomic_init(&self->isSleeping, FALSE);
     self->reachability = [Reachability reachabilityForInternetConnection];
     self->reachability = [Reachability reachabilityForInternetConnection];
     atomic_init(&self->currentNetworkStatus, NotReachable);
     atomic_init(&self->currentNetworkStatus, NotReachable);
     self->tunnelWholeDevice = FALSE;
     self->tunnelWholeDevice = FALSE;
@@ -178,6 +181,12 @@
     return [self start];
     return [self start];
 }
 }
 
 
+// See comment in header
+- (void)setSleeping:(BOOL)isSleeping {
+    [self logMessage: isSleeping ? @"Sleeping" : @"Waking"];
+    atomic_store(&self->isSleeping, isSleeping);
+}
+
 /*!
 /*!
  Start the tunnel. If the tunnel is already started it will be stopped first.
  Start the tunnel. If the tunnel is already started it will be stopped first.
  Assumes self.sessionID has been initialized -- i.e., assumes that
  Assumes self.sessionID has been initialized -- i.e., assumes that
@@ -1080,6 +1089,11 @@
 }
 }
 
 
 - (long)hasNetworkConnectivity {
 - (long)hasNetworkConnectivity {
+
+    if (atomic_load(&self->isSleeping)) {
+        return FALSE;
+    }
+
     BOOL hasConnectivity = [self->reachability currentReachabilityStatus] != NotReachable;
     BOOL hasConnectivity = [self->reachability currentReachabilityStatus] != NotReachable;
 
 
     if (!hasConnectivity) {
     if (!hasConnectivity) {