9 |
9 |
10 #ifdef ENABLE_NETWORK |
10 #ifdef ENABLE_NETWORK |
11 |
11 |
12 #include "../stdafx.h" |
12 #include "../stdafx.h" |
13 #include "../debug.h" |
13 #include "../debug.h" |
14 #include "network_data.h" |
|
15 #include "../date_func.h" |
14 #include "../date_func.h" |
16 #include "../map_func.h" |
15 #include "../map_func.h" |
17 #include "network_gamelist.h" |
16 #include "network_gamelist.h" |
18 #include "network_udp.h" |
17 #include "network_udp.h" |
|
18 #include "network_internal.h" |
19 #include "../variables.h" |
19 #include "../variables.h" |
20 #include "../newgrf_config.h" |
20 #include "../newgrf_config.h" |
21 #include "../core/endian_func.hpp" |
21 #include "../core/endian_func.hpp" |
22 #include "../string_func.h" |
22 #include "../string_func.h" |
23 #include "../player_base.h" |
23 #include "../player_base.h" |
24 #include "../player_func.h" |
24 #include "../player_func.h" |
25 #include "../settings_type.h" |
25 #include "../settings_type.h" |
|
26 #include "../rev.h" |
26 |
27 |
27 #include "core/udp.h" |
28 #include "core/udp.h" |
28 |
29 |
29 enum { |
30 enum { |
30 ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes) |
31 ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes) |
66 }; |
67 }; |
67 |
68 |
68 DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER) |
69 DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER) |
69 { |
70 { |
70 // Just a fail-safe.. should never happen |
71 // Just a fail-safe.. should never happen |
71 if (!_network_udp_server) |
72 if (!_network_udp_server) { |
72 return; |
73 return; |
|
74 } |
|
75 |
|
76 NetworkGameInfo ngi; |
|
77 |
|
78 /* Update some game_info */ |
|
79 ngi.clients_on = _network_game_info.clients_on; |
|
80 ngi.start_date = _network_game_info.start_date; |
|
81 |
|
82 ngi.server_lang = _settings_client.network.server_lang; |
|
83 ngi.use_password = !StrEmpty(_settings_client.network.server_password); |
|
84 ngi.clients_max = _settings_client.network.max_clients; |
|
85 ngi.companies_on = ActivePlayerCount(); |
|
86 ngi.companies_max = _settings_client.network.max_companies; |
|
87 ngi.spectators_on = NetworkSpectatorCount(); |
|
88 ngi.spectators_max = _settings_client.network.max_spectators; |
|
89 ngi.game_date = _date; |
|
90 ngi.map_width = MapSizeX(); |
|
91 ngi.map_height = MapSizeY(); |
|
92 ngi.map_set = _settings_game.game_creation.landscape; |
|
93 ngi.dedicated = _network_dedicated; |
|
94 ngi.grfconfig = _grfconfig; |
|
95 |
|
96 ttd_strlcpy(ngi.map_name, _network_game_info.map_name, lengthof(ngi.map_name)); |
|
97 ttd_strlcpy(ngi.server_name, _settings_client.network.server_name, lengthof(ngi.server_name)); |
|
98 ttd_strlcpy(ngi.server_revision, _openttd_revision, lengthof(ngi.server_revision)); |
73 |
99 |
74 Packet packet(PACKET_UDP_SERVER_RESPONSE); |
100 Packet packet(PACKET_UDP_SERVER_RESPONSE); |
75 |
101 this->Send_NetworkGameInfo(&packet, &ngi); |
76 // Update some game_info |
|
77 _network_game_info.game_date = _date; |
|
78 _network_game_info.map_width = MapSizeX(); |
|
79 _network_game_info.map_height = MapSizeY(); |
|
80 _network_game_info.map_set = _settings.game_creation.landscape; |
|
81 _network_game_info.companies_on = ActivePlayerCount(); |
|
82 _network_game_info.spectators_on = NetworkSpectatorCount(); |
|
83 _network_game_info.grfconfig = _grfconfig; |
|
84 |
|
85 this->Send_NetworkGameInfo(&packet, &_network_game_info); |
|
86 |
102 |
87 // Let the client know that we are here |
103 // Let the client know that we are here |
88 this->SendPacket(&packet, client_addr); |
104 this->SendPacket(&packet, client_addr); |
89 |
105 |
90 DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr)); |
106 DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr)); |
426 for (i = 0; _broadcast_list[i] != 0; i++) { |
442 for (i = 0; _broadcast_list[i] != 0; i++) { |
427 Packet p(PACKET_UDP_CLIENT_FIND_SERVER); |
443 Packet p(PACKET_UDP_CLIENT_FIND_SERVER); |
428 struct sockaddr_in out_addr; |
444 struct sockaddr_in out_addr; |
429 |
445 |
430 out_addr.sin_family = AF_INET; |
446 out_addr.sin_family = AF_INET; |
431 out_addr.sin_port = htons(_network_server_port); |
447 out_addr.sin_port = htons(_settings_client.network.server_port); |
432 out_addr.sin_addr.s_addr = _broadcast_list[i]; |
448 out_addr.sin_addr.s_addr = _broadcast_list[i]; |
433 |
449 |
434 DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr)); |
450 DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr)); |
435 |
451 |
436 socket->SendPacket(&p, &out_addr); |
452 socket->SendPacket(&p, &out_addr); |
533 |
549 |
534 /* Send the packet */ |
550 /* Send the packet */ |
535 Packet p(PACKET_UDP_SERVER_UNREGISTER); |
551 Packet p(PACKET_UDP_SERVER_UNREGISTER); |
536 /* Packet is: Version, server_port */ |
552 /* Packet is: Version, server_port */ |
537 p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION); |
553 p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION); |
538 p.Send_uint16(_network_server_port); |
554 p.Send_uint16(_settings_client.network.server_port); |
539 _udp_master_socket->SendPacket(&p, &out_addr); |
555 _udp_master_socket->SendPacket(&p, &out_addr); |
540 } |
556 } |
541 |
557 |
542 /* Register us to the master server |
558 /* Register us to the master server |
543 This function checks if it needs to send an advertise */ |
559 This function checks if it needs to send an advertise */ |
582 /* Send the packet */ |
598 /* Send the packet */ |
583 Packet p(PACKET_UDP_SERVER_REGISTER); |
599 Packet p(PACKET_UDP_SERVER_REGISTER); |
584 /* Packet is: WELCOME_MESSAGE, Version, server_port */ |
600 /* Packet is: WELCOME_MESSAGE, Version, server_port */ |
585 p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE); |
601 p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE); |
586 p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION); |
602 p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION); |
587 p.Send_uint16(_network_server_port); |
603 p.Send_uint16(_settings_client.network.server_port); |
588 _udp_master_socket->SendPacket(&p, &out_addr); |
604 _udp_master_socket->SendPacket(&p, &out_addr); |
589 } |
605 } |
590 |
606 |
591 void NetworkUDPInitialize() |
607 void NetworkUDPInitialize() |
592 { |
608 { |