author | koda |
Sun, 06 Nov 2011 17:43:57 +0100 | |
changeset 6301 | 1259736c0134 |
parent 6246 | 6b2d19ed521a |
child 6320 | 238a6dc0e7ad |
permissions | -rw-r--r-- |
4547 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
4547 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 10/01/2010. |
|
19 |
*/ |
|
20 |
||
21 |
||
6246 | 22 |
#import "ServerProtocolNetwork.h" |
23 |
#import "SDL_net.h" |
|
5201 | 24 |
#import "hwconsts.h" |
4547 | 25 |
|
26 |
#define BUFFER_SIZE 256 |
|
27 |
||
6246 | 28 |
static TCPsocket sd; |
29 |
static ServerProtocolNetwork *serverConnection; |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5483
diff
changeset
|
30 |
|
6246 | 31 |
@implementation ServerProtocolNetwork |
32 |
@synthesize serverPort, serverAddress; |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5483
diff
changeset
|
33 |
|
4547 | 34 |
-(id) init { |
35 |
if (self = [super init]) { |
|
6246 | 36 |
self.serverPort = NETGAME_DEFAULT_PORT; |
37 |
self.serverAddress = @"netserver.hedgewars.org"; |
|
4547 | 38 |
} |
6246 | 39 |
serverConnection = self; |
4547 | 40 |
return self; |
41 |
} |
|
42 |
||
43 |
-(void) dealloc { |
|
6246 | 44 |
releaseAndNil(serverAddress); |
4547 | 45 |
[super dealloc]; |
46 |
} |
|
47 |
||
48 |
-(int) sendToServer:(NSString *)command { |
|
49 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command]; |
|
50 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
|
51 |
[message release]; |
|
52 |
return result; |
|
53 |
} |
|
54 |
||
55 |
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument { |
|
56 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument]; |
|
57 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]); |
|
58 |
[message release]; |
|
59 |
return result; |
|
60 |
} |
|
61 |
||
62 |
-(void) serverProtocol { |
|
63 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
64 |
IPaddress ip; |
|
65 |
BOOL clientQuit = NO; |
|
66 |
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE); |
|
67 |
int dim = BUFFER_SIZE; |
|
68 |
uint8_t msgSize; |
|
6246 | 69 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
4547 | 70 |
|
71 |
if (SDLNet_Init() < 0) { |
|
72 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
73 |
clientQuit = YES; |
|
74 |
} |
|
75 |
||
76 |
// Resolving the host using NULL make network interface to listen |
|
6246 | 77 |
if (SDLNet_ResolveHost(&ip, [self.serverAddress UTF8String] , self.serverPort) < 0 && !clientQuit) { |
78 |
DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError()); |
|
4547 | 79 |
clientQuit = YES; |
80 |
} |
|
81 |
||
82 |
// Open a connection with the IP provided (listen on the host's port) |
|
83 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
|
6246 | 84 |
DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort); |
4547 | 85 |
clientQuit = YES; |
86 |
} |
|
87 |
||
6246 | 88 |
DLog(@"Found server on port %d", self.serverPort); |
4547 | 89 |
while (!clientQuit) { |
90 |
int index = 0; |
|
91 |
BOOL exitBufferLoop = NO; |
|
92 |
memset(buffer, '\0', dim); |
|
6246 | 93 |
|
4547 | 94 |
while (exitBufferLoop != YES) { |
95 |
msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2); |
|
6246 | 96 |
|
4547 | 97 |
// exit in case of error |
98 |
if (msgSize <= 0) { |
|
99 |
DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
|
100 |
clientQuit = YES; |
|
101 |
break; |
|
102 |
} |
|
6246 | 103 |
|
4547 | 104 |
// update index position and check for End-Of-Message |
105 |
index += msgSize; |
|
106 |
if (strncmp(&buffer[index-2], "\n\n", 2) == 0) { |
|
107 |
exitBufferLoop = YES; |
|
108 |
} |
|
6246 | 109 |
|
4547 | 110 |
// if message is too big allocate new space |
111 |
if (index >= dim) { |
|
112 |
dim += BUFFER_SIZE; |
|
113 |
buffer = (char *)realloc(buffer, dim); |
|
114 |
if (buffer == NULL) { |
|
115 |
clientQuit = YES; |
|
116 |
break; |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding]; |
|
122 |
NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"]; |
|
123 |
[bufferedMessage release]; |
|
124 |
NSString *command = [listOfCommands objectAtIndex:0]; |
|
125 |
DLog(@"size = %d, %@", index-2, listOfCommands); |
|
126 |
if ([command isEqualToString:@"PING"]) { |
|
127 |
if ([listOfCommands count] > 1) |
|
128 |
[self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]]; |
|
129 |
else |
|
130 |
[self sendToServer:@"PONG"]; |
|
131 |
DLog(@"PONG"); |
|
132 |
} |
|
133 |
else if ([command isEqualToString:@"NICK"]) { |
|
134 |
//what is this for? |
|
135 |
} |
|
136 |
else if ([command isEqualToString:@"PROTO"]) { |
|
137 |
//what is this for? |
|
138 |
} |
|
139 |
else if ([command isEqualToString:@"ROOM"]) { |
|
140 |
//TODO: stub |
|
141 |
} |
|
142 |
else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
|
143 |
//TODO: stub |
|
144 |
} |
|
145 |
else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
|
146 |
//TODO: stub |
|
147 |
} |
|
148 |
else if ([command isEqualToString:@"ASKPASSWORD"]) { |
|
6246 | 149 |
NSString *pwd = [defaults objectForKey:@"password"]; |
4547 | 150 |
[self sendToServer:@"PASSWORD" withArgument:pwd]; |
151 |
} |
|
152 |
else if ([command isEqualToString:@"CONNECTED"]) { |
|
4603 | 153 |
int netProto; |
4547 | 154 |
char *versionStr; |
155 |
HW_versionInfo(&netProto, &versionStr); |
|
6246 | 156 |
NSString *nick = [defaults objectForKey:@"username"]; |
4547 | 157 |
[self sendToServer:@"NICK" withArgument:nick]; |
158 |
[self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]]; |
|
159 |
} |
|
160 |
else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
|
161 |
DLog(@"%@", [listOfCommands objectAtIndex:1]); |
|
162 |
} |
|
163 |
else if ([command isEqualToString:@"WARNING"]) { |
|
164 |
if ([listOfCommands count] > 1) |
|
165 |
DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]); |
|
166 |
else |
|
167 |
DLog(@"Server warning - unknown"); |
|
168 |
} |
|
169 |
else if ([command isEqualToString:@"ERROR"]) { |
|
170 |
DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]); |
|
171 |
} |
|
172 |
else if ([command isEqualToString:@"BYE"]) { |
|
173 |
//TODO: handle "Reconnected too fast" |
|
174 |
DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]); |
|
175 |
clientQuit = YES; |
|
176 |
} |
|
177 |
else { |
|
178 |
DLog(@"Unknown/Unsupported message received: %@", command); |
|
179 |
} |
|
180 |
} |
|
181 |
DLog(@"Server closed connection, ending thread"); |
|
182 |
||
183 |
free(buffer); |
|
184 |
SDLNet_TCP_Close(sd); |
|
185 |
SDLNet_Quit(); |
|
186 |
||
187 |
[pool release]; |
|
188 |
} |
|
189 |
||
190 |
@end |