Permalink
Browse files

Improve efficiency. Refactor API.

  • Loading branch information...
1 parent 24f5e96 commit 446386a98b818fc645e0259e1aa7e63fc6e7a737 @GlennChiu committed Oct 3, 2012
Showing with 47 additions and 36 deletions.
  1. +9 −9 GCNetworkReachability.h
  2. +38 −27 GCNetworkReachability.m
@@ -34,7 +34,7 @@
typedef enum : unsigned char
{
- GCNetworkReachabilityStatusNotReachable = 1 << 0,
+ GCNetworkReachabilityStatusNotReachable = (1 << 0),
GCNetworkReachabilityStatusWWAN = (1 << 1) | GCNetworkReachabilityStatusNotReachable,
GCNetworkReachabilityStatusWiFi = (1 << 2) | GCNetworkReachabilityStatusNotReachable
} GCNetworkReachabilityStatus;
@orj
orj Oct 30, 2012

Wondering why you bitwise or the GCNetworkReachabilityStatusWWAN and GCNetworkReachabilityStatusWiFi values with GCNetworkReachabilityStatusNotReachable. By using bitwise flags like this in this enum you are suggesting that code like this will work:

if (status & GCNetworkReachabilityStatusNotReachable) 
{
    // not reachable
}
else
{
   // reachable
}

But by including the GCNetworkReachabilityStatusNotReachable bit in all values of this enum you can't do this. So you have to use it like a "normal" enum. Eg:

switch (status)
{
    case GCNetworkReachabilityStatusNotReachable:
        // ...
    case GCNetworkReachabilityStatusWWAN:
       // etc...
}
@orj
orj Oct 30, 2012

Also wondering why you don't just expose the underlying SCNetworkReachabilityFlags as they provide much more fine grained data on reachability.

@GlennChiu
GlennChiu Oct 30, 2012 Owner

About the bitwise flags: It made it a little bit cleaner to convert the SCNetworkReachabilityFlags to GCNetworkReachabilityStatus values.

But you're right, it might be confusing for the user, so I will change the multi-bit values to single bit values.

About exposing the SCNetworkReachabilityFlags: You're also right about that, however I want to keep the header file clean. It should only expose the most used API's and I'm not sure if exposing SCNetworkReachabilityFlags will be helpful for a lot of developers. If you think otherwise, I might consider to add the API.

@orj
orj via email Oct 31, 2012
@GlennChiu
GlennChiu Nov 6, 2012 Owner

I have updated the code with your feedback.

@@ -61,13 +61,20 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
+ (GCNetworkReachability *)reachabilityForLocalWiFi;
-- (id)initWithHostAddress:(const struct sockaddr_in *)hostAddress;
+- (id)initWithHostAddress:(const struct sockaddr *)hostAddress;
- (id)initWithHostName:(NSString *)hostName;
- (id)initWithReachability:(SCNetworkReachabilityRef)reachability;
+- (void)startMonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabilityStatus status))block;
+
+- (void)startMonitoringNetworkReachabilityWithNotification;
+
+- (void)stopMonitoringNetworkReachability;
+
+
- (GCNetworkReachabilityStatus)currentReachabilityStatus;
@@ -79,11 +86,4 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
- (BOOL)isReachableViaWWAN;
#endif
-
-- (void)startMonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabilityStatus status))block;
-
-- (void)startMonitoringNetworkReachabilityWithNotification;
-
-- (void)stopMonitoringNetworkReachability;
-
@end
@@ -66,8 +66,8 @@
static void GCNetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info);
static void GCNetworkReachabilityPostNotification(void *info, GCNetworkReachabilityStatus status);
static void GCNetworkReachabilityPrintFlags(SCNetworkReachabilityFlags flags);
-static struct sockaddr_in GCNetworkReachabilityCreateSocketAddress(void);
-static struct sockaddr_in6 GCNetworkReachabilityCreateIPv6SocketAddress(void);
+static void GCNetworkReachabilitySetSocketAddress(struct sockaddr_in *addr);
+static void GCNetworkReachabilitySetIPv6SocketAddress(struct sockaddr_in6 *addr);
static BOOL _localWiFi;
@@ -108,37 +108,42 @@ static void GCNetworkReachabilityPrintFlags(SCNetworkReachabilityFlags flags)
+ (GCNetworkReachability *)reachabilityWithHostName:(NSString *)hostName
{
- return hostName ? [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [hostName UTF8String])] : nil;
+ assert(hostName);
+
+ return [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [hostName UTF8String])];
}
+ (GCNetworkReachability *)reachabilityWithHostAddress:(const struct sockaddr *)hostAddress
{
- return hostAddress ? [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress)] : nil;
+ assert(hostAddress);
+
+ return [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress)];
}
-static struct sockaddr_in GCNetworkReachabilityCreateSocketAddress(void)
+static void GCNetworkReachabilitySetSocketAddress(struct sockaddr_in *addr)
{
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_len = sizeof(addr);
- addr.sin_family = AF_INET;
- return addr;
+ memset(addr, 0, sizeof(struct sockaddr_in));
+ addr->sin_len = sizeof(struct sockaddr_in);
+ addr->sin_family = AF_INET;
}
+ (GCNetworkReachability *)reachabilityWithInternetAddress:(in_addr_t)internetAddress
{
- struct sockaddr_in addr = GCNetworkReachabilityCreateSocketAddress();
+ assert(internetAddress >= (in_addr_t)0);
+
+ struct sockaddr_in addr;
+ GCNetworkReachabilitySetSocketAddress(&addr);
addr.sin_addr.s_addr = htonl(internetAddress);
return [self reachabilityWithHostAddress:(const struct sockaddr *)&addr];
}
+ (GCNetworkReachability *)reachabilityWithInternetAddressString:(NSString *)internetAddress
{
- if (!internetAddress) return nil;
+ assert(internetAddress);
- struct sockaddr_in addr = GCNetworkReachabilityCreateSocketAddress();
+ struct sockaddr_in addr;
+ GCNetworkReachabilitySetSocketAddress(&addr);
inet_pton(AF_INET, [internetAddress UTF8String], &addr.sin_addr);
-
return [self reachabilityWithHostAddress:(const struct sockaddr *)&addr];
}
@@ -156,38 +161,38 @@ + (GCNetworkReachability *)reachabilityForLocalWiFi
return [self reachabilityWithInternetAddress:localAddr];
}
-static struct sockaddr_in6 GCNetworkReachabilityCreateIPv6SocketAddress(void)
+static void GCNetworkReachabilitySetIPv6SocketAddress(struct sockaddr_in6 *addr)
{
- struct sockaddr_in6 addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin6_len = sizeof(addr);
- addr.sin6_family = AF_INET6;
- return addr;
+ memset(addr, 0, sizeof(struct sockaddr_in6));
+ addr->sin6_len = sizeof(struct sockaddr_in6);
+ addr->sin6_family = AF_INET6;
}
+ (GCNetworkReachability *)reachabilityWithIPv6Address:(const struct in6_addr)internetAddress
{
+ assert(&internetAddress);
+
char strAddr[INET6_ADDRSTRLEN];
- struct sockaddr_in6 addr = GCNetworkReachabilityCreateIPv6SocketAddress();
+ struct sockaddr_in6 addr;
+ GCNetworkReachabilitySetIPv6SocketAddress(&addr);
addr.sin6_addr = internetAddress;
inet_ntop(AF_INET6, &addr.sin6_addr, strAddr, INET6_ADDRSTRLEN);
inet_pton(AF_INET6, strAddr, &addr.sin6_addr);
-
return [self reachabilityWithHostAddress:(const struct sockaddr *)&addr];
}
+ (GCNetworkReachability *)reachabilityWithIPv6AddressString:(NSString *)internetAddress
{
- if (!internetAddress) return nil;
+ assert(internetAddress);
- struct sockaddr_in6 addr = GCNetworkReachabilityCreateIPv6SocketAddress();
+ struct sockaddr_in6 addr;
+ GCNetworkReachabilitySetIPv6SocketAddress(&addr);
inet_pton(AF_INET6, [internetAddress UTF8String], &addr.sin6_addr);
-
return [self reachabilityWithHostAddress:(const struct sockaddr *)&addr];
}
-- (id)initWithHostAddress:(const struct sockaddr_in *)hostAddress
+- (id)initWithHostAddress:(const struct sockaddr *)hostAddress
{
assert(hostAddress);
@@ -357,9 +362,13 @@ - (void)startMonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabi
self->_handler_blk = [block copy];
+ __weak GCNetworkReachability *w_self = self;
+
void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
- self->_handler_blk(status);
+ GCNetworkReachability *s_self = w_self;
+
+ if (s_self) s_self->_handler_blk(status);
};
SCNetworkReachabilityContext context = {
@@ -404,6 +413,8 @@ - (void)stopMonitoringNetworkReachability
{
if (self->_networkReachability) SCNetworkReachabilitySetCallback(self->_networkReachability, NULL, NULL);
+ if (self->_handler_blk) self->_handler_blk = nil;
+
[self releaseReachabilityQueue];
}

0 comments on commit 446386a

Please sign in to comment.