22 #import "HWUtils.h" |
22 #import "HWUtils.h" |
23 #import <sys/types.h> |
23 #import <sys/types.h> |
24 #import <sys/sysctl.h> |
24 #import <sys/sysctl.h> |
25 #import <netinet/in.h> |
25 #import <netinet/in.h> |
26 #import <SystemConfiguration/SCNetworkReachability.h> |
26 #import <SystemConfiguration/SCNetworkReachability.h> |
27 #import "hwconsts.h" |
|
28 #import "EngineProtocolNetwork.h" |
|
29 #import "SDL_uikitwindow.h" |
27 #import "SDL_uikitwindow.h" |
30 |
28 |
31 static NSString *cachedModel = nil; |
29 static NSString *cachedModel = nil; |
32 static NSArray *cachedColors = nil; |
30 static NSArray *cachedColors = nil; |
|
31 static NSMutableArray *activePorts = nil; |
33 |
32 |
34 static TGameType gameType = gtNone; |
33 static TGameType gameType = gtNone; |
35 static TGameStatus gameStatus = gsNone; |
34 static TGameStatus gameStatus = gsNone; |
36 |
35 |
37 @implementation HWUtils |
36 @implementation HWUtils |
96 } |
95 } |
97 |
96 |
98 +(void) releaseCache { |
97 +(void) releaseCache { |
99 [cachedModel release], cachedModel = nil; |
98 [cachedModel release], cachedModel = nil; |
100 [cachedColors release], cachedColors = nil; |
99 [cachedColors release], cachedColors = nil; |
|
100 // don't release activePorts here |
101 } |
101 } |
102 |
102 |
103 #pragma mark - |
103 #pragma mark - |
104 #pragma mark Helper Functions without cache |
104 #pragma mark Helper Functions without cache |
105 +(NSInteger) randomPort { |
105 +(NSInteger) randomPort { |
106 srandom(time(NULL)); |
106 // set a new feed only at initialization time and forbid connecting to the server port |
107 NSInteger res = (random() % 64511) + 1024; |
107 if (activePorts == nil) { |
108 // recall self until you get a free port |
108 srandom(time(NULL)); |
109 if (res == NETGAME_DEFAULT_PORT || res == [EngineProtocolNetwork activeEnginePort]) |
109 activePorts = [[NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]] retain]; |
110 return [self randomPort]; |
110 } |
111 else |
111 |
112 return res; |
112 // pick a random number from the free ports list |
|
113 NSInteger res = 0; |
|
114 do { |
|
115 res = (random() % 64511) + 1024; |
|
116 } while ([activePorts containsObject:[NSNumber numberWithInt:res]]); |
|
117 |
|
118 // add this number to the forbdding list |
|
119 [activePorts addObject:[NSNumber numberWithInt:res]]; |
|
120 return res; |
|
121 } |
|
122 |
|
123 +(void) freePort:(NSInteger) port { |
|
124 [activePorts removeObject:[NSNumber numberWithInt:port]]; |
113 } |
125 } |
114 |
126 |
115 +(BOOL) isNetworkReachable { |
127 +(BOOL) isNetworkReachable { |
116 // Create zero addy |
128 // Create zero addy |
117 struct sockaddr_in zeroAddress; |
129 struct sockaddr_in zeroAddress; |