Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
17
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORReachability.h"
18
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORReachability_Private.h"
19
 
20
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
21
 
22
#import <netinet/in.h>
23
 
24
/** Sets the _callbackFlag ivar whenever the network changes.
25
 *
26
 * @param reachability The reachability object calling back.
27
 * @param flags The new flag values.
28
 * @param info Any data that might be passed in by the callback.
29
 */
30
static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
31
                                       GDTCORNetworkReachabilityFlags flags,
32
                                       void *info);
33
 
34
@implementation GDTCORReachability {
35
  /** The reachability object. */
36
  GDTCORNetworkReachabilityRef _reachabilityRef;
37
 
38
  /** The queue on which callbacks and all work will occur. */
39
  dispatch_queue_t _reachabilityQueue;
40
 
41
  /** Flags specified by reachability callbacks. */
42
  GDTCORNetworkReachabilityFlags _callbackFlags;
43
}
44
 
45
+ (void)initialize {
46
  [self sharedInstance];
47
}
48
 
49
+ (instancetype)sharedInstance {
50
  static GDTCORReachability *sharedInstance;
51
  static dispatch_once_t onceToken;
52
  dispatch_once(&onceToken, ^{
53
    sharedInstance = [[GDTCORReachability alloc] init];
54
  });
55
  return sharedInstance;
56
}
57
 
58
+ (GDTCORNetworkReachabilityFlags)currentFlags {
59
  __block GDTCORNetworkReachabilityFlags currentFlags;
60
#if !TARGET_OS_WATCH
61
  dispatch_sync([GDTCORReachability sharedInstance] -> _reachabilityQueue, ^{
62
    GDTCORReachability *reachability = [GDTCORReachability sharedInstance];
63
    currentFlags =
64
        reachability->_callbackFlags ? reachability->_callbackFlags : reachability->_flags;
65
    GDTCORLogDebug(@"Initial reachability flags determined: %d", currentFlags);
66
  });
67
#else
68
  currentFlags = kGDTCORNetworkReachabilityFlagsReachable;
69
#endif
70
  return currentFlags;
71
}
72
 
73
- (instancetype)init {
74
  self = [super init];
75
#if !TARGET_OS_WATCH
76
  if (self) {
77
    struct sockaddr_in zeroAddress;
78
    bzero(&zeroAddress, sizeof(zeroAddress));
79
    zeroAddress.sin_len = sizeof(zeroAddress);
80
    zeroAddress.sin_family = AF_INET;
81
 
82
    _reachabilityQueue =
83
        dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
84
    _reachabilityRef = SCNetworkReachabilityCreateWithAddress(
85
        kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
86
    Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
87
    if (!success) {
88
      GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
89
    }
90
    success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
91
    if (!success) {
92
      GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
93
                       @"The reachability callback wasn't set.");
94
    }
95
 
96
    // Get the initial set of flags.
97
    dispatch_async(_reachabilityQueue, ^{
98
      Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
99
      if (!valid) {
100
        GDTCORLogDebug(@"%@", @"Determining reachability failed.");
101
        self->_flags = 0;
102
      }
103
    });
104
  }
105
#endif
106
  return self;
107
}
108
 
109
- (void)setCallbackFlags:(GDTCORNetworkReachabilityFlags)flags {
110
  if (_callbackFlags != flags) {
111
    self->_callbackFlags = flags;
112
  }
113
}
114
 
115
@end
116
 
117
#pragma clang diagnostic push
118
#pragma clang diagnostic ignored "-Wunused-function"
119
static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
120
                                       GDTCORNetworkReachabilityFlags flags,
121
                                       void *info) {
122
#pragma clang diagnostic pop
123
  GDTCORLogDebug(@"Reachability changed, new flags: %d", flags);
124
  [[GDTCORReachability sharedInstance] setCallbackFlags:flags];
125
}