aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m
diff options
context:
space:
mode:
Diffstat (limited to 'src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m')
-rw-r--r--src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m79
1 files changed, 36 insertions, 43 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m b/src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m
index fec6391d39..40afca2d3d 100644
--- a/src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m
+++ b/src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m
@@ -58,20 +58,19 @@
}
*/
-#define GRPC_XMACRO_ITEM(methodName, FlagName) \
- -(BOOL)methodName { \
- return !!(_flags & kSCNetworkReachabilityFlags##FlagName); \
- }
+#define GRPC_XMACRO_ITEM(methodName, FlagName) \
+- (BOOL)methodName { \
+ return !!(_flags & kSCNetworkReachabilityFlags ## FlagName); \
+}
#include "GRPCReachabilityFlagNames.xmacro.h"
#undef GRPC_XMACRO_ITEM
- (BOOL)isHostReachable {
- // Note: connectionOnDemand means it'll be reachable only if using the
- // CFSocketStream API or APIs on top of it.
- // connectionRequired means we can't tell until a connection is attempted
- // (e.g. for VPN on demand).
- return self.reachable && !self.interventionRequired &&
- !self.connectionOnDemand;
+ // Note: connectionOnDemand means it'll be reachable only if using the CFSocketStream API or APIs
+ // on top of it.
+ // connectionRequired means we can't tell until a connection is attempted (e.g. for VPN on
+ // demand).
+ return self.reachable && !self.interventionRequired && !self.connectionOnDemand;
}
- (NSString *)description {
@@ -80,9 +79,9 @@
/*
* For each flag, add its name to the array if it's ON. Example:
- if (self.isCell) {
- [activeOptions addObject:@"isCell"];
- }
+ if (self.isCell) {
+ [activeOptions addObject:@"isCell"];
+ }
*/
#define GRPC_XMACRO_ITEM(methodName, FlagName) \
@@ -92,14 +91,12 @@
#include "GRPCReachabilityFlagNames.xmacro.h"
#undef GRPC_XMACRO_ITEM
- return activeOptions.count == 0
- ? @"(none)"
- : [activeOptions componentsJoinedByString:@", "];
+ return activeOptions.count == 0 ? @"(none)" : [activeOptions componentsJoinedByString:@", "];
}
- (BOOL)isEqual:(id)object {
return [object isKindOfClass:[GRPCReachabilityFlags class]] &&
- _flags == ((GRPCReachabilityFlags *)object)->_flags;
+ _flags == ((GRPCReachabilityFlags *)object)->_flags;
}
- (NSUInteger)hash {
@@ -109,16 +106,15 @@
#pragma mark Connectivity Monitor
-// Assumes the third argument is a block that accepts a GRPCReachabilityFlags
-// object, and passes the received ones to it.
+// Assumes the third argument is a block that accepts a GRPCReachabilityFlags object, and passes the
+// received ones to it.
static void PassFlagsToContextInfoBlock(SCNetworkReachabilityRef target,
SCNetworkReachabilityFlags flags,
void *info) {
-#pragma unused(target)
- // This can be called many times with the same info. The info is retained by
- // SCNetworkReachability while this function is being executed.
- void (^handler)(GRPCReachabilityFlags *) =
- (__bridge void (^)(GRPCReachabilityFlags *))info;
+ #pragma unused (target)
+ // This can be called many times with the same info. The info is retained by SCNetworkReachability
+ // while this function is being executed.
+ void (^handler)(GRPCReachabilityFlags *) = (__bridge void (^)(GRPCReachabilityFlags *))info;
handler([[GRPCReachabilityFlags alloc] initWithFlags:flags]);
}
@@ -127,8 +123,7 @@ static void PassFlagsToContextInfoBlock(SCNetworkReachabilityRef target,
GRPCReachabilityFlags *_previousReachabilityFlags;
}
-- (nullable instancetype)initWithReachability:
- (nullable SCNetworkReachabilityRef)reachability {
+- (nullable instancetype)initWithReachability:(nullable SCNetworkReachabilityRef)reachability {
if (!reachability) {
return nil;
}
@@ -149,23 +144,23 @@ static void PassFlagsToContextInfoBlock(SCNetworkReachabilityRef target,
SCNetworkReachabilityRef reachability =
SCNetworkReachabilityCreateWithName(NULL, hostName);
- GRPCConnectivityMonitor *returnValue =
- [[self alloc] initWithReachability:reachability];
+ GRPCConnectivityMonitor *returnValue = [[self alloc] initWithReachability:reachability];
if (reachability) {
CFRelease(reachability);
}
return returnValue;
}
-- (void)handleLossWithHandler:(void (^)())handler
- wifiStatusChangeHandler:(nonnull void (^)())wifiStatusChangeHandler {
+- (void)handleLossWithHandler:(nullable void (^)())lossHandler
+ wifiStatusChangeHandler:(nullable void (^)())wifiStatusChangeHandler {
__weak typeof(self) weakSelf = self;
[self startListeningWithHandler:^(GRPCReachabilityFlags *flags) {
typeof(self) strongSelf = weakSelf;
if (strongSelf) {
- if (!flags.reachable) {
- handler();
- } else if (strongSelf->_previousReachabilityFlags &&
+ if (lossHandler && !flags.reachable) {
+ lossHandler();
+ } else if (wifiStatusChangeHandler &&
+ strongSelf->_previousReachabilityFlags &&
(flags.isWWAN ^
strongSelf->_previousReachabilityFlags.isWWAN)) {
wifiStatusChangeHandler();
@@ -176,19 +171,17 @@ static void PassFlagsToContextInfoBlock(SCNetworkReachabilityRef target,
}
- (void)startListeningWithHandler:(void (^)(GRPCReachabilityFlags *))handler {
- // Copy to ensure the handler block is in the heap (and so can't be
- // deallocated when this method returns).
+ // Copy to ensure the handler block is in the heap (and so can't be deallocated when this method
+ // returns).
void (^copiedHandler)(GRPCReachabilityFlags *) = [handler copy];
SCNetworkReachabilityContext context = {
- .version = 0,
- .info = (__bridge void *)copiedHandler,
- .retain = CFRetain,
- .release = CFRelease,
+ .version = 0,
+ .info = (__bridge void *)copiedHandler,
+ .retain = CFRetain,
+ .release = CFRelease,
};
- // The following will retain context.info, and release it when the callback is
- // set to NULL.
- SCNetworkReachabilitySetCallback(_reachabilityRef,
- PassFlagsToContextInfoBlock, &context);
+ // The following will retain context.info, and release it when the callback is set to NULL.
+ SCNetworkReachabilitySetCallback(_reachabilityRef, PassFlagsToContextInfoBlock, &context);
SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _queue);
}