project_files/HedgewarsMobile/Classes/HWUtils.m
branchios-revival
changeset 11206 2e80c9861818
parent 11148 064a53861759
child 11219 c51ecb9bcf05
equal deleted inserted replaced
11205:9fd7b8d3cf4d 11206:2e80c9861818
   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 numberWithInteger: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 numberWithInteger:res]];
   116     [activePorts addObject:[NSNumber numberWithInteger:res]];
   118     return res;
   117     return res;