just moving ios server stuff around
authorkoda
Mon, 31 Oct 2011 01:44:32 +0100
changeset 6246 6b2d19ed521a
parent 6245 6df227b5e4c9
child 6247 6dfad55fd71c
just moving ios server stuff around
project_files/HedgewarsMobile/Classes/Appirater.m
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
project_files/HedgewarsMobile/Classes/HWUtils.h
project_files/HedgewarsMobile/Classes/HWUtils.m
project_files/HedgewarsMobile/Classes/MainMenuViewController.m
project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.h
project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.m
project_files/HedgewarsMobile/Classes/ServerSetup.h
project_files/HedgewarsMobile/Classes/ServerSetup.m
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
--- a/project_files/HedgewarsMobile/Classes/Appirater.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/Appirater.m	Mon Oct 31 01:44:32 2011 +0100
@@ -37,7 +37,6 @@
 #import "Appirater.h"
 #import <SystemConfiguration/SCNetworkReachability.h>
 #import <netinet/in.h>
-#import "ServerSetup.h"
 
 NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
 NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
@@ -106,7 +105,7 @@
              launchCount > LAUNCHES_UNTIL_PROMPT &&
              !declinedToRate &&
              !ratedApp) {
-            if ([ServerSetup isNetworkReachable]) {	// check if they can reach the app store
+            if ([HWUtils isNetworkReachable]) {	// check if they can reach the app store
                 willShowPrompt = YES;
                 [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
             }
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Mon Oct 31 01:44:32 2011 +0100
@@ -60,6 +60,10 @@
 
 #pragma mark -
 #pragma mark Spawner functions
+-(void) spawnThread:(NSString *)onSaveFile {
+    [self spawnThread:onSaveFile withOptions:nil];
+}
+
 -(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary {
     self.stream = (onSaveFile) ? [[NSOutputStream alloc] initToFileAtPath:onSaveFile append:YES] : nil;
     [self.stream open];
@@ -69,10 +73,6 @@
                            withObject:dictionary];
 }
 
--(void) spawnThread:(NSString *)onSaveFile {
-    [self spawnThread:onSaveFile withOptions:nil];
-}
-
 #pragma mark -
 #pragma mark Provider functions
 // unpacks team data from the selected team.plist to a sequence of engine commands
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Mon Oct 31 01:44:32 2011 +0100
@@ -20,7 +20,6 @@
 
 
 #import "GameInterfaceBridge.h"
-#import "ServerSetup.h"
 #import "EngineProtocolNetwork.h"
 #import "OverlayViewController.h"
 #import "StatsPageViewController.h"
@@ -32,7 +31,7 @@
 
 -(id) initWithController:(id) viewController {
     if (self = [super init]) {
-        self.ipcPort = [ServerSetup randomPort];
+        self.ipcPort = [HWUtils randomPort];
         self.gameType = gtNone;
         self.savePath = nil;
 
--- a/project_files/HedgewarsMobile/Classes/HWUtils.h	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/HWUtils.h	Mon Oct 31 01:44:32 2011 +0100
@@ -27,6 +27,8 @@
 
 +(NSString *)modelType;
 +(NSArray *)teamColors;
++(NSInteger) randomPort;
++(BOOL) isNetworkReachable;
 +(void) releaseCache;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/HWUtils.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/HWUtils.m	Mon Oct 31 01:44:32 2011 +0100
@@ -22,6 +22,8 @@
 #import "HWUtils.h"
 #import <sys/types.h>
 #import <sys/sysctl.h>
+#import <netinet/in.h>
+#import <SystemConfiguration/SCNetworkReachability.h>
 #import "hwconsts.h"
 
 static NSString *cachedModel = nil;
@@ -60,6 +62,46 @@
     return cachedColors;
 }
 
++(NSInteger) randomPort {
+    srandom(time(NULL));
+    NSInteger res = (random() % 64511) + 1024;
+    return (res == NETGAME_DEFAULT_PORT) ? [HWUtils randomPort] : res;
+}
+
++(BOOL) isNetworkReachable {
+    // Create zero addy
+    struct sockaddr_in zeroAddress;
+    bzero(&zeroAddress, sizeof(zeroAddress));
+    zeroAddress.sin_len = sizeof(zeroAddress);
+    zeroAddress.sin_family = AF_INET;
+
+    // Recover reachability flags
+    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
+    SCNetworkReachabilityFlags flags;
+
+    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
+    CFRelease(defaultRouteReachability);
+
+    if (!didRetrieveFlags) {
+        NSLog(@"Error. Could not recover network reachability flags");
+        return NO;
+    }
+
+    BOOL isReachable = flags & kSCNetworkFlagsReachable;
+    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
+    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
+
+    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
+    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
+                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
+                                             timeoutInterval:20.0];
+    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
+    BOOL testResult = testConnection ? YES : NO;
+    [testConnection release];
+
+    return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
+}
+
 +(void) releaseCache {
     releaseAndNil(cachedModel);
     releaseAndNil(cachedColors);
--- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Mon Oct 31 01:44:32 2011 +0100
@@ -29,7 +29,7 @@
 #import "MissionTrainingViewController.h"
 #import "GameInterfaceBridge.h"
 #import "Appirater.h"
-#import "ServerSetup.h"
+#import "ServerProtocolNetwork.h"
 
 
 @implementation MainMenuViewController
@@ -122,15 +122,11 @@
         [Appirater appLaunched];
     }
 
-
     /*
-    ServerSetup *setup = [[ServerSetup alloc] init];
-    if (isNetworkReachable()) {
-        DLog(@"network is reachable");
-        [NSThread detachNewThreadSelector:@selector(serverProtocol)
+    ServerProtocolNetwork *setup = [[ServerProtocolNetwork alloc] init];
+    [NSThread detachNewThreadSelector:@selector(serverProtocol)
                                  toTarget:setup
                                withObject:nil];
-    }
     [setup release];
     */
 }
--- a/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m	Mon Oct 31 01:44:32 2011 +0100
@@ -22,7 +22,6 @@
 #import "MapPreviewButtonView.h"
 #import "MapConfigViewController.h"
 #import "UIImageExtra.h"
-#import "ServerSetup.h"
 #import <pthread.h>
 #import <QuartzCore/QuartzCore.h>
 
@@ -69,7 +68,7 @@
     IPaddress ip;
     BOOL serverQuit = NO;
     static uint8_t map[128*32];
-    int port = [ServerSetup randomPort];
+    int port = [HWUtils randomPort];
 
     if (SDLNet_Init() < 0) {
         DLog(@"SDLNet_Init: %s", SDLNet_GetError());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.h	Mon Oct 31 01:44:32 2011 +0100
@@ -0,0 +1,33 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 16/12/2010.
+ */
+
+
+#import <Foundation/Foundation.h>
+#import "EngineProtocolNetwork.h"
+
+@interface ServerProtocolNetwork : NSObject {
+    NSInteger serverPort;
+    NSString *serverAddress;
+}
+
+@property (assign) NSInteger serverPort;
+@property (nonatomic,retain) NSString *serverAddress;
+
+@end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/ServerProtocolNetwork.m	Mon Oct 31 01:44:32 2011 +0100
@@ -0,0 +1,190 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 10/01/2010.
+ */
+
+
+#import "ServerProtocolNetwork.h"
+#import "SDL_net.h"
+#import "hwconsts.h"
+
+#define BUFFER_SIZE 256
+
+static TCPsocket sd;
+static ServerProtocolNetwork *serverConnection;
+
+@implementation ServerProtocolNetwork
+@synthesize serverPort, serverAddress;
+
+-(id) init {
+    if (self = [super init]) {
+        self.serverPort = NETGAME_DEFAULT_PORT;
+        self.serverAddress = @"netserver.hedgewars.org";
+    }
+    serverConnection = self;
+    return self;
+}
+
+-(void) dealloc {
+    releaseAndNil(serverAddress);
+    [super dealloc];
+}
+
+-(int) sendToServer:(NSString *)command {
+    NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
+    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
+    [message release];
+    return result;
+}
+
+-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
+    NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
+    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
+    [message release];
+    return result;
+}
+
+-(void) serverProtocol {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    IPaddress ip;
+    BOOL clientQuit = NO;
+    char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
+    int dim = BUFFER_SIZE;
+    uint8_t msgSize;
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+
+    if (SDLNet_Init() < 0) {
+        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
+        clientQuit = YES;
+    }
+
+    // Resolving the host using NULL make network interface to listen
+    if (SDLNet_ResolveHost(&ip, [self.serverAddress UTF8String] , self.serverPort) < 0 && !clientQuit) {
+        DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError());
+        clientQuit = YES;
+    }
+
+    // Open a connection with the IP provided (listen on the host's port)
+    if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
+        DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort);
+        clientQuit = YES;
+    }
+
+    DLog(@"Found server on port %d", self.serverPort);
+    while (!clientQuit) {
+        int index = 0;
+        BOOL exitBufferLoop = NO;
+        memset(buffer, '\0', dim);
+
+        while (exitBufferLoop != YES) {
+            msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
+
+            // exit in case of error
+            if (msgSize <= 0) {
+                DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
+                clientQuit = YES;
+                break;
+            }
+
+            // update index position and check for End-Of-Message
+            index += msgSize;
+            if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
+                exitBufferLoop = YES;
+            }
+
+            // if message is too big allocate new space
+            if (index >= dim) {
+                dim += BUFFER_SIZE;
+                buffer = (char *)realloc(buffer, dim);
+                if (buffer == NULL) {
+                    clientQuit = YES;
+                    break;
+                }
+            }
+        }
+
+        NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
+        NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
+        [bufferedMessage release];
+        NSString *command = [listOfCommands objectAtIndex:0];
+        DLog(@"size = %d, %@", index-2, listOfCommands);
+        if ([command isEqualToString:@"PING"]) {
+            if ([listOfCommands count] > 1)
+                [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
+            else
+                [self sendToServer:@"PONG"];
+            DLog(@"PONG");
+        }
+        else if ([command isEqualToString:@"NICK"]) {
+            //what is this for?
+        }
+        else if ([command isEqualToString:@"PROTO"]) {
+            //what is this for?
+        }
+        else if ([command isEqualToString:@"ROOM"]) {
+            //TODO: stub
+        }
+        else if ([command isEqualToString:@"LOBBY:LEFT"]) {
+            //TODO: stub
+        }
+        else if ([command isEqualToString:@"LOBBY:JOINED"]) {
+            //TODO: stub
+        }
+        else if ([command isEqualToString:@"ASKPASSWORD"]) {
+            NSString *pwd = [defaults objectForKey:@"password"];
+            [self sendToServer:@"PASSWORD" withArgument:pwd];
+        }
+        else if ([command isEqualToString:@"CONNECTED"]) {
+            int netProto;
+            char *versionStr;
+            HW_versionInfo(&netProto, &versionStr);
+            NSString *nick = [defaults objectForKey:@"username"];
+            [self sendToServer:@"NICK" withArgument:nick];
+            [self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
+        }
+        else if ([command isEqualToString:@"SERVER_MESSAGE"]) {
+            DLog(@"%@", [listOfCommands objectAtIndex:1]);
+        }
+        else if ([command isEqualToString:@"WARNING"]) {
+            if ([listOfCommands count] > 1)
+                DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
+            else
+                DLog(@"Server warning - unknown");
+        }
+        else if ([command isEqualToString:@"ERROR"]) {
+            DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
+        }
+        else if ([command isEqualToString:@"BYE"]) {
+            //TODO: handle "Reconnected too fast"
+            DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
+            clientQuit = YES;
+        }
+        else {
+            DLog(@"Unknown/Unsupported message received: %@", command);
+        }
+    }
+    DLog(@"Server closed connection, ending thread");
+
+    free(buffer);
+    SDLNet_TCP_Close(sd);
+    SDLNet_Quit();
+
+    [pool release];
+}
+
+@end
--- a/project_files/HedgewarsMobile/Classes/ServerSetup.h	Sun Oct 30 23:24:36 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-/*
- * Hedgewars-iOS, a Hedgewars port for iOS devices
- * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * File created on 16/12/2010.
- */
-
-
-#import <Foundation/Foundation.h>
-#import "SDL_net.h"
-
-@interface ServerSetup : NSObject {
-    NSDictionary *systemSettings;
-
-    TCPsocket sd;      // External socket descriptor
-}
-
-+(NSInteger) randomPort;
-+(BOOL) isNetworkReachable;
-
-@property (nonatomic, retain) NSDictionary *systemSettings;
-
-@end
--- a/project_files/HedgewarsMobile/Classes/ServerSetup.m	Sun Oct 30 23:24:36 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,227 +0,0 @@
-/*
- * Hedgewars-iOS, a Hedgewars port for iOS devices
- * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * File created on 10/01/2010.
- */
-
-
-#import "ServerSetup.h"
-#import <netinet/in.h>
-#import <SystemConfiguration/SCNetworkReachability.h>
-
-#import "hwconsts.h"
-
-#define BUFFER_SIZE 256
-
-@implementation ServerSetup
-@synthesize systemSettings;
-
-
-+(NSInteger) randomPort {
-    srandom(time(NULL));
-    NSInteger res = (random() % 64511) + 1024;
-    return (res == NETGAME_DEFAULT_PORT) ? [ServerSetup randomPort] : res;
-}
-
-+(BOOL) isNetworkReachable {
-    // Create zero addy
-    struct sockaddr_in zeroAddress;
-    bzero(&zeroAddress, sizeof(zeroAddress));
-    zeroAddress.sin_len = sizeof(zeroAddress);
-    zeroAddress.sin_family = AF_INET;
-
-    // Recover reachability flags
-    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
-    SCNetworkReachabilityFlags flags;
-
-    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
-    CFRelease(defaultRouteReachability);
-
-    if (!didRetrieveFlags) {
-        NSLog(@"Error. Could not recover network reachability flags");
-        return NO;
-    }
-
-    BOOL isReachable = flags & kSCNetworkFlagsReachable;
-    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
-    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
-
-    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
-    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
-                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
-                                             timeoutInterval:20.0];
-    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
-    BOOL testResult = testConnection ? YES : NO;
-    [testConnection release];
-
-    return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
-}
-
--(id) init {
-    if (self = [super init]) {
-        self.systemSettings = nil; //nsuserdefault
-    }
-    return self;
-}
-
--(void) dealloc {
-
-    [super dealloc];
-}
-
--(int) sendToServer:(NSString *)command {
-    NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
-    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
-    [message release];
-    return result;
-}
-
--(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
-    NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
-    int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
-    [message release];
-    return result;
-}
-
--(void) serverProtocol {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    IPaddress ip;
-    BOOL clientQuit = NO;
-    char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
-    int dim = BUFFER_SIZE;
-    uint8_t msgSize;
-
-    if (SDLNet_Init() < 0) {
-        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
-        clientQuit = YES;
-    }
-
-    // Resolving the host using NULL make network interface to listen
-    if (SDLNet_ResolveHost(&ip, "netserver.hedgewars.org", NETGAME_DEFAULT_PORT) < 0 && !clientQuit) {
-        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
-        clientQuit = YES;
-    }
-
-    // Open a connection with the IP provided (listen on the host's port)
-    if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
-        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), NETGAME_DEFAULT_PORT);
-        clientQuit = YES;
-    }
-
-    DLog(@"Found server on port %d", NETGAME_DEFAULT_PORT);
-    while (!clientQuit) {
-        int index = 0;
-        BOOL exitBufferLoop = NO;
-        memset(buffer, '\0', dim);
-        
-        while (exitBufferLoop != YES) {
-            msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
-            
-            // exit in case of error
-            if (msgSize <= 0) {
-                DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
-                clientQuit = YES;
-                break;
-            }
-            
-            // update index position and check for End-Of-Message
-            index += msgSize;
-            if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
-                exitBufferLoop = YES;
-            }
-            
-            // if message is too big allocate new space
-            if (index >= dim) {
-                dim += BUFFER_SIZE;
-                buffer = (char *)realloc(buffer, dim);
-                if (buffer == NULL) {
-                    clientQuit = YES;
-                    break;
-                }
-            }
-        }
-
-        NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
-        NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
-        [bufferedMessage release];
-        NSString *command = [listOfCommands objectAtIndex:0];
-        DLog(@"size = %d, %@", index-2, listOfCommands);
-        if ([command isEqualToString:@"PING"]) {
-            if ([listOfCommands count] > 1)
-                [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
-            else
-                [self sendToServer:@"PONG"];
-            DLog(@"PONG");
-        }
-        else if ([command isEqualToString:@"NICK"]) {
-            //what is this for?
-        }
-        else if ([command isEqualToString:@"PROTO"]) {
-            //what is this for?
-        }
-        else if ([command isEqualToString:@"ROOM"]) {
-            //TODO: stub
-        }
-        else if ([command isEqualToString:@"LOBBY:LEFT"]) {
-            //TODO: stub
-        }
-        else if ([command isEqualToString:@"LOBBY:JOINED"]) {
-            //TODO: stub
-        }
-        else if ([command isEqualToString:@"ASKPASSWORD"]) {
-            NSString *pwd = [self.systemSettings objectForKey:@"password"];
-            [self sendToServer:@"PASSWORD" withArgument:pwd];
-        }
-        else if ([command isEqualToString:@"CONNECTED"]) {
-            int netProto;
-            char *versionStr;
-            HW_versionInfo(&netProto, &versionStr);
-            NSString *nick = [self.systemSettings objectForKey:@"username"];
-            [self sendToServer:@"NICK" withArgument:nick];
-            [self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
-        }
-        else if ([command isEqualToString:@"SERVER_MESSAGE"]) {
-            DLog(@"%@", [listOfCommands objectAtIndex:1]);
-        }
-        else if ([command isEqualToString:@"WARNING"]) {
-            if ([listOfCommands count] > 1)
-                DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
-            else
-                DLog(@"Server warning - unknown");
-        }
-        else if ([command isEqualToString:@"ERROR"]) {
-            DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
-        }
-        else if ([command isEqualToString:@"BYE"]) {
-            //TODO: handle "Reconnected too fast"
-            DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
-            clientQuit = YES;
-        }
-        else {
-            DLog(@"Unknown/Unsupported message received: %@", command);
-        }
-    }
-    DLog(@"Server closed connection, ending thread");
-
-    free(buffer);
-    SDLNet_TCP_Close(sd);
-    SDLNet_Quit();
-
-    [pool release];
-}
-
-@end
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Sun Oct 30 23:24:36 2011 +0100
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Mon Oct 31 01:44:32 2011 +0100
@@ -230,7 +230,7 @@
 		61DF0EDC1284DF2300F3F10B /* HelpPageLobbyViewController-iPhone.xib in Resources */ = {isa = PBXBuildFile; fileRef = 61DF0EDB1284DF2300F3F10B /* HelpPageLobbyViewController-iPhone.xib */; };
 		61DF0F211284F72A00F3F10B /* HelpPageInGameViewController-iPhone.xib in Resources */ = {isa = PBXBuildFile; fileRef = 61DF0F201284F72A00F3F10B /* HelpPageInGameViewController-iPhone.xib */; };
 		61E1F4F811D004240016A5AA /* adler32.pas in Sources */ = {isa = PBXBuildFile; fileRef = 61E1F4F711D004240016A5AA /* adler32.pas */; };
-		61E2E12E12BAAEE30051B659 /* ServerSetup.m in Sources */ = {isa = PBXBuildFile; fileRef = 61E2E12D12BAAEE30051B659 /* ServerSetup.m */; };
+		61E2E12E12BAAEE30051B659 /* ServerProtocolNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = 61E2E12D12BAAEE30051B659 /* ServerProtocolNetwork.m */; };
 		61E2F7441283752C00E12521 /* fb.png in Resources */ = {isa = PBXBuildFile; fileRef = 61E2F7421283752C00E12521 /* fb.png */; };
 		61E2F7451283752C00E12521 /* tw.png in Resources */ = {isa = PBXBuildFile; fileRef = 61E2F7431283752C00E12521 /* tw.png */; };
 		61E5D68D12AB006F00566F29 /* uLandPainted.pas in Sources */ = {isa = PBXBuildFile; fileRef = 61E5D68C12AB006F00566F29 /* uLandPainted.pas */; };
@@ -603,8 +603,8 @@
 		61DF0EDB1284DF2300F3F10B /* HelpPageLobbyViewController-iPhone.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = "HelpPageLobbyViewController-iPhone.xib"; sourceTree = "<group>"; };
 		61DF0F201284F72A00F3F10B /* HelpPageInGameViewController-iPhone.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = "HelpPageInGameViewController-iPhone.xib"; sourceTree = "<group>"; };
 		61E1F4F711D004240016A5AA /* adler32.pas */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.pascal; name = adler32.pas; path = ../../hedgewars/adler32.pas; sourceTree = SOURCE_ROOT; };
-		61E2E12C12BAAEE30051B659 /* ServerSetup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServerSetup.h; sourceTree = "<group>"; };
-		61E2E12D12BAAEE30051B659 /* ServerSetup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ServerSetup.m; sourceTree = "<group>"; };
+		61E2E12C12BAAEE30051B659 /* ServerProtocolNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServerProtocolNetwork.h; sourceTree = "<group>"; };
+		61E2E12D12BAAEE30051B659 /* ServerProtocolNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ServerProtocolNetwork.m; sourceTree = "<group>"; };
 		61E2F7421283752C00E12521 /* fb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = fb.png; path = Resources/Icons/fb.png; sourceTree = "<group>"; };
 		61E2F7431283752C00E12521 /* tw.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = tw.png; path = Resources/Icons/tw.png; sourceTree = "<group>"; };
 		61E5D68C12AB006F00566F29 /* uLandPainted.pas */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.pascal; name = uLandPainted.pas; path = ../../hedgewars/uLandPainted.pas; sourceTree = SOURCE_ROOT; };
@@ -686,8 +686,8 @@
 				61EDB5AF135B3F97009B29A6 /* GameInterfaceBridge.m */,
 				616591E611CA9BA200D6E256 /* EngineProtocolNetwork.h */,
 				616591E711CA9BA200D6E256 /* EngineProtocolNetwork.m */,
-				61E2E12C12BAAEE30051B659 /* ServerSetup.h */,
-				61E2E12D12BAAEE30051B659 /* ServerSetup.m */,
+				61E2E12C12BAAEE30051B659 /* ServerProtocolNetwork.h */,
+				61E2E12D12BAAEE30051B659 /* ServerProtocolNetwork.m */,
 			);
 			path = Classes;
 			sourceTree = "<group>";
@@ -1641,7 +1641,7 @@
 				61E5D68D12AB006F00566F29 /* uLandPainted.pas in Sources */,
 				61F544C712AF1748007FD913 /* HoldTableViewCell.m in Sources */,
 				61AC067412B2E32D000B52A2 /* Appirater.m in Sources */,
-				61E2E12E12BAAEE30051B659 /* ServerSetup.m in Sources */,
+				61E2E12E12BAAEE30051B659 /* ServerProtocolNetwork.m in Sources */,
 				61B7A33812CC21080086B604 /* StatsPageViewController.m in Sources */,
 				61EDB5B0135B3F97009B29A6 /* GameInterfaceBridge.m in Sources */,
 				61A976B3136F668500DD9878 /* uCursor.pas in Sources */,