project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.m
changeset 6320 238a6dc0e7ad
parent 6246 6b2d19ed521a
child 6337 84e7d1a5e3df
equal deleted inserted replaced
6319:b57a37a94ad3 6320:238a6dc0e7ad
    18  * File created on 10/01/2010.
    18  * File created on 10/01/2010.
    19  */
    19  */
    20 
    20 
    21 
    21 
    22 #import "ServerProtocolNetwork.h"
    22 #import "ServerProtocolNetwork.h"
    23 #import "SDL_net.h"
       
    24 #import "hwconsts.h"
    23 #import "hwconsts.h"
    25 
    24 
    26 #define BUFFER_SIZE 256
    25 #define BUFFER_SIZE 256
    27 
    26 
    28 static TCPsocket sd;
       
    29 static ServerProtocolNetwork *serverConnection;
    27 static ServerProtocolNetwork *serverConnection;
    30 
    28 
    31 @implementation ServerProtocolNetwork
    29 @implementation ServerProtocolNetwork
    32 @synthesize serverPort, serverAddress;
    30 @synthesize serverPort, serverAddress, ssd;
    33 
    31 
    34 -(id) init {
    32 #pragma mark -
       
    33 #pragma mark init and class methods
       
    34 -(id) init:(NSInteger) onPort withAddress:(NSString *)address {
    35     if (self = [super init]) {
    35     if (self = [super init]) {
    36         self.serverPort = NETGAME_DEFAULT_PORT;
    36         self.serverPort = onPort;
    37         self.serverAddress = @"netserver.hedgewars.org";
    37         self.serverAddress = address;
    38     }
    38     }
    39     serverConnection = self;
    39     serverConnection = self;
    40     return self;
    40     return self;
    41 }
    41 }
    42 
    42 
       
    43 -(id) init {
       
    44     return [self init:NETGAME_DEFAULT_PORT withAddress:@"netserver.hedgewars.org"];
       
    45 }
       
    46 
       
    47 -(id) initOnPort:(NSInteger) port {
       
    48     return [self init:port withAddress:@"netserver.hedgewars.org"];
       
    49 }
       
    50 
       
    51 -(id) initToAddress:(NSString *)address {
       
    52     return [self init:NETGAME_DEFAULT_PORT withAddress:address];
       
    53 }
       
    54 
    43 -(void) dealloc {
    55 -(void) dealloc {
    44     releaseAndNil(serverAddress);
    56     releaseAndNil(serverAddress);
       
    57     serverConnection = nil;
    45     [super dealloc];
    58     [super dealloc];
    46 }
    59 }
    47 
    60 
       
    61 +(ServerProtocolNetwork *)openServerConnection {
       
    62     ServerProtocolNetwork *connection = [[ServerProtocolNetwork alloc] init];
       
    63     [NSThread detachNewThreadSelector:@selector(serverProtocol)
       
    64                              toTarget:connection
       
    65                            withObject:nil];
       
    66     [connection retain];    // retain count here is +2
       
    67     return connection;
       
    68 }
       
    69 
       
    70 #pragma mark -
       
    71 #pragma mark Communication layer
    48 -(int) sendToServer:(NSString *)command {
    72 -(int) sendToServer:(NSString *)command {
    49     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    73     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
    50     int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
    74     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message length]);
    51     [message release];
    75     [message release];
    52     return result;
    76     return result;
    53 }
    77 }
    54 
    78 
    55 -(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
    79 -(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
    56     NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
    80     NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
    57     int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
    81     int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message length]);
    58     [message release];
    82     [message release];
    59     return result;
    83     return result;
    60 }
    84 }
    61 
    85 
    62 -(void) serverProtocol {
    86 -(void) serverProtocol {
    78         DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError());
   102         DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError());
    79         clientQuit = YES;
   103         clientQuit = YES;
    80     }
   104     }
    81 
   105 
    82     // Open a connection with the IP provided (listen on the host's port)
   106     // Open a connection with the IP provided (listen on the host's port)
    83     if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
   107     if (!(self.ssd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
    84         DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort);
   108         DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort);
    85         clientQuit = YES;
   109         clientQuit = YES;
    86     }
   110     }
    87 
   111 
    88     DLog(@"Found server on port %d", self.serverPort);
   112     DLog(@"Found server on port %d", self.serverPort);
    90         int index = 0;
   114         int index = 0;
    91         BOOL exitBufferLoop = NO;
   115         BOOL exitBufferLoop = NO;
    92         memset(buffer, '\0', dim);
   116         memset(buffer, '\0', dim);
    93 
   117 
    94         while (exitBufferLoop != YES) {
   118         while (exitBufferLoop != YES) {
    95             msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
   119             msgSize = SDLNet_TCP_Recv(self.ssd, &buffer[index], 2);
    96 
   120 
    97             // exit in case of error
   121             // exit in case of error
    98             if (msgSize <= 0) {
   122             if (msgSize <= 0) {
    99                 DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
   123                 DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
   100                 clientQuit = YES;
   124                 clientQuit = YES;
   179         }
   203         }
   180     }
   204     }
   181     DLog(@"Server closed connection, ending thread");
   205     DLog(@"Server closed connection, ending thread");
   182 
   206 
   183     free(buffer);
   207     free(buffer);
   184     SDLNet_TCP_Close(sd);
   208     SDLNet_TCP_Close(self.ssd);
   185     SDLNet_Quit();
   209     SDLNet_Quit();
   186 
   210 
   187     [pool release];
   211     [pool release];
   188 }
   212 }
   189 
   213