249 -(void) startThread: (NSString *) selector { |
249 -(void) startThread: (NSString *) selector { |
250 SEL usage = NSSelectorFromString(selector); |
250 SEL usage = NSSelectorFromString(selector); |
251 [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
251 [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
252 } |
252 } |
253 |
253 |
|
254 -(void) dumpRawData:(const uint8_t*)buffer ofSize:(uint8_t) length { |
|
255 // is it performant to reopen the stream every time? |
|
256 NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES]; |
|
257 [os open]; |
|
258 [os write:&length maxLength:1]; |
|
259 [os write:buffer maxLength:length]; |
|
260 [os close]; |
|
261 [os release]; |
|
262 } |
|
263 |
254 // wrapper that computes the length of the message and then sends the command string, saving the command on a file |
264 // wrapper that computes the length of the message and then sends the command string, saving the command on a file |
255 -(int) sendToEngine: (NSString *)string { |
265 -(int) sendToEngine: (NSString *)string { |
256 uint8_t length = [string length]; |
266 uint8_t length = [string length]; |
257 |
267 |
258 [[NSString stringWithFormat:@"%c%@",length,string] appendToFile:savePath]; |
268 [self dumpRawData:(const uint8_t *)[string UTF8String] ofSize:length]; |
259 SDLNet_TCP_Send(csd, &length, 1); |
269 SDLNet_TCP_Send(csd, &length, 1); |
260 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
270 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
261 } |
271 } |
262 |
272 |
263 // wrapper that computes the length of the message and then sends the command string, skipping file writing |
273 // wrapper that computes the length of the message and then sends the command string, skipping file writing |
302 while (csd == NULL) |
312 while (csd == NULL) |
303 csd = SDLNet_TCP_Accept(sd); |
313 csd = SDLNet_TCP_Accept(sd); |
304 SDLNet_TCP_Close(sd); |
314 SDLNet_TCP_Close(sd); |
305 |
315 |
306 while (!clientQuit) { |
316 while (!clientQuit) { |
307 NSString *msgToSave = nil; |
|
308 NSOutputStream *os = nil; |
|
309 msgSize = 0; |
317 msgSize = 0; |
310 memset(buffer, '\0', BUFFER_SIZE); |
318 memset(buffer, '\0', BUFFER_SIZE); |
311 if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
319 if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
312 break; |
320 break; |
313 if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
321 if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
320 if (isNetGame == YES) |
328 if (isNetGame == YES) |
321 [self sendToEngineNoSave:@"TN"]; |
329 [self sendToEngineNoSave:@"TN"]; |
322 else |
330 else |
323 [self sendToEngineNoSave:@"TL"]; |
331 [self sendToEngineNoSave:@"TL"]; |
324 NSString *saveHeader = @"TS"; |
332 NSString *saveHeader = @"TS"; |
325 [[NSString stringWithFormat:@"%c%@",[saveHeader length], saveHeader] appendToFile:savePath]; |
333 [self dumpRawData:(const uint8_t *)[saveHeader UTF8String] ofSize:[saveHeader length]]; |
326 |
334 |
327 // seed info |
335 // seed info |
328 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
336 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
329 |
337 |
330 // dimension of the map |
338 // dimension of the map |
365 case 'E': |
373 case 'E': |
366 DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
374 DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
367 clientQuit = YES; |
375 clientQuit = YES; |
368 break; |
376 break; |
369 case 'e': |
377 case 'e': |
370 msgToSave = [NSString stringWithFormat:@"%c%s",msgSize,buffer]; |
378 [self dumpRawData:buffer ofSize:msgSize]; |
371 [msgToSave appendToFile:self.savePath]; |
379 |
372 |
|
373 sscanf((char *)buffer, "%*s %d", &eProto); |
380 sscanf((char *)buffer, "%*s %d", &eProto); |
374 short int netProto = 0; |
381 short int netProto = 0; |
375 char *versionStr; |
382 char *versionStr; |
376 |
383 |
377 HW_versionInfo(&netProto, &versionStr); |
384 HW_versionInfo(&netProto, &versionStr); |
402 [[NSNotificationCenter defaultCenter] postNotificationName:@"removedSave" object:nil]; |
409 [[NSNotificationCenter defaultCenter] postNotificationName:@"removedSave" object:nil]; |
403 // and disable the overlay |
410 // and disable the overlay |
404 setGameRunning(NO); |
411 setGameRunning(NO); |
405 break; |
412 break; |
406 default: |
413 default: |
407 // is it performant to reopen the stream every time? |
414 [self dumpRawData:buffer ofSize:msgSize]; |
408 os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES]; |
|
409 [os open]; |
|
410 [os write:&msgSize maxLength:1]; |
|
411 [os write:buffer maxLength:msgSize]; |
|
412 [os close]; |
|
413 [os release]; |
|
414 break; |
415 break; |
415 } |
416 } |
416 } |
417 } |
417 DLog(@"Engine exited, closing server"); |
418 DLog(@"Engine exited, closing server"); |
418 // wait a little to let the client close cleanly |
419 // wait a little to let the client close cleanly |