cocoaTouch/gameSetup.m
author koda
Sun, 10 Jan 2010 00:52:20 +0000
changeset 2691 c0da3a98c01c
child 2692 ce9992075118
permissions -rw-r--r--
initial support for engine protocol
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2691
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     1
//
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     2
//  gameSetup.m
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     3
//  hwengine
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     4
//
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     5
//  Created by Vittorio on 10/01/10.
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     7
//
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     8
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
     9
#import <pthread.h>
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    10
#import "SDL_uikitappdelegate.h"
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    11
#import "gameSetup.h"
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    12
#import "SDL_net.h"
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    13
#import "PascalImports.h"
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    14
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    15
#define IPC_PORT 51342
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    16
#define IPC_PORT_STR "51342"
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    17
#define BUFFER_SIZE 256
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    18
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    19
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    20
@implementation gameSetup
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    21
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    22
void engineProtocolThread () {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    23
	TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    24
	IPaddress ip;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    25
	int idx, eProto;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    26
	BOOL serverQuit, clientQuit;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    27
	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    28
	Uint8 msgSize;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    29
	Uint16 gameTicks;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    30
	
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    31
	if (SDLNet_Init() < 0) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    32
		fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    33
		exit(EXIT_FAILURE);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    34
	}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    35
	
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    36
	/* Resolving the host using NULL make network interface to listen */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    37
	if (SDLNet_ResolveHost(&ip, NULL, IPC_PORT) < 0) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    38
		fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    39
		exit(EXIT_FAILURE);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    40
	}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    41
	
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    42
	/* Open a connection with the IP provided (listen on the host's port) */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    43
	if (!(sd = SDLNet_TCP_Open(&ip))) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    44
		fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    45
		exit(EXIT_FAILURE);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    46
	}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    47
	
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    48
	NSLog(@"engineProtocolThread - Waiting for a client");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    49
	
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    50
	serverQuit = NO;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    51
	while (!serverQuit) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    52
		
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    53
		/* This check the sd if there is a pending connection.
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    54
		 * If there is one, accept that, and open a new socket for communicating */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    55
		if ((csd = SDLNet_TCP_Accept(sd))) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    56
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    57
			NSLog(@"engineProtocolThread - Client found");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    58
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    59
			//first byte of the command alwayas contain the size of the command
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    60
			SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8));
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    61
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    62
			SDLNet_TCP_Recv(csd, buffer, msgSize);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    63
			gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    64
			NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    65
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    66
			if ('C' == buffer[0]) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    67
				NSLog(@"engineProtocolThread - Client found and connected");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    68
				clientQuit = NO;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    69
			} else {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    70
				NSLog(@"engineProtocolThread - wrong Connected message, closing");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    71
				clientQuit = YES;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    72
			}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    73
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    74
			while (!clientQuit){
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    75
				/* Now we can communicate with the client using csd socket
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    76
				 * sd will remain opened waiting other connections */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    77
				idx = 0;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    78
				msgSize = 0;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    79
				memset(buffer, 0, BUFFER_SIZE);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    80
				memset(string, 0, BUFFER_SIZE);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    81
				SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8));
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    82
			
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    83
				SDLNet_TCP_Recv(csd, buffer, msgSize);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    84
				gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    85
				NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    86
				
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    87
				switch (buffer[0]) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    88
					case '?':
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    89
						NSLog(@"Ping? Pong!");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    90
						string[idx++] = 0x01;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    91
						string[idx++] = '!';
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    92
						
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    93
						SDLNet_TCP_Send(csd, string, idx);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    94
						break;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    95
					case 'E':
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    96
						NSLog(@"ERROR - last console line: [%s]", buffer);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    97
						clientQuit = YES;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    98
						break;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
    99
					default:
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   100
						sscanf(buffer, "%*s %d", &eProto);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   101
						if (HW_protoVer() == eProto) {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   102
							NSLog(@"Setting protocol version %s", buffer);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   103
						} else {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   104
							NSLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   105
							clientQuit = YES;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   106
						}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   107
						
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   108
						break;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   109
				} 
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   110
				
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   111
				/*
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   112
				 // Terminate this connection 
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   113
				 if(strcmp(buffer, "exit") == 0)	{
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   114
				 quit2 = 1;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   115
				 printf("Terminate connection\n");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   116
				 }
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   117
				 // Quit the thread
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   118
				 if(strcmp(buffer, "quit") == 0)	{
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   119
				 quit2 = 1;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   120
				 quit = 1;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   121
				 printf("Quit program\n");
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   122
				 }
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   123
				 */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   124
			}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   125
		}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   126
		
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   127
		/* Close the client socket */
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   128
		SDLNet_TCP_Close(csd);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   129
	}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   130
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   131
	SDLNet_TCP_Close(sd);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   132
	SDLNet_Quit();
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   133
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   134
	pthread_exit(NULL);
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   135
}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   136
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   137
void setupArgsForLocalPlay() {
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   138
	forward_argc = 18;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   139
	forward_argv = (char **)realloc(forward_argv, forward_argc * sizeof(char *));
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   140
	//forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   141
	forward_argv[ 1] = forward_argv[0];	// (UNUSED)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   142
	forward_argv[ 2] = "320";			// cScreenWidth (NO EFFECT)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   143
	forward_argv[ 3] = "480";			// cScreenHeight (NO EFFECT)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   144
	forward_argv[ 4] = "32";			// cBitsStr
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   145
	forward_argv[ 5] = IPC_PORT_STR;	// ipcPort; <- (MAIN TODO)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   146
	forward_argv[ 6] = "1";				// cFullScreen (NO EFFECT)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   147
	forward_argv[ 7] = "0";				// isSoundEnabled (TOSET)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   148
	forward_argv[ 8] = "1";				// cVSyncInUse (UNUSED)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   149
	forward_argv[ 9] = "en.txt";		// cLocaleFName (TOSET)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   150
	forward_argv[10] = "100";			// cInitVolume (TOSET)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   151
	forward_argv[11] = "8";				// cTimerInterval
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   152
	forward_argv[12] = "Data";			// PathPrefix
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   153
	forward_argv[13] = "1";				// cShowFPS (TOSET?)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   154
	forward_argv[14] = "0";				// cAltDamage (TOSET)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   155
	forward_argv[15] = "Koda";			// UserNick (DecodeBase64(ParamStr(15)) FTW) <- TODO
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   156
	forward_argv[16] = "0";				// isMusicEnabled (TOSET)
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   157
	forward_argv[17] = "0";				// cReducedQuality
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   158
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   159
	return;
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   160
}
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   161
c0da3a98c01c initial support for engine protocol
koda
parents:
diff changeset
   162
@end