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