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 -(void) serverProtocol { |
|
254 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
255 TCPsocket sd; |
|
256 IPaddress ip; |
|
257 BOOL clientQuit = NO; |
|
258 uint8_t buffer[BUFFER_SIZE]; |
|
259 uint8_t msgSize; |
|
260 NSString *message = nil; |
|
261 |
|
262 if (SDLNet_Init() < 0) { |
|
263 DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
264 clientQuit = YES; |
|
265 } |
|
266 |
|
267 // Resolving the host using NULL make network interface to listen |
|
268 if (SDLNet_ResolveHost(&ip, "netserver.hedgewars.org", DEFAULT_NETGAME_PORT) < 0 && !clientQuit) { |
|
269 DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
|
270 clientQuit = YES; |
|
271 } |
|
272 |
|
273 // Open a connection with the IP provided (listen on the host's port) |
|
274 if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
|
275 DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
|
276 clientQuit = YES; |
|
277 } |
|
278 |
|
279 DLog(@"Found server on port %d", DEFAULT_NETGAME_PORT); |
|
280 while (!clientQuit) { |
|
281 memset(buffer, '\0', BUFFER_SIZE); |
|
282 msgSize = SDLNet_TCP_Recv(sd, buffer, BUFFER_SIZE); |
|
283 if (msgSize <= 0) { |
|
284 DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
|
285 clientQuit = YES; |
|
286 break; |
|
287 } |
|
288 |
|
289 NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:msgSize encoding:NSASCIIStringEncoding]; |
|
290 NSMutableArray *listOfCommands = [NSMutableArray arrayWithArray:[bufferedMessage componentsSeparatedByString:@"\n"]]; |
|
291 [listOfCommands removeLastObject]; |
|
292 [listOfCommands removeLastObject]; |
|
293 NSString *command = [listOfCommands objectAtIndex:0]; |
|
294 DLog(@"size = %d, %@", msgSize, listOfCommands); |
|
295 [bufferedMessage release]; |
|
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) |
|
307 message = [[NSString alloc] initWithFormat:@"PONG\n%@\n\n",[listOfCommands objectAtIndex:1]]; |
|
308 else |
|
309 message = [[NSString alloc] initWithString:@"PONG\n\n"]; |
|
310 |
|
311 SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
|
312 [message release]; |
|
313 } |
|
314 else if ([command isEqualToString:@"NICK"]) { |
|
315 //TODO: what is this for? |
|
316 } |
|
317 else if ([command isEqualToString:@"PROTO"]) { |
|
318 if ([[listOfCommands objectAtIndex:1] intValue] == 34) { |
|
319 //TODO: unused |
|
320 } |
|
321 } |
|
322 else if ([command isEqualToString:@"ROOM"]) { |
|
323 //TODO: stub |
|
324 } |
|
325 else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
|
326 //TODO: stub |
|
327 } |
|
328 else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
|
329 //TODO: stub |
|
330 } |
|
331 else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
|
332 DLog(@"%@", [listOfCommands objectAtIndex:1]); |
|
333 } |
|
334 else if ([command isEqualToString:@"WARNING"]) { |
|
335 if ([listOfCommands count] > 1) |
|
336 DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]); |
|
337 else |
|
338 DLog(@"Server warning - unknown"); |
|
339 } |
|
340 else if ([command isEqualToString:@"ERROR"]) { |
|
341 DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]); |
|
342 } |
|
343 else if ([command isEqualToString:@"BYE"]) { |
|
344 //TODO: handle "Reconnected too fast" |
|
345 DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]); |
|
346 clientQuit = YES; |
|
347 } |
|
348 else { |
|
349 DLog(@"Unknown/Unsupported message received: %@", command); |
|
350 } |
|
351 } |
|
352 DLog(@"Server exited, ending thread"); |
|
353 |
|
354 // Close the client socket |
|
355 SDLNet_TCP_Close(sd); |
|
356 SDLNet_Quit(); |
|
357 |
|
358 [pool release]; |
|
359 } |
|
360 |
253 // method that handles net setup with engine and keeps connection alive |
361 // method that handles net setup with engine and keeps connection alive |
254 -(void) engineProtocol { |
362 -(void) engineProtocol { |
255 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
363 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
256 TCPsocket sd; |
364 TCPsocket sd; |
257 IPaddress ip; |
365 IPaddress ip; |