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