author | celestar |
Thu, 11 Jan 2007 13:16:26 +0000 | |
branch | custombridgeheads |
changeset 5648 | 1608018c5ff2 |
parent 5643 | 3778051e8095 |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifdef ENABLE_NETWORK |
|
4 |
||
5 |
#include "../stdafx.h" |
|
6 |
#include "../debug.h" |
|
7 |
#include "../string.h" |
|
8 |
#include "network_data.h" |
|
9 |
#include "../date.h" |
|
10 |
#include "../map.h" |
|
11 |
#include "network_gamelist.h" |
|
12 |
#include "network_udp.h" |
|
13 |
#include "../variables.h" |
|
14 |
#include "../newgrf_config.h" |
|
15 |
||
16 |
#include "core/udp.h" |
|
17 |
||
18 |
/** |
|
19 |
* @file network_udp.c This file handles the UDP related communication. |
|
20 |
* |
|
21 |
* This is the GameServer <-> MasterServer and GameServer <-> GameClient |
|
22 |
* communication before the game is being joined. |
|
23 |
*/ |
|
24 |
||
25 |
enum { |
|
26 |
ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes) |
|
27 |
ADVERTISE_RETRY_INTERVAL = 300, // readvertise when no response after this many ticks (9 seconds) |
|
28 |
ADVERTISE_RETRY_TIMES = 3 // give up readvertising after this much failed retries |
|
29 |
}; |
|
30 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
31 |
#define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr) |
5642 | 32 |
|
33 |
static NetworkClientState _udp_cs; |
|
34 |
||
35 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER) |
|
36 |
{ |
|
37 |
Packet *packet; |
|
38 |
// Just a fail-safe.. should never happen |
|
39 |
if (!_network_udp_server) |
|
40 |
return; |
|
41 |
||
42 |
packet = NetworkSend_Init(PACKET_UDP_SERVER_RESPONSE); |
|
43 |
||
44 |
// Update some game_info |
|
45 |
_network_game_info.game_date = _date; |
|
46 |
_network_game_info.map_width = MapSizeX(); |
|
47 |
_network_game_info.map_height = MapSizeY(); |
|
48 |
_network_game_info.map_set = _opt.landscape; |
|
49 |
_network_game_info.companies_on = ActivePlayerCount(); |
|
50 |
_network_game_info.spectators_on = NetworkSpectatorCount(); |
|
51 |
_network_game_info.grfconfig = _grfconfig; |
|
52 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
53 |
NetworkSend_NetworkGameInfo(packet, &_network_game_info); |
5642 | 54 |
|
55 |
// Let the client know that we are here |
|
56 |
NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); |
|
57 |
||
58 |
free(packet); |
|
59 |
||
60 |
DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr)); |
|
61 |
} |
|
62 |
||
63 |
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) |
|
64 |
{ |
|
65 |
/* Find the matching GRF file */ |
|
66 |
const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum); |
|
67 |
if (f == NULL) { |
|
68 |
/* Don't know the GRF, so mark game incompatible and the (possibly) |
|
69 |
* already resolved name for this GRF (another server has sent the |
|
70 |
* name of the GRF already */ |
|
71 |
config->name = FindUnknownGRFName(config->grfid, config->md5sum, true); |
|
72 |
SETBIT(config->flags, GCF_NOT_FOUND); |
|
73 |
} else { |
|
74 |
config->filename = f->filename; |
|
75 |
config->name = f->name; |
|
76 |
config->info = f->info; |
|
77 |
} |
|
78 |
SETBIT(config->flags, GCF_COPY); |
|
79 |
} |
|
80 |
||
81 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE) |
|
82 |
{ |
|
83 |
extern const char _openttd_revision[]; |
|
84 |
NetworkGameList *item; |
|
85 |
||
86 |
// Just a fail-safe.. should never happen |
|
87 |
if (_network_udp_server || _udp_cs.has_quit) return; |
|
88 |
||
89 |
DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); |
|
90 |
||
91 |
// Find next item |
|
92 |
item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port)); |
|
93 |
||
94 |
NetworkRecv_NetworkGameInfo(&_udp_cs, p, &item->info); |
|
95 |
||
96 |
item->info.compatible = true; |
|
97 |
{ |
|
98 |
/* Checks whether there needs to be a request for names of GRFs and makes |
|
99 |
* the request if necessary. GRFs that need to be requested are the GRFs |
|
100 |
* that do not exist on the clients system and we do not have the name |
|
101 |
* resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER. |
|
102 |
* The in_request array and in_request_count are used so there is no need |
|
103 |
* to do a second loop over the GRF list, which can be relatively expensive |
|
104 |
* due to the string comparisons. */ |
|
105 |
const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT]; |
|
106 |
const GRFConfig *c; |
|
107 |
uint in_request_count = 0; |
|
108 |
struct sockaddr_in out_addr; |
|
109 |
||
110 |
for (c = item->info.grfconfig; c != NULL; c = c->next) { |
|
111 |
if (HASBIT(c->flags, GCF_NOT_FOUND)) item->info.compatible = false; |
|
112 |
if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue; |
|
113 |
in_request[in_request_count] = c; |
|
114 |
in_request_count++; |
|
115 |
} |
|
116 |
||
117 |
if (in_request_count > 0) { |
|
118 |
/* There are 'unknown' GRFs, now send a request for them */ |
|
119 |
uint i; |
|
120 |
Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS); |
|
121 |
||
122 |
NetworkSend_uint8 (packet, in_request_count); |
|
123 |
for (i = 0; i < in_request_count; i++) { |
|
124 |
NetworkSend_GRFIdentifier(packet, in_request[i]); |
|
125 |
} |
|
126 |
||
127 |
out_addr.sin_family = AF_INET; |
|
128 |
out_addr.sin_port = htons(item->port); |
|
129 |
out_addr.sin_addr.s_addr = item->ip; |
|
130 |
NetworkSendUDP_Packet(_udp_client_socket, packet, &out_addr); |
|
131 |
free(packet); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0; |
|
136 |
if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0; |
|
137 |
||
138 |
if (item->info.hostname[0] == '\0') |
|
139 |
snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr)); |
|
140 |
||
141 |
/* Check if we are allowed on this server based on the revision-match */ |
|
142 |
item->info.version_compatible = |
|
143 |
strcmp(item->info.server_revision, _openttd_revision) == 0 || |
|
144 |
strcmp(item->info.server_revision, NOREV_STRING) == 0; |
|
145 |
item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs |
|
146 |
||
147 |
item->online = true; |
|
148 |
||
149 |
UpdateNetworkGameWindow(false); |
|
150 |
} |
|
151 |
||
152 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO) |
|
153 |
{ |
|
154 |
NetworkClientState *cs; |
|
155 |
NetworkClientInfo *ci; |
|
156 |
Packet *packet; |
|
157 |
Player *player; |
|
158 |
byte current = 0; |
|
159 |
int i; |
|
160 |
||
161 |
// Just a fail-safe.. should never happen |
|
162 |
if (!_network_udp_server) return; |
|
163 |
||
164 |
packet = NetworkSend_Init(PACKET_UDP_SERVER_DETAIL_INFO); |
|
165 |
||
166 |
/* Send the amount of active companies */ |
|
167 |
NetworkSend_uint8 (packet, NETWORK_COMPANY_INFO_VERSION); |
|
168 |
NetworkSend_uint8 (packet, ActivePlayerCount()); |
|
169 |
||
170 |
/* Fetch the latest version of everything */ |
|
171 |
NetworkPopulateCompanyInfo(); |
|
172 |
||
173 |
/* Go through all the players */ |
|
174 |
FOR_ALL_PLAYERS(player) { |
|
175 |
/* Skip non-active players */ |
|
176 |
if (!player->is_active) continue; |
|
177 |
||
178 |
current++; |
|
179 |
||
180 |
/* Send the information */ |
|
181 |
NetworkSend_uint8(packet, current); |
|
182 |
||
183 |
NetworkSend_string(packet, _network_player_info[player->index].company_name); |
|
184 |
NetworkSend_uint32(packet, _network_player_info[player->index].inaugurated_year); |
|
185 |
NetworkSend_uint64(packet, _network_player_info[player->index].company_value); |
|
186 |
NetworkSend_uint64(packet, _network_player_info[player->index].money); |
|
187 |
NetworkSend_uint64(packet, _network_player_info[player->index].income); |
|
188 |
NetworkSend_uint16(packet, _network_player_info[player->index].performance); |
|
189 |
||
190 |
/* Send 1 if there is a passord for the company else send 0 */ |
|
191 |
if (_network_player_info[player->index].password[0] != '\0') { |
|
192 |
NetworkSend_uint8(packet, 1); |
|
193 |
} else { |
|
194 |
NetworkSend_uint8(packet, 0); |
|
195 |
} |
|
196 |
||
197 |
for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) |
|
198 |
NetworkSend_uint16(packet, _network_player_info[player->index].num_vehicle[i]); |
|
199 |
||
200 |
for (i = 0; i < NETWORK_STATION_TYPES; i++) |
|
201 |
NetworkSend_uint16(packet, _network_player_info[player->index].num_station[i]); |
|
202 |
||
203 |
/* Find the clients that are connected to this player */ |
|
204 |
FOR_ALL_CLIENTS(cs) { |
|
205 |
ci = DEREF_CLIENT_INFO(cs); |
|
206 |
if (ci->client_playas == player->index) { |
|
207 |
/* The uint8 == 1 indicates that a client is following */ |
|
208 |
NetworkSend_uint8(packet, 1); |
|
209 |
NetworkSend_string(packet, ci->client_name); |
|
210 |
NetworkSend_string(packet, ci->unique_id); |
|
211 |
NetworkSend_uint32(packet, ci->join_date); |
|
212 |
} |
|
213 |
} |
|
214 |
/* Also check for the server itself */ |
|
215 |
ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); |
|
216 |
if (ci->client_playas == player->index) { |
|
217 |
/* The uint8 == 1 indicates that a client is following */ |
|
218 |
NetworkSend_uint8(packet, 1); |
|
219 |
NetworkSend_string(packet, ci->client_name); |
|
220 |
NetworkSend_string(packet, ci->unique_id); |
|
221 |
NetworkSend_uint32(packet, ci->join_date); |
|
222 |
} |
|
223 |
||
224 |
/* Indicates end of client list */ |
|
225 |
NetworkSend_uint8(packet, 0); |
|
226 |
} |
|
227 |
||
228 |
/* And check if we have any spectators */ |
|
229 |
FOR_ALL_CLIENTS(cs) { |
|
230 |
ci = DEREF_CLIENT_INFO(cs); |
|
231 |
if (!IsValidPlayer(ci->client_playas)) { |
|
232 |
/* The uint8 == 1 indicates that a client is following */ |
|
233 |
NetworkSend_uint8(packet, 1); |
|
234 |
NetworkSend_string(packet, ci->client_name); |
|
235 |
NetworkSend_string(packet, ci->unique_id); |
|
236 |
NetworkSend_uint32(packet, ci->join_date); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/* Also check for the server itself */ |
|
241 |
ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); |
|
242 |
if (!IsValidPlayer(ci->client_playas)) { |
|
243 |
/* The uint8 == 1 indicates that a client is following */ |
|
244 |
NetworkSend_uint8(packet, 1); |
|
245 |
NetworkSend_string(packet, ci->client_name); |
|
246 |
NetworkSend_string(packet, ci->unique_id); |
|
247 |
NetworkSend_uint32(packet, ci->join_date); |
|
248 |
} |
|
249 |
||
250 |
/* Indicates end of client list */ |
|
251 |
NetworkSend_uint8(packet, 0); |
|
252 |
||
253 |
NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); |
|
254 |
||
255 |
free(packet); |
|
256 |
} |
|
257 |
||
258 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST) |
|
259 |
{ |
|
260 |
int i; |
|
261 |
struct in_addr ip; |
|
262 |
uint16 port; |
|
263 |
uint8 ver; |
|
264 |
||
265 |
/* packet begins with the protocol version (uint8) |
|
266 |
* then an uint16 which indicates how many |
|
267 |
* ip:port pairs are in this packet, after that |
|
268 |
* an uint32 (ip) and an uint16 (port) for each pair |
|
269 |
*/ |
|
270 |
||
271 |
ver = NetworkRecv_uint8(&_udp_cs, p); |
|
272 |
||
273 |
if (_udp_cs.has_quit) return; |
|
274 |
||
275 |
if (ver == 1) { |
|
276 |
for (i = NetworkRecv_uint16(&_udp_cs, p); i != 0 ; i--) { |
|
277 |
ip.s_addr = TO_LE32(NetworkRecv_uint32(&_udp_cs, p)); |
|
278 |
port = NetworkRecv_uint16(&_udp_cs, p); |
|
279 |
NetworkUDPQueryServer(inet_ntoa(ip), port); |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER) |
|
285 |
{ |
|
286 |
_network_advertise_retries = 0; |
|
287 |
DEBUG(net, 2, "[udp] advertising on master server successfull"); |
|
288 |
||
289 |
/* We are advertised, but we don't want to! */ |
|
290 |
if (!_network_advertise) NetworkUDPRemoveAdvertise(); |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* A client has requested the names of some NewGRFs. |
|
295 |
* |
|
296 |
* Replying this can be tricky as we have a limit of SEND_MTU bytes |
|
297 |
* in the reply packet and we can send up to 100 bytes per NewGRF |
|
298 |
* (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name). |
|
299 |
* As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it |
|
300 |
* could be that a packet overflows. To stop this we only reply |
|
301 |
* with the first N NewGRFs so that if the first N + 1 NewGRFs |
|
302 |
* would be sent, the packet overflows. |
|
303 |
* in_reply and in_reply_count are used to keep a list of GRFs to |
|
304 |
* send in the reply. |
|
305 |
*/ |
|
306 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS) |
|
307 |
{ |
|
308 |
uint8 num_grfs; |
|
309 |
uint i; |
|
310 |
||
311 |
const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT]; |
|
312 |
Packet *packet; |
|
313 |
uint8 in_reply_count = 0; |
|
314 |
uint packet_len = 0; |
|
315 |
||
316 |
/* Just a fail-safe.. should never happen */ |
|
317 |
if (_udp_cs.has_quit) return; |
|
318 |
||
319 |
DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); |
|
320 |
||
321 |
num_grfs = NetworkRecv_uint8 (&_udp_cs, p); |
|
322 |
if (num_grfs > NETWORK_MAX_GRF_COUNT) return; |
|
323 |
||
324 |
for (i = 0; i < num_grfs; i++) { |
|
325 |
GRFConfig c; |
|
326 |
const GRFConfig *f; |
|
327 |
||
328 |
NetworkRecv_GRFIdentifier(&_udp_cs, p, &c); |
|
329 |
||
330 |
/* Find the matching GRF file */ |
|
331 |
f = FindGRFConfig(c.grfid, c.md5sum); |
|
332 |
if (f == NULL) continue; // The GRF is unknown to this server |
|
333 |
||
334 |
/* If the reply might exceed the size of the packet, only reply |
|
335 |
* the current list and do not send the other data. |
|
336 |
* The name could be an empty string, if so take the filename. */ |
|
337 |
packet_len += sizeof(c.grfid) + sizeof(c.md5sum) + |
|
338 |
min(strlen((f->name != NULL && strlen(f->name) > 0) ? f->name : f->filename) + 1, NETWORK_GRF_NAME_LENGTH); |
|
339 |
if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply |
|
340 |
break; |
|
341 |
} |
|
342 |
in_reply[in_reply_count] = f; |
|
343 |
in_reply_count++; |
|
344 |
} |
|
345 |
||
346 |
if (in_reply_count == 0) return; |
|
347 |
||
348 |
packet = NetworkSend_Init(PACKET_UDP_SERVER_NEWGRFS); |
|
349 |
NetworkSend_uint8 (packet, in_reply_count); |
|
350 |
for (i = 0; i < in_reply_count; i++) { |
|
351 |
char name[NETWORK_GRF_NAME_LENGTH]; |
|
352 |
||
353 |
/* The name could be an empty string, if so take the filename */ |
|
354 |
ttd_strlcpy(name, (in_reply[i]->name != NULL && strlen(in_reply[i]->name) > 0) ? |
|
355 |
in_reply[i]->name : in_reply[i]->filename, sizeof(name)); |
|
356 |
NetworkSend_GRFIdentifier(packet, in_reply[i]); |
|
357 |
NetworkSend_string(packet, name); |
|
358 |
} |
|
359 |
||
360 |
NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); |
|
361 |
free(packet); |
|
362 |
} |
|
363 |
||
364 |
/** The return of the client's request of the names of some NewGRFs */ |
|
365 |
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS) |
|
366 |
{ |
|
367 |
uint8 num_grfs; |
|
368 |
uint i; |
|
369 |
||
370 |
/* Just a fail-safe.. should never happen */ |
|
371 |
if (_udp_cs.has_quit) return; |
|
372 |
||
373 |
DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); |
|
374 |
||
375 |
num_grfs = NetworkRecv_uint8 (&_udp_cs, p); |
|
376 |
if (num_grfs > NETWORK_MAX_GRF_COUNT) return; |
|
377 |
||
378 |
for (i = 0; i < num_grfs; i++) { |
|
379 |
char *unknown_name; |
|
380 |
char name[NETWORK_GRF_NAME_LENGTH]; |
|
381 |
GRFConfig c; |
|
382 |
||
383 |
NetworkRecv_GRFIdentifier(&_udp_cs, p, &c); |
|
384 |
NetworkRecv_string(&_udp_cs, p, name, sizeof(name)); |
|
385 |
||
386 |
/* An empty name is not possible under normal circumstances |
|
387 |
* and causes problems when showing the NewGRF list. */ |
|
388 |
if (strlen(name) == 0) continue; |
|
389 |
||
390 |
/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple. |
|
391 |
* If it exists and not resolved yet, then name of the fake GRF is |
|
392 |
* overwritten with the name from the reply. */ |
|
393 |
unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false); |
|
394 |
if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) { |
|
395 |
ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH); |
|
396 |
} |
|
397 |
} |
|
398 |
} |
|
399 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
400 |
/** |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
401 |
* Every type of UDP packet should only be received by a single socket; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
402 |
* The socket communicating with the masterserver should receive the |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
403 |
* game information of some 'random' host. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
404 |
*/ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
405 |
typedef struct NetworkUDPPacketAndSocket { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
406 |
void (*callback)(Packet *p, const struct sockaddr_in *client_addr); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
407 |
SOCKET *incoming_socket; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
408 |
} NetworkUPDPacketAndSocket; |
5642 | 409 |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
410 |
static const NetworkUPDPacketAndSocket _network_udp_packet[PACKET_UDP_END] = { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
411 |
{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER), &_udp_server_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
412 |
{ RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE), &_udp_client_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
413 |
{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO), &_udp_server_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
414 |
{ NULL, NULL }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
415 |
{ NULL, NULL }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
416 |
{ RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER), &_udp_master_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
417 |
{ NULL, NULL }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
418 |
{ RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST), &_udp_client_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
419 |
{ NULL, NULL }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
420 |
{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS), &_udp_server_socket }, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
421 |
{ RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS), &_udp_client_socket }, |
5642 | 422 |
}; |
423 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
424 |
void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr) |
5642 | 425 |
{ |
426 |
byte type; |
|
427 |
||
428 |
/* Fake a client, so we can see when there is an illegal packet */ |
|
429 |
_udp_cs.socket = INVALID_SOCKET; |
|
430 |
_udp_cs.has_quit = false; |
|
431 |
||
432 |
type = NetworkRecv_uint8(&_udp_cs, p); |
|
433 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
434 |
if (type < PACKET_UDP_END && *_network_udp_packet[type].incoming_socket == udp && !_udp_cs.has_quit) { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
435 |
_network_udp_packet[type].callback(p, client_addr); |
5642 | 436 |
} else { |
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
437 |
if (*_network_udp_packet[type].incoming_socket != udp) { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
438 |
DEBUG(net, 0, "[udp] received packet on wrong port from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
439 |
} else if (!_udp_cs.has_quit) { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
440 |
DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type, inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); |
5642 | 441 |
} else { |
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
442 |
DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); |
5642 | 443 |
} |
444 |
} |
|
445 |
} |
|
446 |
||
447 |
||
448 |
// Close UDP connection |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
449 |
void NetworkUDPStop(void) |
5642 | 450 |
{ |
451 |
DEBUG(net, 1, "[udp] closed listeners"); |
|
452 |
||
453 |
if (_network_udp_server) { |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
454 |
NetworkUDPClose(&_udp_server_socket); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
455 |
NetworkUDPClose(&_udp_master_socket); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
456 |
} else { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
457 |
NetworkUDPClose(&_udp_client_socket); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
458 |
} |
5642 | 459 |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
460 |
_network_udp_server = false; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
461 |
_network_udp_broadcast = 0; |
5642 | 462 |
} |
463 |
||
464 |
// Broadcast to all ips |
|
465 |
static void NetworkUDPBroadCast(SOCKET udp) |
|
466 |
{ |
|
467 |
Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); |
|
468 |
uint i; |
|
469 |
||
470 |
for (i = 0; _broadcast_list[i] != 0; i++) { |
|
471 |
struct sockaddr_in out_addr; |
|
472 |
||
473 |
out_addr.sin_family = AF_INET; |
|
474 |
out_addr.sin_port = htons(_network_server_port); |
|
475 |
out_addr.sin_addr.s_addr = _broadcast_list[i]; |
|
476 |
||
477 |
DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr)); |
|
478 |
||
479 |
NetworkSendUDP_Packet(udp, p, &out_addr); |
|
480 |
} |
|
481 |
||
482 |
free(p); |
|
483 |
} |
|
484 |
||
485 |
||
486 |
// Request the the server-list from the master server |
|
487 |
void NetworkUDPQueryMasterServer(void) |
|
488 |
{ |
|
489 |
struct sockaddr_in out_addr; |
|
490 |
Packet *p; |
|
491 |
||
492 |
if (_udp_client_socket == INVALID_SOCKET) |
|
493 |
if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
494 |
return; |
|
495 |
||
496 |
p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST); |
|
497 |
||
498 |
out_addr.sin_family = AF_INET; |
|
499 |
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
500 |
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
501 |
||
502 |
// packet only contains protocol version |
|
503 |
NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
504 |
||
505 |
NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); |
|
506 |
||
507 |
DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port)); |
|
508 |
||
509 |
free(p); |
|
510 |
} |
|
511 |
||
512 |
// Find all servers |
|
513 |
void NetworkUDPSearchGame(void) |
|
514 |
{ |
|
515 |
// We are still searching.. |
|
516 |
if (_network_udp_broadcast > 0) return; |
|
517 |
||
518 |
// No UDP-socket yet.. |
|
519 |
if (_udp_client_socket == INVALID_SOCKET) |
|
520 |
if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
521 |
return; |
|
522 |
||
523 |
DEBUG(net, 0, "[udp] searching server"); |
|
524 |
||
525 |
NetworkUDPBroadCast(_udp_client_socket); |
|
526 |
_network_udp_broadcast = 300; // Stay searching for 300 ticks |
|
527 |
} |
|
528 |
||
529 |
NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port) |
|
530 |
{ |
|
531 |
struct sockaddr_in out_addr; |
|
532 |
Packet *p; |
|
533 |
NetworkGameList *item; |
|
534 |
||
535 |
// No UDP-socket yet.. |
|
536 |
if (_udp_client_socket == INVALID_SOCKET) |
|
537 |
if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
538 |
return NULL; |
|
539 |
||
540 |
out_addr.sin_family = AF_INET; |
|
541 |
out_addr.sin_port = htons(port); |
|
542 |
out_addr.sin_addr.s_addr = NetworkResolveHost(host); |
|
543 |
||
544 |
// Clear item in gamelist |
|
545 |
item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port)); |
|
546 |
memset(&item->info, 0, sizeof(item->info)); |
|
547 |
ttd_strlcpy(item->info.server_name, host, lengthof(item->info.server_name)); |
|
548 |
ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname)); |
|
549 |
item->online = false; |
|
550 |
||
551 |
// Init the packet |
|
552 |
p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); |
|
553 |
||
554 |
NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); |
|
555 |
||
556 |
free(p); |
|
557 |
||
558 |
UpdateNetworkGameWindow(false); |
|
559 |
return item; |
|
560 |
} |
|
561 |
||
562 |
/* Remove our advertise from the master-server */ |
|
563 |
void NetworkUDPRemoveAdvertise(void) |
|
564 |
{ |
|
565 |
struct sockaddr_in out_addr; |
|
566 |
Packet *p; |
|
567 |
||
568 |
/* Check if we are advertising */ |
|
569 |
if (!_networking || !_network_server || !_network_udp_server) return; |
|
570 |
||
571 |
/* check for socket */ |
|
572 |
if (_udp_master_socket == INVALID_SOCKET) |
|
573 |
if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) |
|
574 |
return; |
|
575 |
||
576 |
DEBUG(net, 1, "[udp] removing advertise from master server"); |
|
577 |
||
578 |
/* Find somewhere to send */ |
|
579 |
out_addr.sin_family = AF_INET; |
|
580 |
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
581 |
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
582 |
||
583 |
/* Send the packet */ |
|
584 |
p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER); |
|
585 |
/* Packet is: Version, server_port */ |
|
586 |
NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
587 |
NetworkSend_uint16(p, _network_server_port); |
|
588 |
NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); |
|
589 |
||
590 |
free(p); |
|
591 |
} |
|
592 |
||
593 |
/* Register us to the master server |
|
594 |
This function checks if it needs to send an advertise */ |
|
595 |
void NetworkUDPAdvertise(void) |
|
596 |
{ |
|
597 |
struct sockaddr_in out_addr; |
|
598 |
Packet *p; |
|
599 |
||
600 |
/* Check if we should send an advertise */ |
|
601 |
if (!_networking || !_network_server || !_network_udp_server || !_network_advertise) |
|
602 |
return; |
|
603 |
||
604 |
/* check for socket */ |
|
605 |
if (_udp_master_socket == INVALID_SOCKET) |
|
606 |
if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) |
|
607 |
return; |
|
608 |
||
609 |
if (_network_need_advertise) { |
|
610 |
_network_need_advertise = false; |
|
611 |
_network_advertise_retries = ADVERTISE_RETRY_TIMES; |
|
612 |
} else { |
|
613 |
/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */ |
|
614 |
if (_network_advertise_retries == 0) { |
|
615 |
if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) |
|
616 |
return; |
|
617 |
_network_advertise_retries = ADVERTISE_RETRY_TIMES; |
|
618 |
} |
|
619 |
||
620 |
if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) |
|
621 |
return; |
|
622 |
} |
|
623 |
||
624 |
_network_advertise_retries--; |
|
625 |
_network_last_advertise_frame = _frame_counter; |
|
626 |
||
627 |
/* Find somewhere to send */ |
|
628 |
out_addr.sin_family = AF_INET; |
|
629 |
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
630 |
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
631 |
||
632 |
DEBUG(net, 1, "[udp] advertising to master server"); |
|
633 |
||
634 |
/* Send the packet */ |
|
635 |
p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER); |
|
636 |
/* Packet is: WELCOME_MESSAGE, Version, server_port */ |
|
637 |
NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE); |
|
638 |
NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
639 |
NetworkSend_uint16(p, _network_server_port); |
|
640 |
NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); |
|
641 |
||
642 |
free(p); |
|
643 |
} |
|
644 |
||
645 |
void NetworkUDPInitialize(void) |
|
646 |
{ |
|
647 |
_udp_client_socket = INVALID_SOCKET; |
|
648 |
_udp_server_socket = INVALID_SOCKET; |
|
649 |
_udp_master_socket = INVALID_SOCKET; |
|
650 |
||
651 |
_network_udp_server = false; |
|
652 |
_network_udp_broadcast = 0; |
|
653 |
} |
|
654 |
||
655 |
#endif /* ENABLE_NETWORK */ |