暂无描述

GDTCORReachability.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #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(SCNetworkReachabilityRef reachability,
  27. SCNetworkReachabilityFlags flags,
  28. void *info);
  29. @implementation GDTCORReachability {
  30. /** The reachability object. */
  31. SCNetworkReachabilityRef _reachabilityRef;
  32. /** The queue on which callbacks and all work will occur. */
  33. dispatch_queue_t _reachabilityQueue;
  34. /** Flags specified by reachability callbacks. */
  35. SCNetworkConnectionFlags _callbackFlags;
  36. }
  37. + (void)load {
  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. + (SCNetworkReachabilityFlags)currentFlags {
  49. __block SCNetworkReachabilityFlags currentFlags;
  50. dispatch_sync([GDTCORReachability sharedInstance] -> _reachabilityQueue, ^{
  51. GDTCORReachability *reachability = [GDTCORReachability sharedInstance];
  52. currentFlags = reachability->_flags ? reachability->_flags : reachability->_callbackFlags;
  53. });
  54. return currentFlags;
  55. }
  56. - (instancetype)init {
  57. self = [super init];
  58. if (self) {
  59. struct sockaddr_in zeroAddress;
  60. bzero(&zeroAddress, sizeof(zeroAddress));
  61. zeroAddress.sin_len = sizeof(zeroAddress);
  62. zeroAddress.sin_family = AF_INET;
  63. _reachabilityQueue =
  64. dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
  65. _reachabilityRef = SCNetworkReachabilityCreateWithAddress(
  66. kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
  67. Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
  68. if (!success) {
  69. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
  70. }
  71. success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
  72. if (!success) {
  73. GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
  74. @"The reachability callback wasn't set.");
  75. }
  76. // Get the initial set of flags.
  77. dispatch_async(_reachabilityQueue, ^{
  78. Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
  79. if (!valid) {
  80. self->_flags = 0;
  81. }
  82. });
  83. }
  84. return self;
  85. }
  86. - (void)setCallbackFlags:(SCNetworkReachabilityFlags)flags {
  87. if (_callbackFlags != flags) {
  88. self->_callbackFlags = flags;
  89. }
  90. }
  91. @end
  92. static void GDTCORReachabilityCallback(SCNetworkReachabilityRef reachability,
  93. SCNetworkReachabilityFlags flags,
  94. void *info) {
  95. [[GDTCORReachability sharedInstance] setCallbackFlags:flags];
  96. }