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 |
|
|
22 |
#import "ServerSetup.h"
|
|
23 |
#import "PascalImports.h"
|
|
24 |
#import "CommodityFunctions.h"
|
|
25 |
#import <SystemConfiguration/SCNetworkReachability.h>
|
|
26 |
#import <netinet/in.h>
|
5201
|
27 |
#import "hwconsts.h"
|
4547
|
28 |
|
|
29 |
#define BUFFER_SIZE 256
|
|
30 |
|
|
31 |
@implementation ServerSetup
|
|
32 |
@synthesize systemSettings;
|
|
33 |
|
|
34 |
-(id) init {
|
|
35 |
if (self = [super init]) {
|
|
36 |
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
|
|
37 |
self.systemSettings = dict;
|
|
38 |
[dict release];
|
|
39 |
}
|
|
40 |
return self;
|
|
41 |
}
|
|
42 |
|
|
43 |
-(void) dealloc {
|
|
44 |
|
|
45 |
[super dealloc];
|
|
46 |
}
|
|
47 |
|
|
48 |
// reusing appirater method
|
|
49 |
-(BOOL) isNetworkReachable {
|
|
50 |
// Create zero addy
|
|
51 |
struct sockaddr_in zeroAddress;
|
|
52 |
bzero(&zeroAddress, sizeof(zeroAddress));
|
|
53 |
zeroAddress.sin_len = sizeof(zeroAddress);
|
|
54 |
zeroAddress.sin_family = AF_INET;
|
|
55 |
|
|
56 |
// Recover reachability flags
|
|
57 |
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
|
|
58 |
SCNetworkReachabilityFlags flags;
|
|
59 |
|
|
60 |
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
|
|
61 |
CFRelease(defaultRouteReachability);
|
|
62 |
|
|
63 |
if (!didRetrieveFlags) {
|
|
64 |
NSLog(@"Error. Could not recover network reachability flags");
|
|
65 |
return NO;
|
|
66 |
}
|
|
67 |
|
|
68 |
BOOL isReachable = flags & kSCNetworkFlagsReachable;
|
|
69 |
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
|
|
70 |
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
|
|
71 |
|
|
72 |
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
|
|
73 |
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
|
|
74 |
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
|
|
75 |
timeoutInterval:20.0];
|
|
76 |
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
|
|
77 |
|
|
78 |
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
|
|
79 |
}
|
|
80 |
|
|
81 |
-(int) sendToServer:(NSString *)command {
|
|
82 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
|
|
83 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
|
|
84 |
[message release];
|
|
85 |
return result;
|
|
86 |
}
|
|
87 |
|
|
88 |
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
|
|
89 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
|
|
90 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
|
|
91 |
[message release];
|
|
92 |
return result;
|
|
93 |
}
|
|
94 |
|
|
95 |
-(void) serverProtocol {
|
|
96 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
97 |
IPaddress ip;
|
|
98 |
BOOL clientQuit = NO;
|
|
99 |
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
|
|
100 |
int dim = BUFFER_SIZE;
|
|
101 |
uint8_t msgSize;
|
|
102 |
|
|
103 |
if (SDLNet_Init() < 0) {
|
|
104 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError());
|
|
105 |
clientQuit = YES;
|
|
106 |
}
|
|
107 |
|
|
108 |
// Resolving the host using NULL make network interface to listen
|
5201
|
109 |
if (SDLNet_ResolveHost(&ip, "netserver.hedgewars.org", NETGAME_DEFAULT_PORT) < 0 && !clientQuit) {
|
4547
|
110 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
|
|
111 |
clientQuit = YES;
|
|
112 |
}
|
|
113 |
|
|
114 |
// Open a connection with the IP provided (listen on the host's port)
|
|
115 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
|
5201
|
116 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), NETGAME_DEFAULT_PORT);
|
4547
|
117 |
clientQuit = YES;
|
|
118 |
}
|
|
119 |
|
5201
|
120 |
DLog(@"Found server on port %d", NETGAME_DEFAULT_PORT);
|
4547
|
121 |
while (!clientQuit) {
|
|
122 |
int index = 0;
|
|
123 |
BOOL exitBufferLoop = NO;
|
|
124 |
memset(buffer, '\0', dim);
|
|
125 |
|
|
126 |
while (exitBufferLoop != YES) {
|
|
127 |
msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
|
|
128 |
|
|
129 |
// exit in case of error
|
|
130 |
if (msgSize <= 0) {
|
|
131 |
DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
|
|
132 |
clientQuit = YES;
|
|
133 |
break;
|
|
134 |
}
|
|
135 |
|
|
136 |
// update index position and check for End-Of-Message
|
|
137 |
index += msgSize;
|
|
138 |
if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
|
|
139 |
exitBufferLoop = YES;
|
|
140 |
}
|
|
141 |
|
|
142 |
// if message is too big allocate new space
|
|
143 |
if (index >= dim) {
|
|
144 |
dim += BUFFER_SIZE;
|
|
145 |
buffer = (char *)realloc(buffer, dim);
|
|
146 |
if (buffer == NULL) {
|
|
147 |
clientQuit = YES;
|
|
148 |
break;
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
|
|
154 |
NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
|
|
155 |
[bufferedMessage release];
|
|
156 |
NSString *command = [listOfCommands objectAtIndex:0];
|
|
157 |
DLog(@"size = %d, %@", index-2, listOfCommands);
|
|
158 |
if ([command isEqualToString:@"PING"]) {
|
|
159 |
if ([listOfCommands count] > 1)
|
|
160 |
[self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
|
|
161 |
else
|
|
162 |
[self sendToServer:@"PONG"];
|
|
163 |
DLog(@"PONG");
|
|
164 |
}
|
|
165 |
else if ([command isEqualToString:@"NICK"]) {
|
|
166 |
//what is this for?
|
|
167 |
}
|
|
168 |
else if ([command isEqualToString:@"PROTO"]) {
|
|
169 |
//what is this for?
|
|
170 |
}
|
|
171 |
else if ([command isEqualToString:@"ROOM"]) {
|
|
172 |
//TODO: stub
|
|
173 |
}
|
|
174 |
else if ([command isEqualToString:@"LOBBY:LEFT"]) {
|
|
175 |
//TODO: stub
|
|
176 |
}
|
|
177 |
else if ([command isEqualToString:@"LOBBY:JOINED"]) {
|
|
178 |
//TODO: stub
|
|
179 |
}
|
|
180 |
else if ([command isEqualToString:@"ASKPASSWORD"]) {
|
|
181 |
NSString *pwd = [self.systemSettings objectForKey:@"password"];
|
|
182 |
[self sendToServer:@"PASSWORD" withArgument:pwd];
|
|
183 |
}
|
|
184 |
else if ([command isEqualToString:@"CONNECTED"]) {
|
4603
|
185 |
int netProto;
|
4547
|
186 |
char *versionStr;
|
|
187 |
HW_versionInfo(&netProto, &versionStr);
|
|
188 |
NSString *nick = [self.systemSettings objectForKey:@"username"];
|
|
189 |
[self sendToServer:@"NICK" withArgument:nick];
|
|
190 |
[self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
|
|
191 |
}
|
|
192 |
else if ([command isEqualToString:@"SERVER_MESSAGE"]) {
|
|
193 |
DLog(@"%@", [listOfCommands objectAtIndex:1]);
|
|
194 |
}
|
|
195 |
else if ([command isEqualToString:@"WARNING"]) {
|
|
196 |
if ([listOfCommands count] > 1)
|
|
197 |
DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
|
|
198 |
else
|
|
199 |
DLog(@"Server warning - unknown");
|
|
200 |
}
|
|
201 |
else if ([command isEqualToString:@"ERROR"]) {
|
|
202 |
DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
|
|
203 |
}
|
|
204 |
else if ([command isEqualToString:@"BYE"]) {
|
|
205 |
//TODO: handle "Reconnected too fast"
|
|
206 |
DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
|
|
207 |
clientQuit = YES;
|
|
208 |
}
|
|
209 |
else {
|
|
210 |
DLog(@"Unknown/Unsupported message received: %@", command);
|
|
211 |
}
|
|
212 |
}
|
|
213 |
DLog(@"Server closed connection, ending thread");
|
|
214 |
|
|
215 |
free(buffer);
|
|
216 |
SDLNet_TCP_Close(sd);
|
|
217 |
SDLNet_Quit();
|
|
218 |
|
|
219 |
[pool release];
|
|
220 |
}
|
|
221 |
|
|
222 |
@end
|