project_files/HedgewarsMobile/Classes/ServerSetup.m
changeset 5483 fc755bb8096d
parent 5206 db775bddf771
child 6078 8c0cc07731e5
equal deleted inserted replaced
5482:c047e70c53f4 5483:fc755bb8096d
    20 
    20 
    21 
    21 
    22 #import "ServerSetup.h"
    22 #import "ServerSetup.h"
    23 #import "PascalImports.h"
    23 #import "PascalImports.h"
    24 #import "CommodityFunctions.h"
    24 #import "CommodityFunctions.h"
    25 #import <SystemConfiguration/SCNetworkReachability.h>
       
    26 #import <netinet/in.h>
       
    27 #import "hwconsts.h"
    25 #import "hwconsts.h"
    28 
    26 
    29 #define BUFFER_SIZE 256
    27 #define BUFFER_SIZE 256
    30 
    28 
    31 @implementation ServerSetup
    29 @implementation ServerSetup
    39 }
    37 }
    40 
    38 
    41 -(void) dealloc {
    39 -(void) dealloc {
    42 
    40 
    43     [super dealloc];
    41     [super dealloc];
    44 }
       
    45 
       
    46 // reusing appirater method
       
    47 -(BOOL) isNetworkReachable {
       
    48     // Create zero addy
       
    49     struct sockaddr_in zeroAddress;
       
    50     bzero(&zeroAddress, sizeof(zeroAddress));
       
    51     zeroAddress.sin_len = sizeof(zeroAddress);
       
    52     zeroAddress.sin_family = AF_INET;
       
    53     
       
    54     // Recover reachability flags
       
    55     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
       
    56     SCNetworkReachabilityFlags flags;
       
    57     
       
    58     BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
       
    59     CFRelease(defaultRouteReachability);
       
    60     
       
    61     if (!didRetrieveFlags) {
       
    62         NSLog(@"Error. Could not recover network reachability flags");
       
    63         return NO;
       
    64     }
       
    65     
       
    66     BOOL isReachable = flags & kSCNetworkFlagsReachable;
       
    67     BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
       
    68     BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
       
    69     
       
    70     NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
       
    71     NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
       
    72                                                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
       
    73                                              timeoutInterval:20.0];
       
    74     NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
       
    75     
       
    76     return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
       
    77 }
    42 }
    78 
    43 
    79 -(int) sendToServer:(NSString *)command {
    44 -(int) sendToServer:(NSString *)command {
    80     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    45     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    81     int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
    46     int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);