project_files/HedgewarsMobile/Classes/HWUtils.m
branchqmlfrontend
changeset 11408 b894922d58cc
parent 11223 c51ecb9bcf05
child 11559 73e6a3d2f768
equal deleted inserted replaced
11076:fcbdee9cdd74 11408:b894922d58cc
   101 #pragma mark -
   101 #pragma mark -
   102 #pragma mark Helper Functions without cache
   102 #pragma mark Helper Functions without cache
   103 +(NSInteger) randomPort {
   103 +(NSInteger) randomPort {
   104     // set a new feed only at initialization time and forbid connecting to the server port
   104     // set a new feed only at initialization time and forbid connecting to the server port
   105     if (activePorts == nil) {
   105     if (activePorts == nil) {
   106         srandom(time(NULL));
       
   107         activePorts = [[NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]] retain];
   106         activePorts = [[NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]] retain];
   108     }
   107     }
   109 
   108 
   110     // pick a random number from the free ports list
   109     // pick a random number from the free ports list
   111     NSInteger res = 0;
   110     NSInteger res = 0;
   112     do {
   111     do {
   113         res = (random() % 64511) + 1024;
   112         res = (arc4random_uniform(64511)) + 1024;
   114     } while ([activePorts containsObject:[NSNumber numberWithInt:res]]);
   113     } while ([activePorts containsObject:[NSNumber numberWithInteger:res]]);
   115 
   114 
   116     // add this number to the forbdding list
   115     // add this number to the forbdding list
   117     [activePorts addObject:[NSNumber numberWithInt:res]];
   116     [activePorts addObject:[NSNumber numberWithInteger:res]];
   118     return res;
   117     return res;
   119 }
   118 }
   120 
   119 
   121 +(void) freePort:(NSInteger) port {
   120 +(void) freePort:(NSInteger) port {
   122     [activePorts removeObject:[NSNumber numberWithInt:port]];
   121     [activePorts removeObject:[NSNumber numberWithInteger:port]];
   123 }
   122 }
   124 
   123 
   125 +(BOOL) isNetworkReachable {
   124 +(BOOL) isNetworkReachable {
   126     // Create zero addy
   125     // Create zero addy
   127     struct sockaddr_in zeroAddress;
   126     struct sockaddr_in zeroAddress;
   154     [testConnection release];
   153     [testConnection release];
   155 
   154 
   156     return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
   155     return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
   157 }
   156 }
   158 
   157 
       
   158 + (NSString *)languageID
       
   159 {
       
   160     NSString *language = [[NSLocale preferredLanguages] firstObject];
       
   161     return [[language componentsSeparatedByString:@"-"] firstObject];
       
   162 }
       
   163 
   159 /*
   164 /*
   160 +(UIView *)mainSDLViewInstance {
   165 +(UIView *)mainSDLViewInstance {
   161     SDL_Window *window = HW_getSDLWindow();
   166     SDL_Window *window = HW_getSDLWindow();
   162     if (window == NULL) {
   167     if (window == NULL) {
   163         SDL_SetError("Window does not exist");
   168         SDL_SetError("Window does not exist");