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"
|
5201
|
25 |
#import "hwconsts.h"
|
4547
|
26 |
|
|
27 |
#define BUFFER_SIZE 256
|
|
28 |
|
|
29 |
@implementation ServerSetup
|
|
30 |
@synthesize systemSettings;
|
|
31 |
|
|
32 |
-(id) init {
|
|
33 |
if (self = [super init]) {
|
5206
|
34 |
self.systemSettings = nil; //nsuserdefault
|
4547
|
35 |
}
|
|
36 |
return self;
|
|
37 |
}
|
|
38 |
|
|
39 |
-(void) dealloc {
|
|
40 |
|
|
41 |
[super dealloc];
|
|
42 |
}
|
|
43 |
|
|
44 |
-(int) sendToServer:(NSString *)command {
|
|
45 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
|
|
46 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
|
|
47 |
[message release];
|
|
48 |
return result;
|
|
49 |
}
|
|
50 |
|
|
51 |
-(int) sendToServer:(NSString *)command withArgument:(NSString *)argument {
|
|
52 |
NSString *message = [[NSString alloc] initWithFormat:@"%@\n%@\n\n",command,argument];
|
|
53 |
int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);
|
|
54 |
[message release];
|
|
55 |
return result;
|
|
56 |
}
|
|
57 |
|
|
58 |
-(void) serverProtocol {
|
|
59 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
60 |
IPaddress ip;
|
|
61 |
BOOL clientQuit = NO;
|
|
62 |
char *buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
|
|
63 |
int dim = BUFFER_SIZE;
|
|
64 |
uint8_t msgSize;
|
|
65 |
|
|
66 |
if (SDLNet_Init() < 0) {
|
|
67 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError());
|
|
68 |
clientQuit = YES;
|
|
69 |
}
|
|
70 |
|
|
71 |
// Resolving the host using NULL make network interface to listen
|
5201
|
72 |
if (SDLNet_ResolveHost(&ip, "netserver.hedgewars.org", NETGAME_DEFAULT_PORT) < 0 && !clientQuit) {
|
4547
|
73 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
|
|
74 |
clientQuit = YES;
|
|
75 |
}
|
|
76 |
|
|
77 |
// Open a connection with the IP provided (listen on the host's port)
|
|
78 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
|
5201
|
79 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), NETGAME_DEFAULT_PORT);
|
4547
|
80 |
clientQuit = YES;
|
|
81 |
}
|
|
82 |
|
5201
|
83 |
DLog(@"Found server on port %d", NETGAME_DEFAULT_PORT);
|
4547
|
84 |
while (!clientQuit) {
|
|
85 |
int index = 0;
|
|
86 |
BOOL exitBufferLoop = NO;
|
|
87 |
memset(buffer, '\0', dim);
|
|
88 |
|
|
89 |
while (exitBufferLoop != YES) {
|
|
90 |
msgSize = SDLNet_TCP_Recv(sd, &buffer[index], 2);
|
|
91 |
|
|
92 |
// exit in case of error
|
|
93 |
if (msgSize <= 0) {
|
|
94 |
DLog(@"SDLNet_TCP_Recv: %s", SDLNet_GetError());
|
|
95 |
clientQuit = YES;
|
|
96 |
break;
|
|
97 |
}
|
|
98 |
|
|
99 |
// update index position and check for End-Of-Message
|
|
100 |
index += msgSize;
|
|
101 |
if (strncmp(&buffer[index-2], "\n\n", 2) == 0) {
|
|
102 |
exitBufferLoop = YES;
|
|
103 |
}
|
|
104 |
|
|
105 |
// if message is too big allocate new space
|
|
106 |
if (index >= dim) {
|
|
107 |
dim += BUFFER_SIZE;
|
|
108 |
buffer = (char *)realloc(buffer, dim);
|
|
109 |
if (buffer == NULL) {
|
|
110 |
clientQuit = YES;
|
|
111 |
break;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
NSString *bufferedMessage = [[NSString alloc] initWithBytes:buffer length:index-2 encoding:NSASCIIStringEncoding];
|
|
117 |
NSArray *listOfCommands = [bufferedMessage componentsSeparatedByString:@"\n"];
|
|
118 |
[bufferedMessage release];
|
|
119 |
NSString *command = [listOfCommands objectAtIndex:0];
|
|
120 |
DLog(@"size = %d, %@", index-2, listOfCommands);
|
|
121 |
if ([command isEqualToString:@"PING"]) {
|
|
122 |
if ([listOfCommands count] > 1)
|
|
123 |
[self sendToServer:@"PONG" withArgument:[listOfCommands objectAtIndex:1]];
|
|
124 |
else
|
|
125 |
[self sendToServer:@"PONG"];
|
|
126 |
DLog(@"PONG");
|
|
127 |
}
|
|
128 |
else if ([command isEqualToString:@"NICK"]) {
|
|
129 |
//what is this for?
|
|
130 |
}
|
|
131 |
else if ([command isEqualToString:@"PROTO"]) {
|
|
132 |
//what is this for?
|
|
133 |
}
|
|
134 |
else if ([command isEqualToString:@"ROOM"]) {
|
|
135 |
//TODO: stub
|
|
136 |
}
|
|
137 |
else if ([command isEqualToString:@"LOBBY:LEFT"]) {
|
|
138 |
//TODO: stub
|
|
139 |
}
|
|
140 |
else if ([command isEqualToString:@"LOBBY:JOINED"]) {
|
|
141 |
//TODO: stub
|
|
142 |
}
|
|
143 |
else if ([command isEqualToString:@"ASKPASSWORD"]) {
|
|
144 |
NSString *pwd = [self.systemSettings objectForKey:@"password"];
|
|
145 |
[self sendToServer:@"PASSWORD" withArgument:pwd];
|
|
146 |
}
|
|
147 |
else if ([command isEqualToString:@"CONNECTED"]) {
|
4603
|
148 |
int netProto;
|
4547
|
149 |
char *versionStr;
|
|
150 |
HW_versionInfo(&netProto, &versionStr);
|
|
151 |
NSString *nick = [self.systemSettings objectForKey:@"username"];
|
|
152 |
[self sendToServer:@"NICK" withArgument:nick];
|
|
153 |
[self sendToServer:@"PROTO" withArgument:[NSString stringWithFormat:@"%d",netProto]];
|
|
154 |
}
|
|
155 |
else if ([command isEqualToString:@"SERVER_MESSAGE"]) {
|
|
156 |
DLog(@"%@", [listOfCommands objectAtIndex:1]);
|
|
157 |
}
|
|
158 |
else if ([command isEqualToString:@"WARNING"]) {
|
|
159 |
if ([listOfCommands count] > 1)
|
|
160 |
DLog(@"Server warning - %@", [listOfCommands objectAtIndex:1]);
|
|
161 |
else
|
|
162 |
DLog(@"Server warning - unknown");
|
|
163 |
}
|
|
164 |
else if ([command isEqualToString:@"ERROR"]) {
|
|
165 |
DLog(@"Server error - %@", [listOfCommands objectAtIndex:1]);
|
|
166 |
}
|
|
167 |
else if ([command isEqualToString:@"BYE"]) {
|
|
168 |
//TODO: handle "Reconnected too fast"
|
|
169 |
DLog(@"Server disconnected, reason: %@", [listOfCommands objectAtIndex:1]);
|
|
170 |
clientQuit = YES;
|
|
171 |
}
|
|
172 |
else {
|
|
173 |
DLog(@"Unknown/Unsupported message received: %@", command);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
DLog(@"Server closed connection, ending thread");
|
|
177 |
|
|
178 |
free(buffer);
|
|
179 |
SDLNet_TCP_Close(sd);
|
|
180 |
SDLNet_Quit();
|
|
181 |
|
|
182 |
[pool release];
|
|
183 |
}
|
|
184 |
|
|
185 |
@end
|