|
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 |
|
31 #define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, struct sockaddr_in *client_addr) |
|
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 |
|
53 NetworkSend_NetworkGameInfo(p, &_network_game_info); |
|
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 |
|
400 |
|
401 // The layout for the receive-functions by UDP |
|
402 typedef void NetworkUDPPacket(Packet *p, struct sockaddr_in *client_addr); |
|
403 |
|
404 static NetworkUDPPacket* const _network_udp_packet[] = { |
|
405 RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER), |
|
406 RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE), |
|
407 RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO), |
|
408 NULL, |
|
409 NULL, |
|
410 RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER), |
|
411 NULL, |
|
412 RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST), |
|
413 NULL, |
|
414 RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS), |
|
415 RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS), |
|
416 }; |
|
417 |
|
418 |
|
419 // If this fails, check the array above with network_data.h |
|
420 assert_compile(lengthof(_network_udp_packet) == PACKET_UDP_END); |
|
421 |
|
422 |
|
423 void NetworkHandleUDPPacket(Packet *p, struct sockaddr_in *client_addr) |
|
424 { |
|
425 byte type; |
|
426 |
|
427 /* Fake a client, so we can see when there is an illegal packet */ |
|
428 _udp_cs.socket = INVALID_SOCKET; |
|
429 _udp_cs.has_quit = false; |
|
430 |
|
431 type = NetworkRecv_uint8(&_udp_cs, p); |
|
432 |
|
433 if (type < PACKET_UDP_END && _network_udp_packet[type] != NULL && !_udp_cs.has_quit) { |
|
434 _network_udp_packet[type](p, client_addr); |
|
435 } else { |
|
436 if (!_udp_cs.has_quit) { |
|
437 DEBUG(net, 0, "[udp] received invalid packet type %d", type); |
|
438 } else { |
|
439 DEBUG(net, 0, "[udp] received illegal packet"); |
|
440 } |
|
441 } |
|
442 } |
|
443 |
|
444 |
|
445 // Close UDP connection |
|
446 void NetworkUDPClose(void) |
|
447 { |
|
448 DEBUG(net, 1, "[udp] closed listeners"); |
|
449 |
|
450 if (_network_udp_server) { |
|
451 if (_udp_server_socket != INVALID_SOCKET) { |
|
452 closesocket(_udp_server_socket); |
|
453 _udp_server_socket = INVALID_SOCKET; |
|
454 } |
|
455 |
|
456 if (_udp_master_socket != INVALID_SOCKET) { |
|
457 closesocket(_udp_master_socket); |
|
458 _udp_master_socket = INVALID_SOCKET; |
|
459 } |
|
460 |
|
461 _network_udp_server = false; |
|
462 _network_udp_broadcast = 0; |
|
463 } else { |
|
464 if (_udp_client_socket != INVALID_SOCKET) { |
|
465 closesocket(_udp_client_socket); |
|
466 _udp_client_socket = INVALID_SOCKET; |
|
467 } |
|
468 _network_udp_broadcast = 0; |
|
469 } |
|
470 } |
|
471 |
|
472 // Broadcast to all ips |
|
473 static void NetworkUDPBroadCast(SOCKET udp) |
|
474 { |
|
475 Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); |
|
476 uint i; |
|
477 |
|
478 for (i = 0; _broadcast_list[i] != 0; i++) { |
|
479 struct sockaddr_in out_addr; |
|
480 |
|
481 out_addr.sin_family = AF_INET; |
|
482 out_addr.sin_port = htons(_network_server_port); |
|
483 out_addr.sin_addr.s_addr = _broadcast_list[i]; |
|
484 |
|
485 DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr)); |
|
486 |
|
487 NetworkSendUDP_Packet(udp, p, &out_addr); |
|
488 } |
|
489 |
|
490 free(p); |
|
491 } |
|
492 |
|
493 |
|
494 // Request the the server-list from the master server |
|
495 void NetworkUDPQueryMasterServer(void) |
|
496 { |
|
497 struct sockaddr_in out_addr; |
|
498 Packet *p; |
|
499 |
|
500 if (_udp_client_socket == INVALID_SOCKET) |
|
501 if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
502 return; |
|
503 |
|
504 p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST); |
|
505 |
|
506 out_addr.sin_family = AF_INET; |
|
507 out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
508 out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
509 |
|
510 // packet only contains protocol version |
|
511 NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
512 |
|
513 NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); |
|
514 |
|
515 DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port)); |
|
516 |
|
517 free(p); |
|
518 } |
|
519 |
|
520 // Find all servers |
|
521 void NetworkUDPSearchGame(void) |
|
522 { |
|
523 // We are still searching.. |
|
524 if (_network_udp_broadcast > 0) return; |
|
525 |
|
526 // No UDP-socket yet.. |
|
527 if (_udp_client_socket == INVALID_SOCKET) |
|
528 if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
529 return; |
|
530 |
|
531 DEBUG(net, 0, "[udp] searching server"); |
|
532 |
|
533 NetworkUDPBroadCast(_udp_client_socket); |
|
534 _network_udp_broadcast = 300; // Stay searching for 300 ticks |
|
535 } |
|
536 |
|
537 NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port) |
|
538 { |
|
539 struct sockaddr_in out_addr; |
|
540 Packet *p; |
|
541 NetworkGameList *item; |
|
542 |
|
543 // No UDP-socket yet.. |
|
544 if (_udp_client_socket == INVALID_SOCKET) |
|
545 if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) |
|
546 return NULL; |
|
547 |
|
548 out_addr.sin_family = AF_INET; |
|
549 out_addr.sin_port = htons(port); |
|
550 out_addr.sin_addr.s_addr = NetworkResolveHost(host); |
|
551 |
|
552 // Clear item in gamelist |
|
553 item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port)); |
|
554 memset(&item->info, 0, sizeof(item->info)); |
|
555 ttd_strlcpy(item->info.server_name, host, lengthof(item->info.server_name)); |
|
556 ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname)); |
|
557 item->online = false; |
|
558 |
|
559 // Init the packet |
|
560 p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); |
|
561 |
|
562 NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); |
|
563 |
|
564 free(p); |
|
565 |
|
566 UpdateNetworkGameWindow(false); |
|
567 return item; |
|
568 } |
|
569 |
|
570 /* Remove our advertise from the master-server */ |
|
571 void NetworkUDPRemoveAdvertise(void) |
|
572 { |
|
573 struct sockaddr_in out_addr; |
|
574 Packet *p; |
|
575 |
|
576 /* Check if we are advertising */ |
|
577 if (!_networking || !_network_server || !_network_udp_server) return; |
|
578 |
|
579 /* check for socket */ |
|
580 if (_udp_master_socket == INVALID_SOCKET) |
|
581 if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) |
|
582 return; |
|
583 |
|
584 DEBUG(net, 1, "[udp] removing advertise from master server"); |
|
585 |
|
586 /* Find somewhere to send */ |
|
587 out_addr.sin_family = AF_INET; |
|
588 out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
589 out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
590 |
|
591 /* Send the packet */ |
|
592 p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER); |
|
593 /* Packet is: Version, server_port */ |
|
594 NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
595 NetworkSend_uint16(p, _network_server_port); |
|
596 NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); |
|
597 |
|
598 free(p); |
|
599 } |
|
600 |
|
601 /* Register us to the master server |
|
602 This function checks if it needs to send an advertise */ |
|
603 void NetworkUDPAdvertise(void) |
|
604 { |
|
605 struct sockaddr_in out_addr; |
|
606 Packet *p; |
|
607 |
|
608 /* Check if we should send an advertise */ |
|
609 if (!_networking || !_network_server || !_network_udp_server || !_network_advertise) |
|
610 return; |
|
611 |
|
612 /* check for socket */ |
|
613 if (_udp_master_socket == INVALID_SOCKET) |
|
614 if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) |
|
615 return; |
|
616 |
|
617 if (_network_need_advertise) { |
|
618 _network_need_advertise = false; |
|
619 _network_advertise_retries = ADVERTISE_RETRY_TIMES; |
|
620 } else { |
|
621 /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */ |
|
622 if (_network_advertise_retries == 0) { |
|
623 if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) |
|
624 return; |
|
625 _network_advertise_retries = ADVERTISE_RETRY_TIMES; |
|
626 } |
|
627 |
|
628 if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) |
|
629 return; |
|
630 } |
|
631 |
|
632 _network_advertise_retries--; |
|
633 _network_last_advertise_frame = _frame_counter; |
|
634 |
|
635 /* Find somewhere to send */ |
|
636 out_addr.sin_family = AF_INET; |
|
637 out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); |
|
638 out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); |
|
639 |
|
640 DEBUG(net, 1, "[udp] advertising to master server"); |
|
641 |
|
642 /* Send the packet */ |
|
643 p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER); |
|
644 /* Packet is: WELCOME_MESSAGE, Version, server_port */ |
|
645 NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE); |
|
646 NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); |
|
647 NetworkSend_uint16(p, _network_server_port); |
|
648 NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); |
|
649 |
|
650 free(p); |
|
651 } |
|
652 |
|
653 void NetworkUDPInitialize(void) |
|
654 { |
|
655 _udp_client_socket = INVALID_SOCKET; |
|
656 _udp_server_socket = INVALID_SOCKET; |
|
657 _udp_master_socket = INVALID_SOCKET; |
|
658 |
|
659 _network_udp_server = false; |
|
660 _network_udp_broadcast = 0; |
|
661 } |
|
662 |
|
663 #endif /* ENABLE_NETWORK */ |