18 * File created on 10/01/2010. |
18 * File created on 10/01/2010. |
19 */ |
19 */ |
20 |
20 |
21 |
21 |
22 #import "ServerSetup.h" |
22 #import "ServerSetup.h" |
23 #import "PascalImports.h" |
23 #import <netinet/in.h> |
24 #import "CommodityFunctions.h" |
24 #import <SystemConfiguration/SCNetworkReachability.h> |
|
25 |
25 #import "hwconsts.h" |
26 #import "hwconsts.h" |
26 |
27 |
27 #define BUFFER_SIZE 256 |
28 #define BUFFER_SIZE 256 |
28 |
29 |
29 @implementation ServerSetup |
30 @implementation ServerSetup |
30 @synthesize systemSettings; |
31 @synthesize systemSettings; |
|
32 |
|
33 |
|
34 +(NSInteger) randomPort { |
|
35 srandom(time(NULL)); |
|
36 NSInteger res = (random() % 64511) + 1024; |
|
37 return (res == NETGAME_DEFAULT_PORT) ? [ServerSetup randomPort] : res; |
|
38 } |
|
39 |
|
40 +(BOOL) isNetworkReachable { |
|
41 // Create zero addy |
|
42 struct sockaddr_in zeroAddress; |
|
43 bzero(&zeroAddress, sizeof(zeroAddress)); |
|
44 zeroAddress.sin_len = sizeof(zeroAddress); |
|
45 zeroAddress.sin_family = AF_INET; |
|
46 |
|
47 // Recover reachability flags |
|
48 SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
|
49 SCNetworkReachabilityFlags flags; |
|
50 |
|
51 BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
|
52 CFRelease(defaultRouteReachability); |
|
53 |
|
54 if (!didRetrieveFlags) { |
|
55 NSLog(@"Error. Could not recover network reachability flags"); |
|
56 return NO; |
|
57 } |
|
58 |
|
59 BOOL isReachable = flags & kSCNetworkFlagsReachable; |
|
60 BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
|
61 BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; |
|
62 |
|
63 NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; |
|
64 NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL |
|
65 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData |
|
66 timeoutInterval:20.0]; |
|
67 NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; |
|
68 BOOL testResult = testConnection ? YES : NO; |
|
69 [testConnection release]; |
|
70 |
|
71 return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO; |
|
72 } |
31 |
73 |
32 -(id) init { |
74 -(id) init { |
33 if (self = [super init]) { |
75 if (self = [super init]) { |
34 self.systemSettings = nil; //nsuserdefault |
76 self.systemSettings = nil; //nsuserdefault |
35 } |
77 } |