GDTCORReachability.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/Private/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 = reachability->_flags ? reachability->_flags : reachability->_callbackFlags;
  54. GDTCORLogDebug("Initial reachability flags determined: %d", currentFlags);
  55. });
  56. return currentFlags;
  57. }
  58. - (instancetype)init {
  59. self = [super init];
  60. if (self) {
  61. struct sockaddr_in zeroAddress;
  62. bzero(&zeroAddress, sizeof(zeroAddress));
  63. zeroAddress.sin_len = sizeof(zeroAddress);
  64. zeroAddress.sin_family = AF_INET;
  65. _reachabilityQueue =
  66. dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
  67. _reachabilityRef = SCNetworkReachabilityCreateWithAddress(
  68. kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
  69. Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
  70. if (!success) {
  71. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
  72. }
  73. success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
  74. if (!success) {
  75. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
  76. @"The reachability callback wasn't set.");
  77. }
  78. // Get the initial set of flags.
  79. dispatch_async(_reachabilityQueue, ^{
  80. Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
  81. if (!valid) {
  82. GDTCORLogDebug("%@", @"Determining reachability failed.");
  83. self->_flags = 0;
  84. }
  85. });
  86. }
  87. return self;
  88. }
  89. - (void)setCallbackFlags:(SCNetworkReachabilityFlags)flags {
  90. if (_callbackFlags != flags) {
  91. self->_callbackFlags = flags;
  92. }
  93. }
  94. @end
  95. static void GDTCORReachabilityCallback(SCNetworkReachabilityRef reachability,
  96. SCNetworkReachabilityFlags flags,
  97. void *info) {
  98. GDTCORLogDebug("Reachability changed, new flags: %d", flags);
  99. [[GDTCORReachability sharedInstance] setCallbackFlags:flags];
  100. }
  101. #endif