GDTCORReachability.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORLibrary/Public/GDTCORReachability.h"
  17. #import "GDTCORLibrary/Private/GDTCORReachability_Private.h"
  18. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  19. #import <netinet/in.h>
  20. /** Sets the _callbackFlag ivar whenever the network changes.
  21. *
  22. * @param reachability The reachability object calling back.
  23. * @param flags The new flag values.
  24. * @param info Any data that might be passed in by the callback.
  25. */
  26. static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
  27. GDTCORNetworkReachabilityFlags flags,
  28. void *info);
  29. @implementation GDTCORReachability {
  30. /** The reachability object. */
  31. GDTCORNetworkReachabilityRef _reachabilityRef;
  32. /** The queue on which callbacks and all work will occur. */
  33. dispatch_queue_t _reachabilityQueue;
  34. /** Flags specified by reachability callbacks. */
  35. GDTCORNetworkReachabilityFlags _callbackFlags;
  36. }
  37. + (void)initialize {
  38. [self sharedInstance];
  39. }
  40. + (instancetype)sharedInstance {
  41. static GDTCORReachability *sharedInstance;
  42. static dispatch_once_t onceToken;
  43. dispatch_once(&onceToken, ^{
  44. sharedInstance = [[GDTCORReachability alloc] init];
  45. });
  46. return sharedInstance;
  47. }
  48. + (GDTCORNetworkReachabilityFlags)currentFlags {
  49. __block GDTCORNetworkReachabilityFlags currentFlags;
  50. #if !TARGET_OS_WATCH
  51. dispatch_sync([GDTCORReachability sharedInstance] -> _reachabilityQueue, ^{
  52. GDTCORReachability *reachability = [GDTCORReachability sharedInstance];
  53. currentFlags =
  54. reachability->_callbackFlags ? reachability->_callbackFlags : reachability->_flags;
  55. GDTCORLogDebug(@"Initial reachability flags determined: %d", currentFlags);
  56. });
  57. #else
  58. currentFlags = kGDTCORNetworkReachabilityFlagsReachable;
  59. #endif
  60. return currentFlags;
  61. }
  62. - (instancetype)init {
  63. self = [super init];
  64. #if !TARGET_OS_WATCH
  65. if (self) {
  66. struct sockaddr_in zeroAddress;
  67. bzero(&zeroAddress, sizeof(zeroAddress));
  68. zeroAddress.sin_len = sizeof(zeroAddress);
  69. zeroAddress.sin_family = AF_INET;
  70. _reachabilityQueue =
  71. dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
  72. _reachabilityRef = SCNetworkReachabilityCreateWithAddress(
  73. kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
  74. Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
  75. if (!success) {
  76. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
  77. }
  78. success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
  79. if (!success) {
  80. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
  81. @"The reachability callback wasn't set.");
  82. }
  83. // Get the initial set of flags.
  84. dispatch_async(_reachabilityQueue, ^{
  85. Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
  86. if (!valid) {
  87. GDTCORLogDebug(@"%@", @"Determining reachability failed.");
  88. self->_flags = 0;
  89. }
  90. });
  91. }
  92. #endif
  93. return self;
  94. }
  95. - (void)setCallbackFlags:(GDTCORNetworkReachabilityFlags)flags {
  96. if (_callbackFlags != flags) {
  97. self->_callbackFlags = flags;
  98. }
  99. }
  100. @end
  101. #pragma clang diagnostic push
  102. #pragma clang diagnostic ignored "-Wunused-function"
  103. static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
  104. GDTCORNetworkReachabilityFlags flags,
  105. void *info) {
  106. #pragma clang diagnostic pop
  107. GDTCORLogDebug(@"Reachability changed, new flags: %d", flags);
  108. [[GDTCORReachability sharedInstance] setCallbackFlags:flags];
  109. }