author | Wuzzy <almikes@aol.com> |
Tue, 12 Apr 2016 23:40:24 +0200 | |
changeset 11718 | 106112bbacfa |
parent 11244 | 57891137ffef |
child 12877 | 00215a7ec5f5 |
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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
8835
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
4547 | 17 |
*/ |
18 |
||
19 |
||
6246 | 20 |
#import "ServerProtocolNetwork.h" |
8835 | 21 |
#import "hwconsts.h" |
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]; |
|
11244
57891137ffef
- Fix for sending to engine non-english characters
antonc27 <antonc27@mail.ru>
parents:
11137
diff
changeset
|
72 |
int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); |
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]; |
|
11244
57891137ffef
- Fix for sending to engine non-english characters
antonc27 <antonc27@mail.ru>
parents:
11137
diff
changeset
|
79 |
int result = SDLNet_TCP_Send(self.ssd, [message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); |
4547 | 80 |
[message release]; |
81 |
return result; |
|
82 |
} |
|
83 |
||
84 |
-(void) serverProtocol { |
|
11137
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
10108
diff
changeset
|
85 |
@autoreleasepool { |
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
10108
diff
changeset
|
86 |
|
4547 | 87 |
IPaddress ip; |
88 |
BOOL clientQuit = NO; |
|
89 |
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE); |
|
90 |
int dim = BUFFER_SIZE; |
|
91 |
uint8_t msgSize; |
|
6246 | 92 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
4547 | 93 |
|
94 |
if (SDLNet_Init() < 0) { |
|
95 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
96 |
clientQuit = YES; |
|
97 |
} |
|
98 |
||
99 |
// Resolving the host using NULL make network interface to listen |
|
6246 | 100 |
if (SDLNet_ResolveHost(&ip, [self.serverAddress UTF8String] , self.serverPort) < 0 && !clientQuit) { |
101 |
DLog(@"SDLNet_ResolveHost: %s", SDLNet_GetError()); |
|
4547 | 102 |
clientQuit = YES; |
103 |
} |
|
104 |
||
105 |
// Open a connection with the IP provided (listen on the host's port) |
|
6320 | 106 |
if (!(self.ssd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
6246 | 107 |
DLog(@"SDLNet_TCP_Open: %s %d", SDLNet_GetError(), self.serverPort); |
4547 | 108 |
clientQuit = YES; |
109 |
} |
|
110 |
||
6246 | 111 |
DLog(@"Found server on port %d", self.serverPort); |
4547 | 112 |
while (!clientQuit) { |
113 |
int index = 0; |
|
114 |
BOOL exitBufferLoop = NO; |
|
115 |
memset(buffer, '\0', dim); |
|
6246 | 116 |
|
4547 | 117 |
while (exitBufferLoop != YES) { |
6320 | 118 |
msgSize = SDLNet_TCP_Recv(self.ssd, &buffer[index], 2); |
6246 | 119 |
|
4547 | 120 |
// exit in case of error |
121 |
if (msgSize <= 0) { |
|
122 |
DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError()); |
|
123 |
clientQuit = YES; |
|
124 |
break; |
|
125 |
} |
|
6246 | 126 |
|
4547 | 127 |
// update index position and check for End-Of-Message |
128 |
index += msgSize; |
|
129 |
if (strncmp(&buffer[index-2], "\n\n", 2) == 0) { |
|
130 |
exitBufferLoop = YES; |
|
131 |
} |
|
6246 | 132 |
|
4547 | 133 |
// if message is too big allocate new space |
134 |
if (index >= dim) { |
|
135 |
dim += BUFFER_SIZE; |
|
136 |
buffer = (char *)realloc(buffer, dim); |
|
137 |
if (buffer == NULL) { |
|
138 |
clientQuit = YES; |
|
139 |
break; |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding]; |
|
145 |
NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"]; |
|
146 |
[bufferedMessage release]; |
|
147 |
NSString *command = [listOfCommands objectAtIndex:0]; |
|
148 |
DLog(@"size = %d, %@", index-2, listOfCommands); |
|
149 |
if ([command isEqualToString:@"PING"]) { |
|
150 |
if ([listOfCommands count] > 1) |
|
151 |
[self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]]; |
|
152 |
else |
|
153 |
[self sendToServer:@"PONG"]; |
|
154 |
DLog(@"PONG"); |
|
155 |
} |
|
156 |
else if ([command isEqualToString:@"NICK"]) { |
|
157 |
//what is this for? |
|
158 |
} |
|
159 |
else if ([command isEqualToString:@"PROTO"]) { |
|
160 |
//what is this for? |
|
161 |
} |
|
162 |
else if ([command isEqualToString:@"ROOM"]) { |
|
163 |
//TODO: stub |
|
164 |
} |
|
165 |
else if ([command isEqualToString:@"LOBBY:LEFT"]) { |
|
166 |
//TODO: stub |
|
167 |
} |
|
168 |
else if ([command isEqualToString:@"LOBBY:JOINED"]) { |
|
169 |
//TODO: stub |
|
170 |
} |
|
171 |
else if ([command isEqualToString:@"ASKPASSWORD"]) { |
|
6246 | 172 |
NSString *pwd = [defaults objectForKey:@"password"]; |
4547 | 173 |
[self sendToServer:@"PASSWORD" withArgument:pwd]; |
174 |
} |
|
175 |
else if ([command isEqualToString:@"CONNECTED"]) { |
|
4603 | 176 |
int netProto; |
4547 | 177 |
char *versionStr; |
178 |
HW_versionInfo(&netProto, &versionStr); |
|
6246 | 179 |
NSString *nick = [defaults objectForKey:@"username"]; |
4547 | 180 |
[self sendToServer:@"NICK" withArgument:nick]; |
181 |
[self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]]; |
|
182 |
} |
|
183 |
else if ([command isEqualToString:@"SERVER_MESSAGE"]) { |
|
184 |
DLog(@"%@", [listOfCommands objectAtIndex:1]); |
|
185 |
} |
|
186 |
else if ([command isEqualToString:@"WARNING"]) { |
|
187 |
if ([listOfCommands count] > 1) |
|
188 |
DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]); |
|
189 |
else |
|
190 |
DLog(@"Server warning - unknown"); |
|
191 |
} |
|
192 |
else if ([command isEqualToString:@"ERROR"]) { |
|
193 |
DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]); |
|
194 |
} |
|
195 |
else if ([command isEqualToString:@"BYE"]) { |
|
196 |
//TODO: handle "Reconnected too fast" |
|
197 |
DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]); |
|
198 |
clientQuit = YES; |
|
199 |
} |
|
200 |
else { |
|
201 |
DLog(@"Unknown/Unsupported message received: %@", command); |
|
202 |
} |
|
203 |
} |
|
204 |
DLog(@"Server closed connection, ending thread"); |
|
205 |
||
206 |
free(buffer); |
|
6320 | 207 |
SDLNet_TCP_Close(self.ssd); |
4547 | 208 |
SDLNet_Quit(); |
209 |
||
11137
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
10108
diff
changeset
|
210 |
} |
4547 | 211 |
} |
212 |
||
213 |
@end |