project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.m
author Wuzzy <almikes@aol.com>
Wed, 13 Apr 2016 01:26:46 +0200
changeset 11721 8c1a30d4cbc8
parent 11240 57891137ffef
child 12872 00215a7ec5f5
permissions -rw-r--r--
Construction Mode: Fix giving round energy boost at first round
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     1
/*
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
6700
e04da46ee43c the most important commit of the year
koda
parents: 6337
diff changeset
     3
 * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     4
 *
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     8
 *
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    12
 * GNU General Public License for more details.
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    13
 *
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 8835
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    17
 */
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    18
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    19
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    20
#import "ServerProtocolNetwork.h"
8835
01bcf9ea68c1 ios compiles again (using an old version of sdl)
koda
parents: 6908
diff changeset
    21
#import "hwconsts.h"
6832
fae8fd118da9 dates are no more!
koda
parents: 6822
diff changeset
    22
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    23
#define BUFFER_SIZE 256
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    24
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    25
static ServerProtocolNetwork *serverConnection;
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5483
diff changeset
    26
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    27
@implementation ServerProtocolNetwork
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    28
@synthesize serverPort, serverAddress, ssd;
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5483
diff changeset
    29
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    30
#pragma mark -
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    31
#pragma mark init and class methods
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    32
-(id) init:(NSInteger) onPort withAddress:(NSString *)address {
6908
896ed2afcfb8 ios: turn on more warning messages and start correcting them
koda
parents: 6832
diff changeset
    33
    if ((self = [super init])) {
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    34
        self.serverPort = onPort;
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    35
        self.serverAddress = address;
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    36
    }
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    37
    serverConnection = self;
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    38
    return self;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    39
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    40
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    41
-(id) init {
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    42
    return [self init:NETGAME_DEFAULT_PORT withAddress:@"netserver.hedgewars.org"];
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    43
}
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    44
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    45
-(id) initOnPort:(NSInteger) port {
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    46
    return [self init:port withAddress:@"netserver.hedgewars.org"];
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    47
}
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    48
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    49
-(id) initToAddress:(NSString *)address {
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    50
    return [self init:NETGAME_DEFAULT_PORT withAddress:address];
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    51
}
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    52
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    53
-(void) dealloc {
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    54
    releaseAndNil(serverAddress);
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    55
    serverConnection = nil;
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    56
    [super dealloc];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    57
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    58
6337
84e7d1a5e3df in class methods you can call [self alloc], as per objc specifications
koda
parents: 6320
diff changeset
    59
+(id) openServerConnection {
84e7d1a5e3df in class methods you can call [self alloc], as per objc specifications
koda
parents: 6320
diff changeset
    60
    id connection = [[self alloc] init];
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    61
    [NSThread detachNewThreadSelector:@selector(serverProtocol)
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    62
                             toTarget:connection
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    63
                           withObject:nil];
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    64
    [connection retain];    // retain count here is +2
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    65
    return connection;
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    66
}
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    67
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    68
#pragma mark -
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
    69
#pragma mark Communication layer
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    70
-(int) sendToServer:(NSString *)command {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    71
    NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
11240
57891137ffef - Fix for sending to engine non-english characters
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    72
    int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    73
    [message release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    74
    return result;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    75
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    76
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    77
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    78
    NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
11240
57891137ffef - Fix for sending to engine non-english characters
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    79
    int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    80
    [message release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    81
    return result;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    82
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    83
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    84
-(void) serverProtocol {
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 10108
diff changeset
    85
    @autoreleasepool {
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 10108
diff changeset
    86
    
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    87
    IPaddress ip;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    88
    BOOL clientQuit = NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    89
    char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    90
    int dim = BUFFER_SIZE;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    91
    uint8_t msgSize;
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
    92
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    93
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    94
    if (SDLNet_Init() < 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    95
        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    96
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    97
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    98
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    99
    // Resolving the host using NULL make network interface to listen
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   100
    if (SDLNet_ResolveHost(&ip, [self.serverAddress UTF8String] , self.serverPort) < 0 && !clientQuit) {
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   101
        DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError());
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   102
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   103
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   104
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   105
    // Open a connection with the IP provided (listen on the host's port)
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
   106
    if (!(self.ssd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   107
        DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   108
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   109
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   110
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   111
    DLog(@"Found server on port %d", self.serverPort);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   112
    while (!clientQuit) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   113
        int index = 0;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   114
        BOOL exitBufferLoop = NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   115
        memset(buffer, '\0', dim);
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   116
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   117
        while (exitBufferLoop != YES) {
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
   118
            msgSize = SDLNet_TCP_Recv(self.ssd, &buffer[index], 2);
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   119
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   120
            // exit in case of error
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   121
            if (msgSize <= 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   122
                DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   123
                clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   124
                break;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   125
            }
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   126
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   127
            // update index position and check for End-Of-Message
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   128
            index += msgSize;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   129
            if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   130
                exitBufferLoop = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   131
            }
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   132
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   133
            // if message is too big allocate new space
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   134
            if (index >= dim) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   135
                dim += BUFFER_SIZE;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   136
                buffer = (char *)realloc(buffer, dim);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   137
                if (buffer == NULL) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   138
                    clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   139
                    break;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   140
                }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   141
            }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   142
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   143
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   144
        NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   145
        NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   146
        [bufferedMessage release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   147
        NSString *command = [listOfCommands objectAtIndex:0];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   148
        DLog(@"size = %d, %@", index-2, listOfCommands);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   149
        if ([command isEqualToString:@"PING"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   150
            if ([listOfCommands count] > 1)
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   151
                [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   152
            else
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   153
                [self sendToServer:@"PONG"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   154
            DLog(@"PONG");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   155
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   156
        else if ([command isEqualToString:@"NICK"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   157
            //what is this for?
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   158
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   159
        else if ([command isEqualToString:@"PROTO"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   160
            //what is this for?
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   161
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   162
        else if ([command isEqualToString:@"ROOM"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   163
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   164
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   165
        else if ([command isEqualToString:@"LOBBY:LEFT"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   166
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   167
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   168
        else if ([command isEqualToString:@"LOBBY:JOINED"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   169
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   170
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   171
        else if ([command isEqualToString:@"ASKPASSWORD"]) {
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   172
            NSString *pwd = [defaults objectForKey:@"password"];
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   173
            [self sendToServer:@"PASSWORD" withArgument:pwd];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   174
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   175
        else if ([command isEqualToString:@"CONNECTED"]) {
4603
d362ab6c7f53 damn. it.
koda
parents: 4547
diff changeset
   176
            int netProto;
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   177
            char *versionStr;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   178
            HW_versionInfo(&netProto, &versionStr);
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   179
            NSString *nick = [defaults objectForKey:@"username"];
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   180
            [self sendToServer:@"NICK" withArgument:nick];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   181
            [self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   182
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   183
        else if ([command isEqualToString:@"SERVER_MESSAGE"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   184
            DLog(@"%@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   185
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   186
        else if ([command isEqualToString:@"WARNING"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   187
            if ([listOfCommands count] > 1)
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   188
                DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   189
            else
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   190
                DLog(@"Server warning - unknown");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   191
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   192
        else if ([command isEqualToString:@"ERROR"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   193
            DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   194
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   195
        else if ([command isEqualToString:@"BYE"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   196
            //TODO: handle "Reconnected too fast"
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   197
            DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   198
            clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   199
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   200
        else {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   201
            DLog(@"Unknown/Unsupported message received: %@", command);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   202
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   203
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   204
    DLog(@"Server closed connection, ending thread");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   205
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   206
    free(buffer);
6320
238a6dc0e7ad minor updates to ServerProtocolNetwork class
koda
parents: 6246
diff changeset
   207
    SDLNet_TCP_Close(self.ssd);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   208
    SDLNet_Quit();
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   209
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 10108
diff changeset
   210
    }
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   211
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   212
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   213
@end