project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11240 57891137ffef
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    27 @implementation ServerProtocolNetwork
    27 @implementation ServerProtocolNetwork
    28 @synthesize serverPort, serverAddress, ssd;
    28 @synthesize serverPort, serverAddress, ssd;
    29 
    29 
    30 #pragma mark -
    30 #pragma mark -
    31 #pragma mark init and class methods
    31 #pragma mark init and class methods
    32 -(id) init:(NSInteger) onPort withAddress:(NSString *)address {
    32 - (id)init:(NSInteger)onPort withAddress:(NSString *)address {
    33     if ((self = [super init])) {
    33     if ((self = [super init])) {
    34         self.serverPort = onPort;
    34         self.serverPort = onPort;
    35         self.serverAddress = address;
    35         self.serverAddress = address;
    36     }
    36     }
    37     serverConnection = self;
    37     serverConnection = self;
    38     return self;
    38     return self;
    39 }
    39 }
    40 
    40 
    41 -(id) init {
    41 - (id)init {
    42     return [self init:NETGAME_DEFAULT_PORT withAddress:@"netserver.hedgewars.org"];
    42     return [self init:NETGAME_DEFAULT_PORT withAddress:@"netserver.hedgewars.org"];
    43 }
    43 }
    44 
    44 
    45 -(id) initOnPort:(NSInteger) port {
    45 - (id)initOnPort:(NSInteger)port {
    46     return [self init:port withAddress:@"netserver.hedgewars.org"];
    46     return [self init:port withAddress:@"netserver.hedgewars.org"];
    47 }
    47 }
    48 
    48 
    49 -(id) initToAddress:(NSString *)address {
    49 - (id)initToAddress:(NSString *)address {
    50     return [self init:NETGAME_DEFAULT_PORT withAddress:address];
    50     return [self init:NETGAME_DEFAULT_PORT withAddress:address];
    51 }
    51 }
    52 
    52 
    53 -(void) dealloc {
    53 - (void)dealloc {
    54     releaseAndNil(serverAddress);
       
    55     serverConnection = nil;
    54     serverConnection = nil;
    56     [super dealloc];
       
    57 }
    55 }
    58 
    56 
    59 +(id) openServerConnection {
    57 +(id) openServerConnection {
    60     id connection = [[self alloc] init];
    58     id connection = [[self alloc] init];
    61     [NSThread detachNewThreadSelector:@selector(serverProtocol)
    59     [NSThread detachNewThreadSelector:@selector(serverProtocol)
    62                              toTarget:connection
    60                              toTarget:connection
    63                            withObject:nil];
    61                            withObject:nil];
    64     [connection retain];    // retain count here is +2
    62         // retain count here is +2
    65     return connection;
    63     return connection;
    66 }
    64 }
    67 
    65 
    68 #pragma mark -
    66 #pragma mark -
    69 #pragma mark Communication layer
    67 #pragma mark Communication layer
    70 -(int) sendToServer:(NSString *)command {
    68 -(int) sendToServer:(NSString *)command {
    71     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    69     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    72     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
    70     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
    73     [message release];
       
    74     return result;
    71     return result;
    75 }
    72 }
    76 
    73 
    77 -(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
    74 -(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
    78     NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
    75     NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
    79     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
    76     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
    80     [message release];
       
    81     return result;
    77     return result;
    82 }
    78 }
    83 
    79 
    84 -(void) serverProtocol {
    80 - (void)serverProtocol {
    85     @autoreleasepool {
    81     @autoreleasepool {
    86     
    82     
    87     IPaddress ip;
    83     IPaddress ip;
    88     BOOL clientQuit = NO;
    84     BOOL clientQuit = NO;
    89     char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    85     char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
   141             }
   137             }
   142         }
   138         }
   143 
   139 
   144         NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
   140         NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
   145         NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
   141         NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
   146         [bufferedMessage release];
       
   147         NSString *command = [listOfCommands objectAtIndex:0];
   142         NSString *command = [listOfCommands objectAtIndex:0];
   148         DLog(@"size = %d, %@", index-2, listOfCommands);
   143         DLog(@"size = %d, %@", index-2, listOfCommands);
   149         if ([command isEqualToString:@"PING"]) {
   144         if ([command isEqualToString:@"PING"]) {
   150             if ([listOfCommands count] > 1)
   145             if ([listOfCommands count] > 1)
   151                 [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
   146                 [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];