58 [array release]; |
60 [array release]; |
59 } |
61 } |
60 return cachedColors; |
62 return cachedColors; |
61 } |
63 } |
62 |
64 |
|
65 +(NSInteger) randomPort { |
|
66 srandom(time(NULL)); |
|
67 NSInteger res = (random() % 64511) + 1024; |
|
68 return (res == NETGAME_DEFAULT_PORT) ? [HWUtils randomPort] : res; |
|
69 } |
|
70 |
|
71 +(BOOL) isNetworkReachable { |
|
72 // Create zero addy |
|
73 struct sockaddr_in zeroAddress; |
|
74 bzero(&zeroAddress, sizeof(zeroAddress)); |
|
75 zeroAddress.sin_len = sizeof(zeroAddress); |
|
76 zeroAddress.sin_family = AF_INET; |
|
77 |
|
78 // Recover reachability flags |
|
79 SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
|
80 SCNetworkReachabilityFlags flags; |
|
81 |
|
82 BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
|
83 CFRelease(defaultRouteReachability); |
|
84 |
|
85 if (!didRetrieveFlags) { |
|
86 NSLog(@"Error. Could not recover network reachability flags"); |
|
87 return NO; |
|
88 } |
|
89 |
|
90 BOOL isReachable = flags & kSCNetworkFlagsReachable; |
|
91 BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
|
92 BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; |
|
93 |
|
94 NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; |
|
95 NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL |
|
96 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData |
|
97 timeoutInterval:20.0]; |
|
98 NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; |
|
99 BOOL testResult = testConnection ? YES : NO; |
|
100 [testConnection release]; |
|
101 |
|
102 return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO; |
|
103 } |
|
104 |
63 +(void) releaseCache { |
105 +(void) releaseCache { |
64 releaseAndNil(cachedModel); |
106 releaseAndNil(cachedModel); |
65 releaseAndNil(cachedColors); |
107 releaseAndNil(cachedColors); |
66 } |
108 } |
67 |
109 |