232 [os close]; |
232 [os close]; |
233 [os release]; |
233 [os release]; |
234 } |
234 } |
235 |
235 |
236 // wrapper that computes the length of the message and then sends the command string, saving the command on a file |
236 // wrapper that computes the length of the message and then sends the command string, saving the command on a file |
237 -(int) sendToEngine: (NSString *)string { |
237 -(int) sendToEngine:(NSString *)string { |
238 uint8_t length = [string length]; |
238 uint8_t length = [string length]; |
239 |
239 |
240 [self dumpRawData:(const uint8_t *)[string UTF8String] ofSize:length]; |
240 [self dumpRawData:(const uint8_t *)[string UTF8String] ofSize:length]; |
241 SDLNet_TCP_Send(csd, &length, 1); |
241 SDLNet_TCP_Send(csd, &length, 1); |
242 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
242 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
243 } |
243 } |
244 |
244 |
245 // wrapper that computes the length of the message and then sends the command string, skipping file writing |
245 // wrapper that computes the length of the message and then sends the command string, skipping file writing |
246 -(int) sendToEngineNoSave: (NSString *)string { |
246 -(int) sendToEngineNoSave:(NSString *)string { |
247 uint8_t length = [string length]; |
247 uint8_t length = [string length]; |
248 |
248 |
249 SDLNet_TCP_Send(csd, &length, 1); |
249 SDLNet_TCP_Send(csd, &length, 1); |
250 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
250 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
251 } |
251 } |
252 |
252 |
|
253 -(int) sendToServer:(NSString *)command withArgument:(NSString *)argument { |
|
254 NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument]; |
|
255 int result = SDLNet_TCP_Send(esd, [message UTF8String], [message length]); |
|
256 [message release]; |
|
257 return result; |
|
258 } |
|
259 |
|
260 -(int) sendToServer:(NSString *)command { |
|
261 NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command]; |
|
262 int result = SDLNet_TCP_Send(esd, [message UTF8String], [message length]); |
|
263 [message release]; |
|
264 return result; |
|
265 } |
|
266 |
253 -(void) serverProtocol { |
267 -(void) serverProtocol { |
254 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
268 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
255 TCPsocket sd; |
|
256 IPaddress ip; |
269 IPaddress ip; |
257 BOOL clientQuit = NO; |
270 BOOL clientQuit = NO; |
258 uint8_t buffer[BUFFER_SIZE]; |
271 char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE); |
|
272 int dim = BUFFER_SIZE; |
259 uint8_t msgSize; |
273 uint8_t msgSize; |
260 NSString *message = nil; |
274 NSString *arg = nil; |
261 |
275 |
262 if (SDLNet_Init() < 0) { |
276 if (SDLNet_Init() < 0) { |
263 DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
277 DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
264 clientQuit = YES; |
278 clientQuit = YES; |
265 } |
279 } |
269 DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
283 DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
270 clientQuit = YES; |
284 clientQuit = YES; |
271 } |
285 } |
272 |
286 |
273 // Open a connection with the IP provided (listen on the host's port) |
287 // Open a connection with the IP provided (listen on the host's port) |
274 if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
288 if (!(esd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
275 DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
289 DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), DEFAULT_NETGAME_PORT); |
276 clientQuit = YES; |
290 clientQuit = YES; |
277 } |
291 } |
278 |
292 |
279 DLog(@"Found server on port %d", DEFAULT_NETGAME_PORT); |
293 DLog(@"Found server on port %d", DEFAULT_NETGAME_PORT); |
280 while (!clientQuit) { |
294 while (!clientQuit) { |
281 memset(buffer, '\0', BUFFER_SIZE); |
295 int index = 0; |
282 msgSize = SDLNet_TCP_Recv(sd, buffer, BUFFER_SIZE); |
296 BOOL exitBufferLoop = NO; |
283 if (msgSize <= 0) { |
297 memset(buffer, '\0', dim); |
284 DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
298 |
285 clientQuit = YES; |
299 while (exitBufferLoop != YES) { |
286 break; |
300 msgSize = SDLNet_TCP_Recv(esd, &buffer[index], 2); |
287 } |
301 |
288 |
302 // exit in case of error |
289 NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:msgSize encoding:NSASCIIStringEncoding]; |
303 if (msgSize <= 0) { |
290 NSMutableArray *listOfCommands = [NSMutableArray arrayWithArray:[bufferedMessage componentsSeparatedByString:@"\n"]]; |
304 DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
291 [listOfCommands removeLastObject]; |
305 clientQuit = YES; |
292 [listOfCommands removeLastObject]; |
306 break; |
|
307 } |
|
308 |
|
309 // update index position and check for End-Of-Message |
|
310 index += msgSize; |
|
311 if (strncmp(&buffer[index-2], "\n\n", 2) == 0) { |
|
312 exitBufferLoop = YES; |
|
313 } |
|
314 |
|
315 // if message is too big allocate new space |
|
316 if (index >= dim) { |
|
317 dim += BUFFER_SIZE; |
|
318 buffer = (char *)realloc(buffer, dim); |
|
319 if (buffer == NULL) { |
|
320 clientQuit = YES; |
|
321 break; |
|
322 } |
|
323 } |
|
324 } |
|
325 |
|
326 NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding]; |
|
327 NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"]; |
|
328 [bufferedMessage release]; |
293 NSString *command = [listOfCommands objectAtIndex:0]; |
329 NSString *command = [listOfCommands objectAtIndex:0]; |
294 DLog(@"size = %d, %@", msgSize, listOfCommands); |
330 DLog(@"size = %d, %@", index-2, listOfCommands); |
295 [bufferedMessage release]; |
331 if ([command isEqualToString:@"PING"]) { |
296 if ([command isEqualToString:@"CONNECTED"]) { |
|
297 message = [[NSString alloc] initWithFormat:@"NICK\nkoda\n\n"]; |
|
298 SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
|
299 [message release]; |
|
300 |
|
301 message = [[NSString alloc] initWithFormat:@"PROTO\n34\n\n"]; |
|
302 SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
|
303 [message release]; |
|
304 } |
|
305 else if ([command isEqualToString:@"PING"]) { |
|
306 if ([listOfCommands count] > 1) |
332 if ([listOfCommands count] > 1) |
307 message = [[NSString alloc] initWithFormat:@"PONG\n%@\n\n",[listOfCommands objectAtIndex:1]]; |
333 [self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]]; |
308 else |
334 else |
309 message = [[NSString alloc] initWithString:@"PONG\n\n"]; |
335 [self sendToServer:@"PONG"]; |
310 |
336 |
311 SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
337 [arg release]; |
312 [message release]; |
|
313 } |
338 } |
314 else if ([command isEqualToString:@"NICK"]) { |
339 else if ([command isEqualToString:@"NICK"]) { |
315 //TODO: what is this for? |
340 //TODO: what is this for? |
316 } |
341 } |
317 else if ([command isEqualToString:@"PROTO"]) { |
342 else if ([command isEqualToString:@"PROTO"]) { |
325 else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
350 else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
326 //TODO: stub |
351 //TODO: stub |
327 } |
352 } |
328 else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
353 else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
329 //TODO: stub |
354 //TODO: stub |
|
355 } |
|
356 else if ([command isEqualToString:@"ASKPASSWORD"]) { |
|
357 //TODO: store hashed password in settings.plist (nil here will vouluntary trigger an exception) |
|
358 [self sendToServer:@"PASSWORD" withArgument:nil]; |
|
359 } |
|
360 else if ([command isEqualToString:@"CONNECTED"]) { |
|
361 [self sendToServer:@"NICK" withArgument:@"koda"]; |
|
362 [self sendToServer:@"PROTO" withArgument:@"34"]; |
330 } |
363 } |
331 else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
364 else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
332 DLog(@"%@", [listOfCommands objectAtIndex:1]); |
365 DLog(@"%@", [listOfCommands objectAtIndex:1]); |
333 } |
366 } |
334 else if ([command isEqualToString:@"WARNING"]) { |
367 else if ([command isEqualToString:@"WARNING"]) { |