Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 6527cee9874b089006d1b430ddd8482aa4bef193 1 parent a82f360
authored October 17, 2012
38  GCNetworkReachability.h
... ...
@@ -1,32 +1,30 @@
1 1
 //
2  
-//  GCNetworkReachability.h
3  
-//  GCNetworkReachability
4  
-//
5 2
 //  Created by Glenn Chiu on 26/09/2012.
6 3
 //  Copyright (c) 2012 Glenn Chiu. All rights reserved.
7 4
 //
  5
+//  Version 1.0
8 6
 
9  
-// This code is distributed under the terms and conditions of the MIT license.
  7
+//  This code is distributed under the terms and conditions of the MIT license.
10 8
 
11  
-// Copyright (c) 2012 Glenn Chiu
  9
+//  Copyright (c) 2012 Glenn Chiu
12 10
 //
13  
-// Permission is hereby granted, free of charge, to any person obtaining a copy
14  
-// of this software and associated documentation files (the "Software"), to deal
15  
-// in the Software without restriction, including without limitation the rights
16  
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  
-// copies of the Software, and to permit persons to whom the Software is
18  
-// furnished to do so, subject to the following conditions:
  11
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
  12
+//  of this software and associated documentation files (the "Software"), to deal
  13
+//  in the Software without restriction, including without limitation the rights
  14
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15
+//  copies of the Software, and to permit persons to whom the Software is
  16
+//  furnished to do so, subject to the following conditions:
19 17
 //
20  
-// The above copyright notice and this permission notice shall be included in
21  
-// all copies or substantial portions of the Software.
  18
+//  The above copyright notice and this permission notice shall be included in
  19
+//  all copies or substantial portions of the Software.
22 20
 //
23  
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29  
-// THE SOFTWARE.
  21
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27
+//  THE SOFTWARE.
30 28
 
31 29
 #import <Foundation/Foundation.h>
32 30
 #import <SystemConfiguration/SystemConfiguration.h>
83  GCNetworkReachability.m
... ...
@@ -1,32 +1,30 @@
1 1
 //
2  
-//  GCNetworkReachability.m
3  
-//  GCNetworkReachability
4  
-//
5 2
 //  Created by Glenn Chiu on 26/09/2012.
6 3
 //  Copyright (c) 2012 Glenn Chiu. All rights reserved.
7 4
 //
  5
+//  Version 1.0
8 6
 
9  
-// This code is distributed under the terms and conditions of the MIT license.
  7
+//  This code is distributed under the terms and conditions of the MIT license.
10 8
 
11  
-// Copyright (c) 2012 Glenn Chiu
  9
+//  Copyright (c) 2012 Glenn Chiu
12 10
 //
13  
-// Permission is hereby granted, free of charge, to any person obtaining a copy
14  
-// of this software and associated documentation files (the "Software"), to deal
15  
-// in the Software without restriction, including without limitation the rights
16  
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  
-// copies of the Software, and to permit persons to whom the Software is
18  
-// furnished to do so, subject to the following conditions:
  11
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
  12
+//  of this software and associated documentation files (the "Software"), to deal
  13
+//  in the Software without restriction, including without limitation the rights
  14
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15
+//  copies of the Software, and to permit persons to whom the Software is
  16
+//  furnished to do so, subject to the following conditions:
19 17
 //
20  
-// The above copyright notice and this permission notice shall be included in
21  
-// all copies or substantial portions of the Software.
  18
+//  The above copyright notice and this permission notice shall be included in
  19
+//  all copies or substantial portions of the Software.
22 20
 //
23  
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29  
-// THE SOFTWARE.
  21
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27
+//  THE SOFTWARE.
30 28
 
31 29
 #import "GCNetworkReachability.h"
32 30
 #import <arpa/inet.h>
@@ -44,21 +42,23 @@
44 42
 #if TARGET_OS_IPHONE
45 43
 #   if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
46 44
 #       define GC_DISPATCH_RELEASE(v) do {} while(0)
47  
-#       define GC_DISPATCH_RETAIN(v) do {} while(0)
48 45
 #   else
49 46
 #       define GC_DISPATCH_RELEASE(v) dispatch_release(v)
50  
-#       define GC_DISPATCH_RETAIN(v) dispatch_retain(v)
51 47
 #   endif
52 48
 #else
53 49
 #   if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
54 50
 #       define GC_DISPATCH_RELEASE(v) do {} while(0)
55  
-#       define GC_DISPATCH_RETAIN(v) do {} while(0)
56 51
 #   else
57 52
 #       define GC_DISPATCH_RELEASE(v) dispatch_release(v)
58  
-#       define GC_DISPATCH_RETAIN(v) dispatch_retain(v)
59 53
 #   endif
60 54
 #endif
61 55
 
  56
+struct GCNetworkReachabilityFlagContext
  57
+{
  58
+    SCNetworkReachabilityRef target;
  59
+    GCNetworkReachabilityStatus *status;
  60
+};
  61
+
62 62
 static GCNetworkReachabilityStatus GCReachabilityStatusForFlags(SCNetworkReachabilityFlags flags);
63 63
 static const void * GCNetworkReachabilityRetainCallback(const void *info);
64 64
 static void GCNetworkReachabilityReleaseCallback(const void *info);
@@ -68,6 +68,7 @@
68 68
 static void GCNetworkReachabilityPrintFlags(SCNetworkReachabilityFlags flags);
69 69
 static void GCNetworkReachabilitySetSocketAddress(struct sockaddr_in *addr);
70 70
 static void GCNetworkReachabilitySetIPv6SocketAddress(struct sockaddr_in6 *addr);
  71
+static void GCNetworkReachabilityGetCurrentStatus(void *context);
71 72
 
72 73
 static BOOL _localWiFi;
73 74
 
@@ -296,11 +297,33 @@ static GCNetworkReachabilityStatus GCReachabilityStatusForFlags(SCNetworkReachab
296 297
     return status;
297 298
 }
298 299
 
299  
-- (GCNetworkReachabilityStatus)currentReachabilityStatus
  300
+static void GCNetworkReachabilityGetCurrentStatus(void *context)
300 301
 {
  302
+    struct GCNetworkReachabilityFlagContext *ctx = context;
301 303
     SCNetworkReachabilityFlags flags = (SCNetworkReachabilityFlags)0;
302  
-    SCNetworkReachabilityGetFlags(self->_networkReachability, &flags);
303  
-    return GCReachabilityStatusForFlags(flags);
  304
+    GCNetworkReachabilityStatus currentStatus = GCNetworkReachabilityStatusNotReachable;
  305
+    
  306
+    if (!SCNetworkReachabilityGetFlags(ctx->target, &flags))
  307
+    {
  308
+        GCNRLog(@"SCNetworkReachabilityGetFlags() failed with error code: %s", SCErrorString(SCError()));
  309
+        ctx->status = &currentStatus;
  310
+        return;
  311
+    }
  312
+    
  313
+    currentStatus = GCReachabilityStatusForFlags(flags);
  314
+    ctx->status = &currentStatus;
  315
+}
  316
+
  317
+- (GCNetworkReachabilityStatus)currentReachabilityStatus
  318
+{
  319
+    GCNetworkReachabilityStatus status = (GCNetworkReachabilityStatus)0;
  320
+    struct GCNetworkReachabilityFlagContext context = {
  321
+        
  322
+        self->_networkReachability,
  323
+        &status
  324
+    };
  325
+    dispatch_sync_f(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), &context, GCNetworkReachabilityGetCurrentStatus);
  326
+    return *context.status;
304 327
 }
305 328
 
306 329
 - (BOOL)isReachable
@@ -329,7 +352,6 @@ - (BOOL)isReachableViaWWAN
329 352
 static void GCNetworkReachabilityReleaseCallback(const void *info)
330 353
 {
331 354
     CFRelease(info);
332  
-    info = NULL;
333 355
 }
334 356
 
335 357
 static void GCNetworkReachabilityPostNotification(void *info, GCNetworkReachabilityStatus status)
@@ -362,12 +384,11 @@ - (void)startMonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabi
362 384
     
363 385
     self->_handler_blk = [block copy];
364 386
     
365  
-    __weak GCNetworkReachability *w_self = self;
  387
+    __weak typeof(self) w_self = self;
366 388
     
367 389
     void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
368 390
         
369  
-        GCNetworkReachability *s_self = w_self;
370  
-        
  391
+        __strong typeof(w_self) s_self = w_self;
371 392
         if (s_self) s_self->_handler_blk(status);
372 393
     };
373 394
     

0 notes on commit 6527cee

Please sign in to comment.
Something went wrong with that request. Please try again.