aaand remove also ipcport from the class interface as well
authorkoda
Thu, 03 Nov 2011 00:56:44 +0100
changeset 6265 a6944f94c19f
parent 6264 62d59a87daad
child 6266 b02a1e92dba2
aaand remove also ipcport from the class interface as well
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h	Wed Nov 02 19:17:07 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h	Thu Nov 03 00:56:44 2011 +0100
@@ -32,21 +32,17 @@
     id<EngineProtocolDelegate> delegate;
 
     NSOutputStream *stream;
-    NSInteger ipcPort;              // Port on which engine will listen
     TCPsocket csd;                  // Client socket descriptor
 }
 
 @property (nonatomic,assign) id<EngineProtocolDelegate> delegate;
 @property (nonatomic,retain) NSOutputStream *stream;
-@property (assign) NSInteger ipcPort;
 @property (assign) TCPsocket csd;
 
 
 -(id)   init;
--(id)   initOnPort:(NSInteger) port;
 
--(void) spawnThread:(NSString *)onSaveFile;
--(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary;
+-(NSInteger) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary;
 -(void) engineProtocol:(id) object;
 -(void) gameHasEndedWithStats:(NSArray *)stats;
 
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Wed Nov 02 19:17:07 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Thu Nov 03 00:56:44 2011 +0100
@@ -26,25 +26,18 @@
 #define BUFFER_SIZE 255     // like in original frontend
 
 @implementation EngineProtocolNetwork
-@synthesize delegate, stream, ipcPort, csd;
+@synthesize delegate, stream, csd;
 
 -(id) init {
     if (self = [super init]) {
         self.delegate = nil;
 
-        self.ipcPort = 0;
         self.csd = NULL;
         self.stream = nil;
     }
     return self;
 }
 
--(id) initOnPort:(NSInteger) port {
-    if (self = [self init])
-        self.ipcPort = port;
-    return self;
-}
-
 -(void) gameHasEndedWithStats:(NSArray *)stats {
     if (self.delegate != nil && [self.delegate respondsToSelector:@selector(gameHasEndedWithStats:)])
         [self.delegate gameHasEndedWithStats:stats];
@@ -60,17 +53,20 @@
 
 #pragma mark -
 #pragma mark Spawner functions
--(void) spawnThread:(NSString *)onSaveFile {
-    [self spawnThread:onSaveFile withOptions:nil];
-}
-
--(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary {
+-(NSInteger) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary {
     self.stream = (onSaveFile) ? [[NSOutputStream alloc] initToFileAtPath:onSaveFile append:YES] : nil;
     [self.stream open];
 
+    NSInteger ipcPort = [HWUtils randomPort];
+    NSDictionary *config = [[NSDictionary alloc] initWithObjectsAndKeys:
+                            [NSNumber numberWithInt:ipcPort],@"port",
+                            dictionary,@"config", nil];
     [NSThread detachNewThreadSelector:@selector(engineProtocol:)
                              toTarget:self
-                           withObject:dictionary];
+                           withObject:config];
+    [config release];
+
+    return ipcPort;
 }
 
 #pragma mark -
@@ -231,7 +227,8 @@
 // this is launched as thread and handles all IPC with engine
 -(void) engineProtocol:(id) object {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSDictionary *gameConfig = (NSDictionary *)object;
+    NSDictionary *gameConfig = [(NSDictionary *)object objectForKey:@"config"];
+    NSInteger port = [[(NSDictionary *)object objectForKey:@"port"] intValue];
     NSMutableArray *statsArray = nil;
     TCPsocket sd;
     IPaddress ip;
@@ -249,18 +246,18 @@
     }
 
     // Resolving the host using NULL make network interface to listen
-    if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) {
+    if (SDLNet_ResolveHost(&ip, NULL, 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(), ipcPort);
+        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
         clientQuit = YES;
     }
 
-    DLog(@"Waiting for a client on port %d", ipcPort);
+    DLog(@"Waiting for a client on port %d", port);
     while (csd == NULL)
         csd = SDLNet_TCP_Accept(sd);
     SDLNet_TCP_Close(sd);
@@ -394,6 +391,7 @@
                 break;
             case 'q':
                 // game ended, can remove the savefile and present the statistics of the match
+                [HWUtils setGameStatus:gsEnded];
                 [self gameHasEndedWithStats:statsArray];
                 break;
             case 'Q':
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h	Wed Nov 02 19:17:07 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h	Thu Nov 03 00:56:44 2011 +0100
@@ -23,11 +23,9 @@
 
 
 @interface GameInterfaceBridge : NSObject {
-    NSInteger ipcPort;  // Port on which engine will listen
+
 }
 
-@property (assign) NSInteger ipcPort;
-
 
 -(id)   initWithController:(id) viewController;
 -(void) startLocalGame:(NSDictionary *)withOptions;
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Wed Nov 02 19:17:07 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Thu Nov 03 00:56:44 2011 +0100
@@ -27,11 +27,9 @@
 #import "ObjcExports.h"
 
 @implementation GameInterfaceBridge
-@synthesize ipcPort;
 
 -(id) initWithController:(id) viewController {
     if (self = [super init]) {
-        self.ipcPort = [HWUtils randomPort];
     }
     return self;
 }
@@ -42,11 +40,11 @@
 
 #pragma mark -
 // main routine for calling the actual game engine
--(void) engineLaunch:(NSString *)path {
+-(void) engineLaunchOn:(NSInteger) ipcPort withArgument:(NSString *)path {
     const char *gameArgs[11];
     CGFloat width, height;
     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
-    NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
+    NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",ipcPort];
     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
 
@@ -104,14 +102,14 @@
 
     [HWUtils setGameStatus:gsLoading];
 
-    // this is the pascal fuction that starts the game
+    // this is the pascal function that starts the game
     Game(gameArgs);
 }
 
 // prepares the controllers for hosting a game
 -(void) prepareEngineOn:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil {
-    EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
-    [proto spawnThread:pathOrNil withOptions:optionsOrNil];
+    EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] init];
+    NSInteger ipcPort = [proto spawnThread:pathOrNil withOptions:optionsOrNil];
 
     CGRect theFrame = [[UIScreen mainScreen] bounds];
     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
@@ -139,7 +137,7 @@
     [AudioManagerController pauseBackgroundMusic];
 
     // SYSTEMS ARE GO!!
-    [self engineLaunch:pathOrNil];
+    [self engineLaunchOn:ipcPort withArgument:pathOrNil];
     
     // remove completed games notification
     [userDefaults setObject:@"" forKey:@"savedGamePath"];