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