# HG changeset patch # User koda # Date 1292127817 -3600 # Node ID ce9b8206e681abdb3d52f8ec8b6d5a4f3ec38ab4 # Parent 816a0bff50195a033942ff6f41dac83f75905376 add some connection facility to netserver diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Classes/CommodityFunctions.h --- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.h Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.h Sun Dec 12 05:23:37 2010 +0100 @@ -75,3 +75,10 @@ UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor); CGSize PSPNGSizeFromMetaData (NSString *aFileName); + +@interface NSString (extra) + +-(NSString *) getMD5hash; + +@end + diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Classes/CommodityFunctions.m --- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.m Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.m Sun Dec 12 05:23:37 2010 +0100 @@ -25,7 +25,8 @@ #import #import #import -#import "AudioToolbox/AudioToolbox.h" +#import +#import #import "PascalImports.h" BOOL inline rotationManager (UIInterfaceOrientation interfaceOrientation) { @@ -183,3 +184,19 @@ // Return Size. return CGSizeMake(resultWidth,resultHeight); } + +@implementation NSString (extra) + +-(NSString *)getMD5hash { + const char *cStr = [self UTF8String]; + unsigned char result[16]; + CC_MD5( cStr, strlen(cStr), result ); + return [NSString stringWithFormat: + @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", + result[0], result[1], result[2], result[3], result[4], result[5], + result[6], result[7], result[8], result[9], result[10], result[11], + result[12], result[13], result[14], result[15]]; +} + + +@end diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Classes/GameSetup.h --- a/project_files/HedgewarsMobile/Classes/GameSetup.h Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Classes/GameSetup.h Sun Dec 12 05:23:37 2010 +0100 @@ -28,6 +28,7 @@ NSInteger ipcPort; // Port on which engine will listen TCPsocket csd; // Client socket descriptor + TCPsocket esd; // External socket descriptor NSString *savePath; BOOL isNetGame; diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Classes/GameSetup.m --- a/project_files/HedgewarsMobile/Classes/GameSetup.m Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Classes/GameSetup.m Sun Dec 12 05:23:37 2010 +0100 @@ -250,6 +250,114 @@ return SDLNet_TCP_Send(csd, [string UTF8String], length); } +-(void) serverProtocol { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + TCPsocket sd; + IPaddress ip; + BOOL clientQuit = NO; + uint8_t buffer[BUFFER_SIZE]; + uint8_t msgSize; + NSString *message = nil; + + 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", DEFAULT_NETGAME_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); + clientQuit = YES; + } + + DLog(@"Found server on port %d", DEFAULT_NETGAME_PORT); + while (!clientQuit) { + memset(buffer, '\0', BUFFER_SIZE); + msgSize = SDLNet_TCP_Recv(sd, buffer, BUFFER_SIZE); + if (msgSize <= 0) { + DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); + clientQuit = YES; + break; + } + + NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:msgSize encoding:NSASCIIStringEncoding]; + NSMutableArray *listOfCommands = [NSMutableArray arrayWithArray:[bufferedMessage componentsSeparatedByString:@"\n"]]; + [listOfCommands removeLastObject]; + [listOfCommands removeLastObject]; + NSString *command = [listOfCommands objectAtIndex:0]; + DLog(@"size = %d, %@", msgSize, listOfCommands); + [bufferedMessage release]; + if ([command isEqualToString:@"CONNECTED"]) { + message = [[NSString alloc] initWithFormat:@"NICK\nkoda\n\n"]; + SDLNet_TCP_Send(sd, [message UTF8String], [message length]); + [message release]; + + message = [[NSString alloc] initWithFormat:@"PROTO\n34\n\n"]; + SDLNet_TCP_Send(sd, [message UTF8String], [message length]); + [message release]; + } + else if ([command isEqualToString:@"PING"]) { + if ([listOfCommands count] > 1) + message = [[NSString alloc] initWithFormat:@"PONG\n%@\n\n",[listOfCommands objectAtIndex:1]]; + else + message = [[NSString alloc] initWithString:@"PONG\n\n"]; + + SDLNet_TCP_Send(sd, [message UTF8String], [message length]); + [message release]; + } + else if ([command isEqualToString:@"NICK"]) { + //TODO: what is this for? + } + else if ([command isEqualToString:@"PROTO"]) { + if ([[listOfCommands objectAtIndex:1] intValue] == 34) { + //TODO: unused + } + } + 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:@"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 exited, ending thread"); + + // Close the client socket + SDLNet_TCP_Close(sd); + SDLNet_Quit(); + + [pool release]; +} + // method that handles net setup with engine and keeps connection alive -(void) engineProtocol { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -388,7 +496,7 @@ break; } } - DLog(@"Engine exited, closing server"); + DLog(@"Engine exited, ending thread"); // wait a little to let the client close cleanly [NSThread sleepForTimeInterval:2]; // Close the client socket diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m --- a/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m Sun Dec 12 05:23:37 2010 +0100 @@ -122,8 +122,10 @@ GameSetup *setup = [[GameSetup alloc] initWithDictionary:gameDictionary]; NSNumber *isNetGameNum = [gameDictionary objectForKey:@"netgame"]; - if ([isNetGameNum boolValue] == NO) - [setup startThread:@"engineProtocol"]; + [setup startThread:@"engineProtocol"]; + if ([isNetGameNum boolValue] == YES) + [setup startThread:@"serverProtocol"]; + const char **gameArgs = [setup getSettings:[gameDictionary objectForKey:@"savefile"]]; NSNumber *menuStyle = [NSNumber numberWithBool:setup.menuStyle]; [setup release]; diff -r 816a0bff5019 -r ce9b8206e681 project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj --- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj Sat Dec 11 23:28:52 2010 +0300 +++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj Sun Dec 12 05:23:37 2010 +0100 @@ -697,21 +697,21 @@ isa = PBXContainerItemProxy; containerPortal = 6172554E12B3DCEE0098D069 /* Lua.xcodeproj */; proxyType = 2; - remoteGlobalIDString = 1D6058910D05DD3D006BFB54 /* Lua.app */; + remoteGlobalIDString = 1D6058910D05DD3D006BFB54; remoteInfo = "Test Lua"; }; 6172555612B3DCEE0098D069 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 6172554E12B3DCEE0098D069 /* Lua.xcodeproj */; proxyType = 2; - remoteGlobalIDString = 506EE05E10304ED200A389B3 /* libcocos2d libraries.a */; + remoteGlobalIDString = 506EE05E10304ED200A389B3; remoteInfo = "cocos2d libraries"; }; 6172555812B3DCEE0098D069 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 6172554E12B3DCEE0098D069 /* Lua.xcodeproj */; proxyType = 2; - remoteGlobalIDString = E14CF7C110ABC177005470B6 /* libLua.a */; + remoteGlobalIDString = E14CF7C110ABC177005470B6; remoteInfo = Lua; }; 617988D9114AAA3900BA94A9 /* PBXContainerItemProxy */ = {