Permalink
Browse files

Update status methods to async calls. Minor refactor. Add version number

  • Loading branch information...
1 parent a82f360 commit 6527cee9874b089006d1b430ddd8482aa4bef193 @GlennChiu committed Oct 17, 2012
Showing with 70 additions and 51 deletions.
  1. +18 −20 GCNetworkReachability.h
  2. +52 −31 GCNetworkReachability.m
View
@@ -1,32 +1,30 @@
//
-// GCNetworkReachability.h
-// GCNetworkReachability
-//
// Created by Glenn Chiu on 26/09/2012.
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
+// Version 1.0
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
View
@@ -1,32 +1,30 @@
//
-// GCNetworkReachability.m
-// GCNetworkReachability
-//
// Created by Glenn Chiu on 26/09/2012.
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
+// Version 1.0
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCNetworkReachability.h"
#import <arpa/inet.h>
@@ -44,21 +42,23 @@
#if TARGET_OS_IPHONE
# if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
# define GC_DISPATCH_RELEASE(v) do {} while(0)
-# define GC_DISPATCH_RETAIN(v) do {} while(0)
# else
# define GC_DISPATCH_RELEASE(v) dispatch_release(v)
-# define GC_DISPATCH_RETAIN(v) dispatch_retain(v)
# endif
#else
# if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
# define GC_DISPATCH_RELEASE(v) do {} while(0)
-# define GC_DISPATCH_RETAIN(v) do {} while(0)
# else
# define GC_DISPATCH_RELEASE(v) dispatch_release(v)
-# define GC_DISPATCH_RETAIN(v) dispatch_retain(v)
# endif
#endif
+struct GCNetworkReachabilityFlagContext
+{
+ SCNetworkReachabilityRef target;
+ GCNetworkReachabilityStatus *status;
+};
+
static GCNetworkReachabilityStatus GCReachabilityStatusForFlags(SCNetworkReachabilityFlags flags);
static const void * GCNetworkReachabilityRetainCallback(const void *info);
static void GCNetworkReachabilityReleaseCallback(const void *info);
@@ -68,6 +68,7 @@
static void GCNetworkReachabilityPrintFlags(SCNetworkReachabilityFlags flags);
static void GCNetworkReachabilitySetSocketAddress(struct sockaddr_in *addr);
static void GCNetworkReachabilitySetIPv6SocketAddress(struct sockaddr_in6 *addr);
+static void GCNetworkReachabilityGetCurrentStatus(void *context);
static BOOL _localWiFi;
@@ -296,11 +297,33 @@ static GCNetworkReachabilityStatus GCReachabilityStatusForFlags(SCNetworkReachab
return status;
}
-- (GCNetworkReachabilityStatus)currentReachabilityStatus
+static void GCNetworkReachabilityGetCurrentStatus(void *context)
{
+ struct GCNetworkReachabilityFlagContext *ctx = context;
SCNetworkReachabilityFlags flags = (SCNetworkReachabilityFlags)0;
- SCNetworkReachabilityGetFlags(self->_networkReachability, &flags);
- return GCReachabilityStatusForFlags(flags);
+ GCNetworkReachabilityStatus currentStatus = GCNetworkReachabilityStatusNotReachable;
+
+ if (!SCNetworkReachabilityGetFlags(ctx->target, &flags))
+ {
+ GCNRLog(@"SCNetworkReachabilityGetFlags() failed with error code: %s", SCErrorString(SCError()));
+ ctx->status = &currentStatus;
+ return;
+ }
+
+ currentStatus = GCReachabilityStatusForFlags(flags);
+ ctx->status = &currentStatus;
+}
+
+- (GCNetworkReachabilityStatus)currentReachabilityStatus
+{
+ GCNetworkReachabilityStatus status = (GCNetworkReachabilityStatus)0;
+ struct GCNetworkReachabilityFlagContext context = {
+
+ self->_networkReachability,
+ &status
+ };
+ dispatch_sync_f(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), &context, GCNetworkReachabilityGetCurrentStatus);
+ return *context.status;
}
- (BOOL)isReachable
@@ -329,7 +352,6 @@ - (BOOL)isReachableViaWWAN
static void GCNetworkReachabilityReleaseCallback(const void *info)
{
CFRelease(info);
- info = NULL;
}
static void GCNetworkReachabilityPostNotification(void *info, GCNetworkReachabilityStatus status)
@@ -362,12 +384,11 @@ - (void)startMonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabi
self->_handler_blk = [block copy];
- __weak GCNetworkReachability *w_self = self;
+ __weak typeof(self) w_self = self;
void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
- GCNetworkReachability *s_self = w_self;
-
+ __strong typeof(w_self) s_self = w_self;
if (s_self) s_self->_handler_blk(status);
};

0 comments on commit 6527cee

Please sign in to comment.