project_files/HedgewarsMobile/Classes/ServerSetup.m
author koda
Wed, 04 May 2011 01:19:39 +0200
changeset 5201 7b9aa7aac336
parent 4976 088d40d8aba2
child 5206 db775bddf771
permissions -rw-r--r--
* moved colours and default port from .cpp.in to .h * slight tweaks to existing colours (softer shades)
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
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4603
diff changeset
     3
 * Copyright (c) 2009-2011 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
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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
 * File created on 10/01/2010.
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    19
 */
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    20
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    21
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    22
#import "ServerSetup.h"
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    23
#import "PascalImports.h"
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    24
#import "CommodityFunctions.h"
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    25
#import <SystemConfiguration/SCNetworkReachability.h>
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    26
#import <netinet/in.h>
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4976
diff changeset
    27
#import "hwconsts.h"
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    28
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    29
#define BUFFER_SIZE 256
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    30
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    31
@implementation ServerSetup
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    32
@synthesize systemSettings;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    33
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    34
-(id) init {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    35
    if (self = [super init]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    36
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    37
        self.systemSettings = dict;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    38
        [dict release];
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
    return self;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    41
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    42
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    43
-(void) dealloc {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    44
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    45
    [super dealloc];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    46
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    47
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    48
// reusing appirater method
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    49
-(BOOL) isNetworkReachable {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    50
    // Create zero addy
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    51
    struct sockaddr_in zeroAddress;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    52
    bzero(&zeroAddress, sizeof(zeroAddress));
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    53
    zeroAddress.sin_len = sizeof(zeroAddress);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    54
    zeroAddress.sin_family = AF_INET;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    55
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    56
    // Recover reachability flags
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    57
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    58
    SCNetworkReachabilityFlags flags;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    59
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    60
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    61
    CFRelease(defaultRouteReachability);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    62
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    63
    if (!didRetrieveFlags) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    64
        NSLog(@"Error. Could not recover network reachability flags");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    65
        return NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    66
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    67
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    68
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    69
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    70
    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    71
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    72
    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    73
    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    74
                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    75
                                             timeoutInterval:20.0];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    76
    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    77
    
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    78
    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    79
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    80
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    81
-(int) sendToServer:(NSString *)command {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    82
    NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    83
    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    84
    [message release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    85
    return result;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    86
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    87
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    88
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    89
    NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    90
    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    91
    [message release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    92
    return result;
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
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    95
-(void) serverProtocol {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    96
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    97
    IPaddress ip;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    98
    BOOL clientQuit = NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
    99
    char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   100
    int dim = BUFFER_SIZE;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   101
    uint8_t msgSize;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   102
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   103
    if (SDLNet_Init() < 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   104
        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   105
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   106
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   107
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   108
    // Resolving the host using NULL make network interface to listen
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4976
diff changeset
   109
    if (SDLNet_ResolveHost(&ip, "netserver.hedgewars.org", NETGAME_DEFAULT_PORT) < 0 && !clientQuit) {
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   110
        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   111
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   112
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   113
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   114
    // Open a connection with the IP provided (listen on the host's port)
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   115
    if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4976
diff changeset
   116
        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), NETGAME_DEFAULT_PORT);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   117
        clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   118
    }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   119
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4976
diff changeset
   120
    DLog(@"Found server on port %d", NETGAME_DEFAULT_PORT);
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   121
    while (!clientQuit) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   122
        int index = 0;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   123
        BOOL exitBufferLoop = NO;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   124
        memset(buffer, '\0', dim);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   125
        
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   126
        while (exitBufferLoop != YES) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   127
            msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   128
            
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   129
            // exit in case of error
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   130
            if (msgSize <= 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   131
                DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   132
                clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   133
                break;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   134
            }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   135
            
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   136
            // update index position and check for End-Of-Message
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   137
            index += msgSize;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   138
            if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   139
                exitBufferLoop = YES;
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
            // if message is too big allocate new space
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   143
            if (index >= dim) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   144
                dim += BUFFER_SIZE;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   145
                buffer = (char *)realloc(buffer, dim);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   146
                if (buffer == NULL) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   147
                    clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   148
                    break;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   149
                }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   150
            }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   151
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   152
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   153
        NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   154
        NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   155
        [bufferedMessage release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   156
        NSString *command = [listOfCommands objectAtIndex:0];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   157
        DLog(@"size = %d, %@", index-2, listOfCommands);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   158
        if ([command isEqualToString:@"PING"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   159
            if ([listOfCommands count] > 1)
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   160
                [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   161
            else
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   162
                [self sendToServer:@"PONG"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   163
            DLog(@"PONG");
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:@"NICK"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   166
            //what is this for?
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:@"PROTO"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   169
            //what is this for?
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:@"ROOM"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   172
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   173
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   174
        else if ([command isEqualToString:@"LOBBY:LEFT"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   175
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   176
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   177
        else if ([command isEqualToString:@"LOBBY:JOINED"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   178
            //TODO: stub
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   179
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   180
        else if ([command isEqualToString:@"ASKPASSWORD"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   181
            NSString *pwd = [self.systemSettings objectForKey:@"password"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   182
            [self sendToServer:@"PASSWORD" withArgument:pwd];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   183
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   184
        else if ([command isEqualToString:@"CONNECTED"]) {
4603
d362ab6c7f53 damn. it.
koda
parents: 4547
diff changeset
   185
            int netProto;
4547
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   186
            char *versionStr;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   187
            HW_versionInfo(&netProto, &versionStr);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   188
            NSString *nick = [self.systemSettings objectForKey:@"username"];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   189
            [self sendToServer:@"NICK" withArgument:nick];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   190
            [self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
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:@"SERVER_MESSAGE"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   193
            DLog(@"%@", [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:@"WARNING"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   196
            if ([listOfCommands count] > 1)
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   197
                DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   198
            else
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   199
                DLog(@"Server warning - unknown");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   200
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   201
        else if ([command isEqualToString:@"ERROR"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   202
            DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
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
        else if ([command isEqualToString:@"BYE"]) {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   205
            //TODO: handle "Reconnected too fast"
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   206
            DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   207
            clientQuit = YES;
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   208
        }
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   209
        else {
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   210
            DLog(@"Unknown/Unsupported message received: %@", command);
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
    DLog(@"Server closed connection, ending thread");
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   214
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   215
    free(buffer);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   216
    SDLNet_TCP_Close(sd);
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   217
    SDLNet_Quit();
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   218
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   219
    [pool release];
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   220
}
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   221
b70004a576a3 move server connection stuff in its own class
koda
parents:
diff changeset
   222
@end