author | nemo |
Tue, 21 Aug 2012 19:40:02 -0400 | |
changeset 7567 | da2aa8355d91 |
parent 6908 | 896ed2afcfb8 |
child 8835 | 01bcf9ea68c1 |
permissions | -rw-r--r-- |
4547 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 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 |
||
19 |
||
6246 | 20 |
#import "ServerProtocolNetwork.h" |
4547 | 21 |
|
6832 | 22 |
|
4547 | 23 |
#define BUFFER_SIZE 256 |
24 |
||
6246 | 25 |
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
|
26 |
|
6246 | 27 |
@implementation ServerProtocolNetwork |
6320 | 28 |
@synthesize serverPort, serverAddress, ssd; |
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
|
29 |
|
6320 | 30 |
#pragma mark - |
31 |
#pragma mark init and class methods |
|
32 |
-(id) init:(NSInteger) onPort withAddress:(NSString *)address { |
|
6908
896ed2afcfb8
ios: turn on more warning messages and start correcting them
koda
parents:
6832
diff
changeset
|
33 |
if ((self = [super init])) { |
6320 | 34 |
self.serverPort = onPort; |
35 |
self.serverAddress = address; |
|
4547 | 36 |
} |
6246 | 37 |
serverConnection = self; |
4547 | 38 |
return self; |
39 |
} |
|
40 |
||
6320 | 41 |
-(id) init { |
42 |
return [self init:NETGAME_DEFAULT_PORT withAddress:@"netserver.hedgewars.org"]; |
|
43 |
} |
|
44 |
||
45 |
-(id) initOnPort:(NSInteger) port { |
|
46 |
return [self init:port withAddress:@"netserver.hedgewars.org"]; |
|
47 |
} |
|
48 |
||
49 |
-(id) initToAddress:(NSString *)address { |
|
50 |
return [self init:NETGAME_DEFAULT_PORT withAddress:address]; |
|
51 |
} |
|
52 |
||
4547 | 53 |
-(void) dealloc { |
6246 | 54 |
releaseAndNil(serverAddress); |
6320 | 55 |
serverConnection = nil; |
4547 | 56 |
[super dealloc]; |
57 |
} |
|
58 |
||
6337
84e7d1a5e3df
in class methods you can call [self alloc], as per objc specifications
koda
parents:
6320
diff
changeset
|
59 |
+(id) openServerConnection { |
84e7d1a5e3df
in class methods you can call [self alloc], as per objc specifications
koda
parents:
6320
diff
changeset
|
60 |
id connection = [[self alloc] init]; |
6320 | 61 |
[NSThread detachNewThreadSelector:@selector(serverProtocol) |
62 |
toTarget:connection |
|
63 |
withObject:nil]; |
|
64 |
[connection retain]; // retain count here is +2 |
|
65 |
return connection; |
|
66 |
} |
|
67 |
||
68 |
#pragma mark - |
|
69 |
#pragma mark Communication layer |
|
4547 | 70 |
-(int) sendToServer:(NSString *)command { |
71 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command]; |
|
6320 | 72 |
int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message length]); |
4547 | 73 |
[message release]; |
74 |
return result; |
|
75 |
} |
|
76 |
||
77 |
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument { |
|
78 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument]; |
|
6320 | 79 |
int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message length]); |
4547 | 80 |
[message release]; |
81 |
return result; |
|
82 |
} |
|
83 |
||
84 |
-(void) serverProtocol { |
|
85 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
86 |
IPaddress ip; |
|
87 |
BOOL clientQuit = NO; |
|
88 |
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE); |
|
89 |
int dim = BUFFER_SIZE; |
|
90 |
uint8_t msgSize; |
|
6246 | 91 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
4547 | 92 |
|
93 |
if (SDLNet_Init() < 0) { |
|
94 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
95 |
clientQuit = YES; |
|
96 |
} |
|
97 |
||
98 |
// Resolving the host using NULL make network interface to listen |
|
6246 | 99 |
if (SDLNet_ResolveHost(&ip, [self.serverAddress UTF8String] , self.serverPort) < 0 && !clientQuit) { |
100 |
DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError()); |
|
4547 | 101 |
clientQuit = YES; |
102 |
} |
|
103 |
||
104 |
// Open a connection with the IP provided (listen on the host's port) |
|
6320 | 105 |
if (!(self.ssd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
6246 | 106 |
DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort); |
4547 | 107 |
clientQuit = YES; |
108 |
} |
|
109 |
||
6246 | 110 |
DLog(@"Found server on port %d", self.serverPort); |
4547 | 111 |
while (!clientQuit) { |
112 |
int index = 0; |
|
113 |
BOOL exitBufferLoop = NO; |
|
114 |
memset(buffer, '\0', dim); |
|
6246 | 115 |
|
4547 | 116 |
while (exitBufferLoop != YES) { |
6320 | 117 |
msgSize = SDLNet_TCP_Recv(self.ssd, &buffer[index], 2); |
6246 | 118 |
|
4547 | 119 |
// exit in case of error |
120 |
if (msgSize <= 0) { |
|
121 |
DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
|
122 |
clientQuit = YES; |
|
123 |
break; |
|
124 |
} |
|
6246 | 125 |
|
4547 | 126 |
// update index position and check for End-Of-Message |
127 |
index += msgSize; |
|
128 |
if (strncmp(&buffer[index-2], "\n\n", 2) == 0) { |
|
129 |
exitBufferLoop = YES; |
|
130 |
} |
|
6246 | 131 |
|
4547 | 132 |
// if message is too big allocate new space |
133 |
if (index >= dim) { |
|
134 |
dim += BUFFER_SIZE; |
|
135 |
buffer = (char *)realloc(buffer, dim); |
|
136 |
if (buffer == NULL) { |
|
137 |
clientQuit = YES; |
|
138 |
break; |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding]; |
|
144 |
NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"]; |
|
145 |
[bufferedMessage release]; |
|
146 |
NSString *command = [listOfCommands objectAtIndex:0]; |
|
147 |
DLog(@"size = %d, %@", index-2, listOfCommands); |
|
148 |
if ([command isEqualToString:@"PING"]) { |
|
149 |
if ([listOfCommands count] > 1) |
|
150 |
[self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]]; |
|
151 |
else |
|
152 |
[self sendToServer:@"PONG"]; |
|
153 |
DLog(@"PONG"); |
|
154 |
} |
|
155 |
else if ([command isEqualToString:@"NICK"]) { |
|
156 |
//what is this for? |
|
157 |
} |
|
158 |
else if ([command isEqualToString:@"PROTO"]) { |
|
159 |
//what is this for? |
|
160 |
} |
|
161 |
else if ([command isEqualToString:@"ROOM"]) { |
|
162 |
//TODO: stub |
|
163 |
} |
|
164 |
else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
|
165 |
//TODO: stub |
|
166 |
} |
|
167 |
else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
|
168 |
//TODO: stub |
|
169 |
} |
|
170 |
else if ([command isEqualToString:@"ASKPASSWORD"]) { |
|
6246 | 171 |
NSString *pwd = [defaults objectForKey:@"password"]; |
4547 | 172 |
[self sendToServer:@"PASSWORD" withArgument:pwd]; |
173 |
} |
|
174 |
else if ([command isEqualToString:@"CONNECTED"]) { |
|
4603 | 175 |
int netProto; |
4547 | 176 |
char *versionStr; |
177 |
HW_versionInfo(&netProto, &versionStr); |
|
6246 | 178 |
NSString *nick = [defaults objectForKey:@"username"]; |
4547 | 179 |
[self sendToServer:@"NICK" withArgument:nick]; |
180 |
[self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]]; |
|
181 |
} |
|
182 |
else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
|
183 |
DLog(@"%@", [listOfCommands objectAtIndex:1]); |
|
184 |
} |
|
185 |
else if ([command isEqualToString:@"WARNING"]) { |
|
186 |
if ([listOfCommands count] > 1) |
|
187 |
DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]); |
|
188 |
else |
|
189 |
DLog(@"Server warning - unknown"); |
|
190 |
} |
|
191 |
else if ([command isEqualToString:@"ERROR"]) { |
|
192 |
DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]); |
|
193 |
} |
|
194 |
else if ([command isEqualToString:@"BYE"]) { |
|
195 |
//TODO: handle "Reconnected too fast" |
|
196 |
DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]); |
|
197 |
clientQuit = YES; |
|
198 |
} |
|
199 |
else { |
|
200 |
DLog(@"Unknown/Unsupported message received: %@", command); |
|
201 |
} |
|
202 |
} |
|
203 |
DLog(@"Server closed connection, ending thread"); |
|
204 |
||
205 |
free(buffer); |
|
6320 | 206 |
SDLNet_TCP_Close(self.ssd); |
4547 | 207 |
SDLNet_Quit(); |
208 |
||
209 |
[pool release]; |
|
210 |
} |
|
211 |
||
212 |
@end |