GDTCORReachability.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #if !TARGET_OS_WATCH
  19. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  20. #import <netinet/in.h>
  21. /** Sets the _callbackFlag ivar whenever the network changes.
  22. *
  23. * @param reachability The reachability object calling back.
  24. * @param flags The new flag values.
  25. * @param info Any data that might be passed in by the callback.
  26. */
  27. static void GDTCORReachabilityCallback(SCNetworkReachabilityRef reachability,
  28. SCNetworkReachabilityFlags flags,
  29. void *info);
  30. @implementation GDTCORReachability {
  31. /** The reachability object. */
  32. SCNetworkReachabilityRef _reachabilityRef;
  33. /** The queue on which callbacks and all work will occur. */
  34. dispatch_queue_t _reachabilityQueue;
  35. /** Flags specified by reachability callbacks. */
  36. SCNetworkConnectionFlags _callbackFlags;
  37. }
  38. + (void)load {
  39. [self sharedInstance];
  40. }
  41. + (instancetype)sharedInstance {
  42. static GDTCORReachability *sharedInstance;
  43. static dispatch_once_t onceToken;
  44. dispatch_once(&onceToken, ^{
  45. sharedInstance = [[GDTCORReachability alloc] init];
  46. });
  47. return sharedInstance;
  48. }
  49. + (SCNetworkReachabilityFlags)currentFlags {
  50. __block SCNetworkReachabilityFlags currentFlags;
  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. return currentFlags;
  58. }
  59. - (instancetype)init {
  60. self = [super init];
  61. if (self) {
  62. struct sockaddr_in zeroAddress;
  63. bzero(&zeroAddress, sizeof(zeroAddress));
  64. zeroAddress.sin_len = sizeof(zeroAddress);
  65. zeroAddress.sin_family = AF_INET;
  66. _reachabilityQueue =
  67. dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
  68. _reachabilityRef = SCNetworkReachabilityCreateWithAddress(
  69. kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
  70. Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
  71. if (!success) {
  72. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
  73. }
  74. success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
  75. if (!success) {
  76. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
  77. @"The reachability callback wasn't set.");
  78. }
  79. // Get the initial set of flags.
  80. dispatch_async(_reachabilityQueue, ^{
  81. Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
  82. if (!valid) {
  83. GDTCORLogDebug("%@", @"Determining reachability failed.");
  84. self->_flags = 0;
  85. }
  86. });
  87. }
  88. return self;
  89. }
  90. - (void)setCallbackFlags:(SCNetworkReachabilityFlags)flags {
  91. if (_callbackFlags != flags) {
  92. self->_callbackFlags = flags;
  93. }
  94. }
  95. @end
  96. static void GDTCORReachabilityCallback(SCNetworkReachabilityRef reachability,
  97. SCNetworkReachabilityFlags flags,
  98. void *info) {
  99. GDTCORLogDebug("Reachability changed, new flags: %d", flags);
  100. [[GDTCORReachability sharedInstance] setCallbackFlags:flags];
  101. }
  102. #endif