GDTCORReachability.m 3.7 KB

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