2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file network_udp.cpp This file handles the UDP related communication.
|
|
|
|
*
|
|
|
|
* This is the GameServer <-> MasterServer and GameServer <-> GameClient
|
|
|
|
* communication before the game is being joined.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../stdafx.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "../date_func.h"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "../map_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../debug.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network_gamelist.h"
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "network_internal.h"
|
2009-01-20 11:28:18 +00:00
|
|
|
#include "network_udp.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "network.h"
|
2007-12-25 13:28:09 +00:00
|
|
|
#include "../core/endian_func.hpp"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "../company_base.h"
|
2019-03-17 00:59:46 +00:00
|
|
|
#include "../thread.h"
|
2008-06-03 08:04:35 +00:00
|
|
|
#include "../rev.h"
|
2010-07-31 09:33:39 +00:00
|
|
|
#include "../newgrf_text.h"
|
2010-12-05 22:21:13 +00:00
|
|
|
#include "../strings_func.h"
|
|
|
|
#include "table/strings.h"
|
2019-03-10 23:45:39 +00:00
|
|
|
#include <mutex>
|
2019-04-10 16:35:43 +00:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
#include "../3rdparty/mingw-std-threads/mingw.mutex.h"
|
|
|
|
#endif
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
#include "core/udp.h"
|
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
extern const uint8 _out_of_band_grf_md5[16] { 0x00, 0xB0, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0, 0xC0, 0xDE, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
static const uint32 FIND_SERVER_EXTENDED_TOKEN = 0x2A49582A;
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/** Mutex for all out threaded udp resolution and such. */
|
2019-03-10 23:45:39 +00:00
|
|
|
static std::mutex _network_udp_mutex;
|
2009-01-20 03:44:43 +00:00
|
|
|
|
2009-04-05 19:43:41 +00:00
|
|
|
/** Session key to register ourselves to the master server */
|
|
|
|
static uint64 _session_key = 0;
|
|
|
|
|
2021-02-25 19:30:16 +00:00
|
|
|
static const std::chrono::minutes ADVERTISE_NORMAL_INTERVAL(15); ///< interval between advertising.
|
|
|
|
static const std::chrono::seconds ADVERTISE_RETRY_INTERVAL(10); ///< re-advertise when no response after this amount of time.
|
2015-09-19 16:49:46 +00:00
|
|
|
static const uint32 ADVERTISE_RETRY_TIMES = 3; ///< give up re-advertising after this much failed retries
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2021-04-11 09:22:50 +00:00
|
|
|
static bool _network_udp_server; ///< Is the UDP server started?
|
|
|
|
static uint16 _network_udp_broadcast; ///< Timeout for the UDP broadcasts.
|
|
|
|
static uint8 _network_advertise_retries; ///< The number of advertisement retries we did.
|
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
/** Some information about a socket, which exists before the actual socket has been created to provide locking and the likes. */
|
|
|
|
struct UDPSocket {
|
|
|
|
const std::string name; ///< The name of the socket.
|
|
|
|
std::mutex mutex; ///< Mutex for everything that (indirectly) touches the sockets within the handler.
|
|
|
|
NetworkUDPSocketHandler *socket; ///< The actual socket, which may be nullptr when not initialized yet.
|
|
|
|
std::atomic<int> receive_iterations_locked; ///< The number of receive iterations the mutex was locked.
|
|
|
|
|
|
|
|
UDPSocket(const std::string &name_) : name(name_), socket(nullptr) {}
|
|
|
|
|
|
|
|
void Close()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
socket->Close();
|
|
|
|
delete socket;
|
|
|
|
socket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReceivePackets()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
|
|
|
|
if (!lock.try_lock()) {
|
|
|
|
if (++receive_iterations_locked % 32 == 0) {
|
|
|
|
DEBUG(net, 0, "[udp] %s background UDP loop processing appears to be blocked. Your OS may be low on UDP send buffers.", name.c_str());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
receive_iterations_locked.store(0);
|
|
|
|
socket->ReceivePackets();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static UDPSocket _udp_client("Client"); ///< udp client socket
|
|
|
|
static UDPSocket _udp_server("Server"); ///< udp server socket
|
|
|
|
static UDPSocket _udp_master("Master"); ///< udp master socket
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
static Packet PrepareUdpClientFindServerPacket()
|
|
|
|
{
|
|
|
|
Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
|
|
|
|
p.Send_uint32(FIND_SERVER_EXTENDED_TOKEN);
|
|
|
|
p.Send_uint16(0); // flags
|
|
|
|
p.Send_uint16(0); // version
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-01-05 17:08:49 +00:00
|
|
|
/**
|
|
|
|
* Helper function doing the actual work for querying the server.
|
|
|
|
* @param address The address of the server.
|
|
|
|
* @param needs_mutex Whether we need to acquire locks when sending the packet or not.
|
|
|
|
* @param manually Whether the address was entered manually.
|
|
|
|
*/
|
2019-03-17 00:59:46 +00:00
|
|
|
static void DoNetworkUDPQueryServer(NetworkAddress &address, bool needs_mutex, bool manually)
|
2012-01-05 17:08:49 +00:00
|
|
|
{
|
|
|
|
/* Clear item in gamelist */
|
|
|
|
NetworkGameList *item = CallocT<NetworkGameList>(1);
|
2019-03-17 00:59:46 +00:00
|
|
|
address.GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
|
|
|
|
strecpy(item->info.hostname, address.GetHostname(), lastof(item->info.hostname));
|
|
|
|
item->address = address;
|
2012-01-05 17:08:49 +00:00
|
|
|
item->manually = manually;
|
|
|
|
NetworkGameListAddItemDelayed(item);
|
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
std::unique_lock<std::mutex> lock(_udp_client.mutex, std::defer_lock);
|
2019-03-10 23:45:39 +00:00
|
|
|
if (needs_mutex) lock.lock();
|
2012-01-05 17:08:49 +00:00
|
|
|
/* Init the packet */
|
2018-05-12 08:11:41 +00:00
|
|
|
Packet p = PrepareUdpClientFindServerPacket();
|
2021-04-11 09:30:44 +00:00
|
|
|
if (_udp_client.socket != nullptr) _udp_client.socket->SendPacket(&p, &address);
|
2012-01-05 17:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query a specific server.
|
|
|
|
* @param address The address of the server.
|
|
|
|
* @param manually Whether the address was entered manually.
|
|
|
|
*/
|
|
|
|
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (address.IsResolved() || !StartNewThread(nullptr, "ottd:udp-query", &DoNetworkUDPQueryServer, std::move(address), true, std::move(manually))) {
|
2019-03-17 00:59:46 +00:00
|
|
|
DoNetworkUDPQueryServer(address, true, manually);
|
2012-01-05 17:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 14:30:01 +00:00
|
|
|
///*** Communication with the masterserver ***/
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Helper class for connecting to the master server. */
|
2007-01-12 14:30:01 +00:00
|
|
|
class MasterNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
|
|
|
|
protected:
|
2019-03-24 16:24:06 +00:00
|
|
|
void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr) override;
|
|
|
|
void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr) override;
|
2007-01-12 14:30:01 +00:00
|
|
|
public:
|
2011-05-04 20:24:23 +00:00
|
|
|
/**
|
|
|
|
* Create the socket.
|
|
|
|
* @param addresses The addresses to bind on.
|
|
|
|
*/
|
2009-04-08 19:36:51 +00:00
|
|
|
MasterNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
|
2007-01-12 14:30:01 +00:00
|
|
|
virtual ~MasterNetworkUDPSocketHandler() {}
|
|
|
|
};
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void MasterNetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
|
2007-01-12 14:30:01 +00:00
|
|
|
{
|
|
|
|
_network_advertise_retries = 0;
|
2009-05-06 09:52:52 +00:00
|
|
|
DEBUG(net, 2, "[udp] advertising on master server successful (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
|
2007-01-12 14:30:01 +00:00
|
|
|
|
|
|
|
/* We are advertised, but we don't want to! */
|
2009-04-10 20:18:48 +00:00
|
|
|
if (!_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(false);
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void MasterNetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr)
|
2009-04-05 19:43:41 +00:00
|
|
|
{
|
|
|
|
_session_key = p->Recv_uint64();
|
2009-05-06 09:52:52 +00:00
|
|
|
DEBUG(net, 2, "[udp] received new session key from master server (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
|
2009-04-05 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 14:30:01 +00:00
|
|
|
///*** Communication with clients (we are server) ***/
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Helper class for handling all server side communication. */
|
2007-01-12 14:30:01 +00:00
|
|
|
class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
|
|
|
|
protected:
|
2019-03-24 16:24:06 +00:00
|
|
|
void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override;
|
|
|
|
void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr) override;
|
|
|
|
void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr) override;
|
2018-05-12 08:11:41 +00:00
|
|
|
void Reply_CLIENT_FIND_SERVER_extended(Packet *p, NetworkAddress *client_addr, NetworkGameInfo *ngi);
|
2007-01-12 14:30:01 +00:00
|
|
|
public:
|
2011-05-04 20:24:23 +00:00
|
|
|
/**
|
|
|
|
* Create the socket.
|
|
|
|
* @param addresses The addresses to bind on.
|
|
|
|
*/
|
2009-04-07 20:27:13 +00:00
|
|
|
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
|
2007-01-12 14:30:01 +00:00
|
|
|
virtual ~ServerNetworkUDPSocketHandler() {}
|
|
|
|
};
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Just a fail-safe.. should never happen */
|
2008-06-03 08:04:35 +00:00
|
|
|
if (!_network_udp_server) {
|
2004-12-04 17:54:56 +00:00
|
|
|
return;
|
2008-06-03 08:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkGameInfo ngi;
|
|
|
|
|
|
|
|
/* Update some game_info */
|
|
|
|
ngi.clients_on = _network_game_info.clients_on;
|
2009-06-13 12:06:31 +00:00
|
|
|
ngi.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
|
2008-06-03 08:04:35 +00:00
|
|
|
|
|
|
|
ngi.use_password = !StrEmpty(_settings_client.network.server_password);
|
|
|
|
ngi.clients_max = _settings_client.network.max_clients;
|
2009-05-22 15:23:47 +00:00
|
|
|
ngi.companies_on = (byte)Company::GetNumItems();
|
2008-06-03 08:04:35 +00:00
|
|
|
ngi.companies_max = _settings_client.network.max_companies;
|
|
|
|
ngi.spectators_on = NetworkSpectatorCount();
|
|
|
|
ngi.spectators_max = _settings_client.network.max_spectators;
|
|
|
|
ngi.game_date = _date;
|
|
|
|
ngi.map_width = MapSizeX();
|
|
|
|
ngi.map_height = MapSizeY();
|
|
|
|
ngi.map_set = _settings_game.game_creation.landscape;
|
|
|
|
ngi.dedicated = _network_dedicated;
|
|
|
|
ngi.grfconfig = _grfconfig;
|
|
|
|
|
2008-11-02 11:20:15 +00:00
|
|
|
strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
|
|
|
|
strecpy(ngi.server_revision, _openttd_revision, lastof(ngi.server_revision));
|
2018-05-12 08:11:41 +00:00
|
|
|
strecpy(ngi.short_server_revision, _openttd_revision, lastof(ngi.short_server_revision));
|
|
|
|
|
2021-04-25 18:48:57 +00:00
|
|
|
if (p->CanReadFromPacket(8) && p->Recv_uint32() == FIND_SERVER_EXTENDED_TOKEN) {
|
2018-05-12 08:11:41 +00:00
|
|
|
this->Reply_CLIENT_FIND_SERVER_extended(p, client_addr, &ngi);
|
|
|
|
return;
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
Packet packet(PACKET_UDP_SERVER_RESPONSE);
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendNetworkGameInfo(&packet, &ngi);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Let the client know that we are here */
|
2007-02-01 22:30:35 +00:00
|
|
|
this->SendPacket(&packet, client_addr);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2009-05-06 09:52:52 +00:00
|
|
|
DEBUG(net, 2, "[udp] queried from %s", client_addr->GetHostname());
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
void ServerNetworkUDPSocketHandler::Reply_CLIENT_FIND_SERVER_extended(Packet *p, NetworkAddress *client_addr, NetworkGameInfo *ngi)
|
|
|
|
{
|
2018-11-27 18:46:48 +00:00
|
|
|
uint16 flags = p->Recv_uint16();
|
2018-05-12 08:11:41 +00:00
|
|
|
uint16 version = p->Recv_uint16();
|
|
|
|
|
|
|
|
Packet packet(PACKET_UDP_EX_SERVER_RESPONSE);
|
2018-11-27 18:46:48 +00:00
|
|
|
this->SendNetworkGameInfoExtended(&packet, ngi, flags, version);
|
2018-05-12 08:11:41 +00:00
|
|
|
|
|
|
|
/* Let the client know that we are here */
|
2019-10-31 20:40:07 +00:00
|
|
|
this->SendPacket(&packet, client_addr, false, false, true);
|
2018-05-12 08:11:41 +00:00
|
|
|
|
|
|
|
DEBUG(net, 2, "[udp] queried (extended: %u) from %s", version, client_addr->GetHostname());
|
|
|
|
}
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
|
2004-12-13 19:55:31 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Just a fail-safe.. should never happen */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (!_network_udp_server) return;
|
2004-12-13 19:55:31 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
Packet packet(PACKET_UDP_SERVER_DETAIL_INFO);
|
2004-12-13 19:55:31 +00:00
|
|
|
|
|
|
|
/* Send the amount of active companies */
|
2007-02-01 23:26:44 +00:00
|
|
|
packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
|
2009-05-22 15:23:47 +00:00
|
|
|
packet.Send_uint8 ((uint8)Company::GetNumItems());
|
2004-12-13 19:55:31 +00:00
|
|
|
|
2008-12-22 18:40:57 +00:00
|
|
|
/* Fetch the latest version of the stats */
|
|
|
|
NetworkCompanyStats company_stats[MAX_COMPANIES];
|
|
|
|
NetworkPopulateCompanyStats(company_stats);
|
2004-12-13 19:55:31 +00:00
|
|
|
|
2010-12-05 22:21:13 +00:00
|
|
|
/* The minimum company information "blob" size. */
|
|
|
|
static const uint MIN_CI_SIZE = 54;
|
|
|
|
uint max_cname_length = NETWORK_COMPANY_NAME_LENGTH;
|
|
|
|
|
2021-04-18 07:26:06 +00:00
|
|
|
if (!packet.CanWriteToPacket(Company::GetNumItems() * (MIN_CI_SIZE + NETWORK_COMPANY_NAME_LENGTH))) {
|
2010-12-05 22:21:13 +00:00
|
|
|
/* Assume we can at least put the company information in the packets. */
|
2021-04-18 07:26:06 +00:00
|
|
|
assert(packet.CanWriteToPacket(Company::GetNumItems() * MIN_CI_SIZE));
|
2010-12-05 22:21:13 +00:00
|
|
|
|
|
|
|
/* At this moment the company names might not fit in the
|
|
|
|
* packet. Check whether that is really the case. */
|
|
|
|
|
|
|
|
for (;;) {
|
2021-04-18 07:26:06 +00:00
|
|
|
size_t required = 0;
|
2019-12-14 16:22:38 +00:00
|
|
|
for (const Company *company : Company::Iterate()) {
|
2010-12-05 22:21:13 +00:00
|
|
|
char company_name[NETWORK_COMPANY_NAME_LENGTH];
|
|
|
|
SetDParam(0, company->index);
|
|
|
|
GetString(company_name, STR_COMPANY_NAME, company_name + max_cname_length - 1);
|
2021-04-18 07:26:06 +00:00
|
|
|
required += MIN_CI_SIZE;
|
|
|
|
required += strlen(company_name);
|
2010-12-05 22:21:13 +00:00
|
|
|
}
|
2021-04-18 07:26:06 +00:00
|
|
|
if (packet.CanWriteToPacket(required)) break;
|
2010-12-05 22:21:13 +00:00
|
|
|
|
|
|
|
/* Try again, with slightly shorter strings. */
|
|
|
|
assert(max_cname_length > 0);
|
|
|
|
max_cname_length--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Go through all the companies */
|
2019-12-14 16:22:38 +00:00
|
|
|
for (const Company *company : Company::Iterate()) {
|
2004-12-13 19:55:31 +00:00
|
|
|
/* Send the information */
|
2010-12-05 22:21:13 +00:00
|
|
|
this->SendCompanyInformation(&packet, company, &company_stats[company->index], max_cname_length);
|
2004-12-13 19:55:31 +00:00
|
|
|
}
|
2006-10-17 23:34:12 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
this->SendPacket(&packet, client_addr);
|
2004-12-13 19:55:31 +00:00
|
|
|
}
|
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
/**
|
|
|
|
* A client has requested the names of some NewGRFs.
|
|
|
|
*
|
|
|
|
* Replying this can be tricky as we have a limit of SEND_MTU bytes
|
|
|
|
* in the reply packet and we can send up to 100 bytes per NewGRF
|
|
|
|
* (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
|
|
|
|
* As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it
|
|
|
|
* could be that a packet overflows. To stop this we only reply
|
|
|
|
* with the first N NewGRFs so that if the first N + 1 NewGRFs
|
|
|
|
* would be sent, the packet overflows.
|
|
|
|
* in_reply and in_reply_count are used to keep a list of GRFs to
|
|
|
|
* send in the reply.
|
|
|
|
*/
|
2011-05-01 12:18:34 +00:00
|
|
|
void ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
|
2006-12-18 12:26:55 +00:00
|
|
|
{
|
|
|
|
uint8 num_grfs;
|
|
|
|
uint i;
|
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
struct GRFInfo {
|
|
|
|
GRFIdentifier ident;
|
|
|
|
const char *name = nullptr;
|
|
|
|
};
|
|
|
|
std::vector<GRFInfo> in_reply;
|
|
|
|
|
2008-05-08 13:20:54 +00:00
|
|
|
size_t packet_len = 0;
|
2006-12-18 12:26:55 +00:00
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
num_grfs = p->Recv_uint8 ();
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 6, "[udp] newgrf data request (%u) from %s", num_grfs, NetworkAddressDumper().GetAddressAsString(client_addr));
|
2018-06-11 05:26:25 +00:00
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
|
|
|
|
|
2018-06-11 05:26:25 +00:00
|
|
|
auto flush_response = [&]() {
|
|
|
|
if (in_reply.empty()) return;
|
|
|
|
|
|
|
|
Packet packet(PACKET_UDP_SERVER_NEWGRFS);
|
|
|
|
packet.Send_uint8(in_reply.size());
|
|
|
|
for (const GRFInfo &info : in_reply) {
|
|
|
|
char name[NETWORK_GRF_NAME_LENGTH];
|
|
|
|
|
|
|
|
/* The name could be an empty string, if so take the filename */
|
|
|
|
strecpy(name, info.name, lastof(name));
|
|
|
|
this->SendGRFIdentifier(&packet, &info.ident);
|
|
|
|
packet.Send_string(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->SendPacket(&packet, client_addr);
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 6, "[udp] sent newgrf data response (%u of %u) to %s", (uint) in_reply.size(), num_grfs, NetworkAddressDumper().GetAddressAsString(client_addr));
|
2018-06-11 05:26:25 +00:00
|
|
|
|
|
|
|
packet_len = 0;
|
|
|
|
in_reply.clear();
|
|
|
|
};
|
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
for (i = 0; i < num_grfs; i++) {
|
2018-05-12 08:11:41 +00:00
|
|
|
GRFInfo info;
|
|
|
|
this->ReceiveGRFIdentifier(p, &info.ident);
|
2006-12-18 12:26:55 +00:00
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
if (memcmp(info.ident.md5sum, _out_of_band_grf_md5, 16) == 0) {
|
|
|
|
if (info.ident.grfid == 0x56D2B000) {
|
|
|
|
info.name = "=*=*= More than 62 GRFs in total =*=*=";
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const GRFConfig *f;
|
|
|
|
/* Find the matching GRF file */
|
|
|
|
f = FindGRFConfig(info.ident.grfid, FGCM_EXACT, info.ident.md5sum);
|
2019-04-11 17:14:13 +00:00
|
|
|
if (f == nullptr) continue; // The GRF is unknown to this server
|
2018-05-12 08:11:41 +00:00
|
|
|
|
|
|
|
info.ident = f->ident;
|
|
|
|
info.name = f->GetName();
|
|
|
|
}
|
2006-12-18 12:26:55 +00:00
|
|
|
|
|
|
|
/* If the reply might exceed the size of the packet, only reply
|
2006-12-18 17:29:59 +00:00
|
|
|
* the current list and do not send the other data.
|
|
|
|
* The name could be an empty string, if so take the filename. */
|
2018-06-11 05:26:25 +00:00
|
|
|
size_t required_length = sizeof(info.ident.grfid) + sizeof(info.ident.md5sum) +
|
2021-02-01 17:07:34 +00:00
|
|
|
std::min(strlen(info.name) + 1, (size_t)NETWORK_GRF_NAME_LENGTH);
|
2019-10-31 20:40:07 +00:00
|
|
|
if (packet_len + required_length > SEND_MTU_SHORT - 4) { // 4 is 3 byte header + grf count in reply
|
2018-06-11 05:26:25 +00:00
|
|
|
flush_response();
|
2006-12-18 12:26:55 +00:00
|
|
|
}
|
2018-06-11 05:26:25 +00:00
|
|
|
packet_len += required_length;
|
2018-05-12 08:11:41 +00:00
|
|
|
|
|
|
|
in_reply.push_back(info);
|
2006-12-18 12:26:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 05:26:25 +00:00
|
|
|
flush_response();
|
2006-12-18 12:26:55 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 14:30:01 +00:00
|
|
|
///*** Communication with servers (we are client) ***/
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Helper class for handling all client side communication. */
|
2007-01-12 14:30:01 +00:00
|
|
|
class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
|
2018-05-12 08:11:41 +00:00
|
|
|
private:
|
|
|
|
void Receive_SERVER_RESPONSE_Common(Packet *p, NetworkAddress *client_addr, bool extended);
|
2007-01-12 14:30:01 +00:00
|
|
|
protected:
|
2019-03-24 16:24:06 +00:00
|
|
|
void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override;
|
2019-03-27 18:12:04 +00:00
|
|
|
void Receive_EX_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override;
|
2019-03-24 16:24:06 +00:00
|
|
|
void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr) override;
|
|
|
|
void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr) override;
|
|
|
|
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) override;
|
2007-01-12 14:30:01 +00:00
|
|
|
public:
|
|
|
|
virtual ~ClientNetworkUDPSocketHandler() {}
|
|
|
|
};
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
|
2018-05-12 08:11:41 +00:00
|
|
|
{
|
|
|
|
this->Receive_SERVER_RESPONSE_Common(p, client_addr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkUDPSocketHandler::Receive_EX_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
|
|
|
|
{
|
|
|
|
this->Receive_SERVER_RESPONSE_Common(p, client_addr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE_Common(Packet *p, NetworkAddress *client_addr, bool extended)
|
2007-01-12 14:30:01 +00:00
|
|
|
{
|
|
|
|
NetworkGameList *item;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Just a fail-safe.. should never happen */
|
2007-01-12 14:30:01 +00:00
|
|
|
if (_network_udp_server) return;
|
|
|
|
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 4, "[udp]%s server response from %s", extended ? " extended" : "", NetworkAddressDumper().GetAddressAsString(client_addr));
|
2007-01-12 14:30:01 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Find next item */
|
2009-04-02 20:17:46 +00:00
|
|
|
item = NetworkGameListAddItem(*client_addr);
|
2007-01-12 14:30:01 +00:00
|
|
|
|
2010-01-06 20:49:24 +00:00
|
|
|
ClearGRFConfigList(&item->info.grfconfig);
|
2018-05-12 08:11:41 +00:00
|
|
|
if (extended) {
|
|
|
|
this->ReceiveNetworkGameInfoExtended(p, &item->info);
|
|
|
|
} else {
|
|
|
|
this->ReceiveNetworkGameInfo(p, &item->info);
|
|
|
|
}
|
2007-01-12 14:30:01 +00:00
|
|
|
|
|
|
|
item->info.compatible = true;
|
|
|
|
{
|
|
|
|
/* Checks whether there needs to be a request for names of GRFs and makes
|
|
|
|
* the request if necessary. GRFs that need to be requested are the GRFs
|
|
|
|
* that do not exist on the clients system and we do not have the name
|
|
|
|
* resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
|
|
|
|
* The in_request array and in_request_count are used so there is no need
|
|
|
|
* to do a second loop over the GRF list, which can be relatively expensive
|
|
|
|
* due to the string comparisons. */
|
|
|
|
const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
|
|
|
|
const GRFConfig *c;
|
|
|
|
uint in_request_count = 0;
|
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
auto flush_request = [&]() {
|
2007-01-12 14:30:01 +00:00
|
|
|
/* There are 'unknown' GRFs, now send a request for them */
|
|
|
|
uint i;
|
2007-02-01 22:30:35 +00:00
|
|
|
Packet packet(PACKET_UDP_CLIENT_GET_NEWGRFS);
|
2007-01-12 14:30:01 +00:00
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
packet.Send_uint8(in_request_count);
|
2007-01-12 14:30:01 +00:00
|
|
|
for (i = 0; i < in_request_count; i++) {
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendGRFIdentifier(&packet, &in_request[i]->ident);
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-02 20:17:46 +00:00
|
|
|
this->SendPacket(&packet, &item->address);
|
2018-05-12 08:11:41 +00:00
|
|
|
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 4, "[udp] sent newgrf data request (%u) to %s", in_request_count, NetworkAddressDumper().GetAddressAsString(client_addr));
|
2018-06-11 05:26:25 +00:00
|
|
|
|
2018-05-12 08:11:41 +00:00
|
|
|
in_request_count = 0;
|
|
|
|
};
|
|
|
|
|
2019-04-11 17:14:13 +00:00
|
|
|
for (c = item->info.grfconfig; c != nullptr; c = c->next) {
|
2018-05-12 08:11:41 +00:00
|
|
|
if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
|
|
|
|
if (c->status != GCS_NOT_FOUND || strcmp(c->GetName(), UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
|
|
|
|
in_request[in_request_count] = c;
|
|
|
|
in_request_count++;
|
2019-10-31 20:40:07 +00:00
|
|
|
if (in_request_count == NETWORK_MAX_GRF_COUNT_SHORT) flush_request();
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
2018-05-12 08:11:41 +00:00
|
|
|
|
|
|
|
if (in_request_count > 0) flush_request();
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-08 23:41:48 +00:00
|
|
|
if (item->info.hostname[0] == '\0') {
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(item->info.hostname, lastof(item->info.hostname), "%s", client_addr->GetHostname());
|
2009-04-08 23:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (client_addr->GetAddress()->ss_family == AF_INET6) {
|
|
|
|
strecat(item->info.server_name, " (IPv6)", lastof(item->info.server_name));
|
|
|
|
}
|
2007-01-12 14:30:01 +00:00
|
|
|
|
|
|
|
/* Check if we are allowed on this server based on the revision-match */
|
2018-05-12 08:11:41 +00:00
|
|
|
item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision, extended);
|
2007-01-12 14:30:01 +00:00
|
|
|
item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
|
|
|
|
|
|
|
|
item->online = true;
|
|
|
|
|
2012-03-25 19:46:59 +00:00
|
|
|
UpdateNetworkGameWindow();
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 12:18:34 +00:00
|
|
|
void ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr)
|
2007-01-12 14:30:01 +00:00
|
|
|
{
|
|
|
|
/* packet begins with the protocol version (uint8)
|
|
|
|
* then an uint16 which indicates how many
|
|
|
|
* ip:port pairs are in this packet, after that
|
2009-04-08 17:51:04 +00:00
|
|
|
* an uint32 (ip) and an uint16 (port) for each pair.
|
2007-01-12 14:30:01 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-08 17:51:04 +00:00
|
|
|
ServerListType type = (ServerListType)(p->Recv_uint8() - 1);
|
2007-01-12 14:30:01 +00:00
|
|
|
|
2009-04-08 17:51:04 +00:00
|
|
|
if (type < SLT_END) {
|
2009-01-20 01:32:06 +00:00
|
|
|
for (int i = p->Recv_uint16(); i != 0 ; i--) {
|
2009-04-08 23:41:48 +00:00
|
|
|
sockaddr_storage addr_storage;
|
|
|
|
memset(&addr_storage, 0, sizeof(addr_storage));
|
|
|
|
|
|
|
|
if (type == SLT_IPv4) {
|
|
|
|
addr_storage.ss_family = AF_INET;
|
|
|
|
((sockaddr_in*)&addr_storage)->sin_addr.s_addr = TO_LE32(p->Recv_uint32());
|
|
|
|
} else {
|
|
|
|
assert(type == SLT_IPv6);
|
|
|
|
addr_storage.ss_family = AF_INET6;
|
|
|
|
byte *addr = (byte*)&((sockaddr_in6*)&addr_storage)->sin6_addr;
|
|
|
|
for (uint i = 0; i < sizeof(in6_addr); i++) *addr++ = p->Recv_uint8();
|
|
|
|
}
|
|
|
|
NetworkAddress addr(addr_storage, type == SLT_IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
|
|
|
|
addr.SetPort(p->Recv_uint16());
|
2007-01-12 20:19:49 +00:00
|
|
|
|
|
|
|
/* Somehow we reached the end of the packet */
|
|
|
|
if (this->HasClientQuit()) return;
|
2009-04-08 23:41:48 +00:00
|
|
|
|
2019-03-17 00:59:46 +00:00
|
|
|
DoNetworkUDPQueryServer(addr, false, false);
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
/** The return of the client's request of the names of some NewGRFs */
|
2011-05-01 12:18:34 +00:00
|
|
|
void ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr)
|
2006-12-18 12:26:55 +00:00
|
|
|
{
|
|
|
|
uint8 num_grfs;
|
|
|
|
uint i;
|
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
num_grfs = p->Recv_uint8 ();
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 6, "[udp] newgrf data reply (%u) from %s", num_grfs, NetworkAddressDumper().GetAddressAsString(client_addr));
|
2018-06-11 05:26:25 +00:00
|
|
|
|
2006-12-18 12:26:55 +00:00
|
|
|
if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
|
|
|
|
|
|
|
|
for (i = 0; i < num_grfs; i++) {
|
|
|
|
char name[NETWORK_GRF_NAME_LENGTH];
|
2010-02-27 20:26:42 +00:00
|
|
|
GRFIdentifier c;
|
2006-12-18 12:26:55 +00:00
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->ReceiveGRFIdentifier(p, &c);
|
2007-02-01 23:26:44 +00:00
|
|
|
p->Recv_string(name, sizeof(name));
|
2006-12-18 12:26:55 +00:00
|
|
|
|
2006-12-18 17:29:59 +00:00
|
|
|
/* An empty name is not possible under normal circumstances
|
|
|
|
* and causes problems when showing the NewGRF list. */
|
2007-01-13 15:00:16 +00:00
|
|
|
if (StrEmpty(name)) continue;
|
2006-12-18 17:29:59 +00:00
|
|
|
|
2011-03-03 18:47:46 +00:00
|
|
|
/* Try to find the GRFTextWrapper for the name of this GRF ID and MD5sum tuple.
|
2006-12-18 12:26:55 +00:00
|
|
|
* If it exists and not resolved yet, then name of the fake GRF is
|
|
|
|
* overwritten with the name from the reply. */
|
2020-05-17 21:31:47 +00:00
|
|
|
GRFTextWrapper unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
|
|
|
|
if (unknown_name && strcmp(GetGRFStringFromGRFText(unknown_name), UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
|
|
|
|
AddGRFTextToList(unknown_name, name);
|
2006-12-18 12:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 14:30:01 +00:00
|
|
|
void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2007-01-12 14:30:01 +00:00
|
|
|
/* Find the matching GRF file */
|
2010-10-17 12:12:13 +00:00
|
|
|
const GRFConfig *f = FindGRFConfig(config->ident.grfid, FGCM_EXACT, config->ident.md5sum);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) {
|
2007-01-12 14:30:01 +00:00
|
|
|
/* Don't know the GRF, so mark game incompatible and the (possibly)
|
|
|
|
* already resolved name for this GRF (another server has sent the
|
|
|
|
* name of the GRF already */
|
2011-03-03 18:47:46 +00:00
|
|
|
config->name = FindUnknownGRFName(config->ident.grfid, config->ident.md5sum, true);
|
2007-04-04 12:03:10 +00:00
|
|
|
config->status = GCS_NOT_FOUND;
|
2006-04-14 12:19:51 +00:00
|
|
|
} else {
|
2011-03-03 18:47:46 +00:00
|
|
|
config->filename = f->filename;
|
|
|
|
config->name = f->name;
|
|
|
|
config->info = f->info;
|
2011-12-11 12:55:04 +00:00
|
|
|
config->url = f->url;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(config->flags, GCF_COPY);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Broadcast to all ips */
|
2007-01-12 14:30:01 +00:00
|
|
|
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2019-02-17 11:20:52 +00:00
|
|
|
for (NetworkAddress &addr : _broadcast_list) {
|
2018-05-12 08:11:41 +00:00
|
|
|
Packet p = PrepareUdpClientFindServerPacket();
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2019-02-17 11:20:52 +00:00
|
|
|
DEBUG(net, 4, "[udp] broadcasting to %s", addr.GetHostname());
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2019-02-17 11:20:52 +00:00
|
|
|
socket->SendPacket(&p, &addr, true, true);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2004-12-22 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Request the the server-list from the master server */
|
2007-03-07 11:47:46 +00:00
|
|
|
void NetworkUDPQueryMasterServer()
|
2004-12-22 18:42:56 +00:00
|
|
|
{
|
2007-02-01 22:30:35 +00:00
|
|
|
Packet p(PACKET_UDP_CLIENT_GET_LIST);
|
2009-04-02 19:21:26 +00:00
|
|
|
NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
|
2004-12-22 18:42:56 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* packet only contains protocol version */
|
2007-02-01 23:26:44 +00:00
|
|
|
p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
|
2009-04-08 17:51:04 +00:00
|
|
|
p.Send_uint8(SLT_AUTODETECT);
|
2004-12-22 18:42:56 +00:00
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_udp_client.mutex);
|
|
|
|
_udp_client.socket->SendPacket(&p, &out_addr, true);
|
2004-12-22 18:42:56 +00:00
|
|
|
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 2, "[udp] master server queried at %s", NetworkAddressDumper().GetAddressAsString(&out_addr));
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Find all servers */
|
2007-03-07 11:47:46 +00:00
|
|
|
void NetworkUDPSearchGame()
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* We are still searching.. */
|
2006-12-26 17:36:18 +00:00
|
|
|
if (_network_udp_broadcast > 0) return;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(net, 0, "[udp] searching server");
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
NetworkUDPBroadCast(_udp_client.socket);
|
2004-12-04 17:54:56 +00:00
|
|
|
_network_udp_broadcast = 300; // Stay searching for 300 ticks
|
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/**
|
|
|
|
* Thread entry point for de-advertising.
|
|
|
|
*/
|
2019-03-17 00:59:46 +00:00
|
|
|
static void NetworkUDPRemoveAdvertiseThread()
|
2009-01-20 03:44:43 +00:00
|
|
|
{
|
|
|
|
DEBUG(net, 1, "[udp] removing advertise from master server");
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2009-01-20 03:44:43 +00:00
|
|
|
/* Find somewhere to send */
|
2009-04-02 19:21:26 +00:00
|
|
|
NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
|
2009-01-20 03:44:43 +00:00
|
|
|
|
|
|
|
/* Send the packet */
|
|
|
|
Packet p(PACKET_UDP_SERVER_UNREGISTER);
|
|
|
|
/* Packet is: Version, server_port */
|
|
|
|
p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
|
|
|
|
p.Send_uint16(_settings_client.network.server_port);
|
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_udp_master.mutex);
|
|
|
|
if (_udp_master.socket != nullptr) _udp_master.socket->SendPacket(&p, &out_addr, true);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 20:18:48 +00:00
|
|
|
/**
|
|
|
|
* Remove our advertise from the master-server.
|
|
|
|
* @param blocking whether to wait until the removal has finished.
|
|
|
|
*/
|
|
|
|
void NetworkUDPRemoveAdvertise(bool blocking)
|
2004-12-22 18:56:52 +00:00
|
|
|
{
|
|
|
|
/* Check if we are advertising */
|
2006-12-26 17:36:18 +00:00
|
|
|
if (!_networking || !_network_server || !_network_udp_server) return;
|
2004-12-22 18:56:52 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (blocking || !StartNewThread(nullptr, "ottd:udp-advert", &NetworkUDPRemoveAdvertiseThread)) {
|
2019-03-17 00:59:46 +00:00
|
|
|
NetworkUDPRemoveAdvertiseThread();
|
2009-01-20 03:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
2004-12-22 18:56:52 +00:00
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/**
|
|
|
|
* Thread entry point for advertising.
|
|
|
|
*/
|
2019-03-17 00:59:46 +00:00
|
|
|
static void NetworkUDPAdvertiseThread()
|
2009-01-20 03:44:43 +00:00
|
|
|
{
|
2004-12-22 18:56:52 +00:00
|
|
|
/* Find somewhere to send */
|
2009-04-02 19:21:26 +00:00
|
|
|
NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
|
2004-12-22 18:56:52 +00:00
|
|
|
|
2009-01-20 03:44:43 +00:00
|
|
|
DEBUG(net, 1, "[udp] advertising to master server");
|
|
|
|
|
2009-05-06 09:52:52 +00:00
|
|
|
/* Add a bit more messaging when we cannot get a session key */
|
|
|
|
static byte session_key_retries = 0;
|
|
|
|
if (_session_key == 0 && session_key_retries++ == 2) {
|
|
|
|
DEBUG(net, 0, "[udp] advertising to the master server is failing");
|
|
|
|
DEBUG(net, 0, "[udp] we are not receiving the session key from the server");
|
2020-05-06 22:23:03 +00:00
|
|
|
DEBUG(net, 0, "[udp] please allow udp packets from %s to you to be delivered", NetworkAddressDumper().GetAddressAsString(&out_addr, false));
|
|
|
|
DEBUG(net, 0, "[udp] please allow udp packets from you to %s to be delivered", NetworkAddressDumper().GetAddressAsString(&out_addr, false));
|
2009-05-06 09:52:52 +00:00
|
|
|
}
|
|
|
|
if (_session_key != 0 && _network_advertise_retries == 0) {
|
|
|
|
DEBUG(net, 0, "[udp] advertising to the master server is failing");
|
|
|
|
DEBUG(net, 0, "[udp] we are not receiving the acknowledgement from the server");
|
|
|
|
DEBUG(net, 0, "[udp] this usually means that the master server cannot reach us");
|
2009-05-10 13:23:23 +00:00
|
|
|
DEBUG(net, 0, "[udp] please allow udp and tcp packets to port %u to be delivered", _settings_client.network.server_port);
|
|
|
|
DEBUG(net, 0, "[udp] please allow udp and tcp packets from port %u to be delivered", _settings_client.network.server_port);
|
2009-05-06 09:52:52 +00:00
|
|
|
}
|
|
|
|
|
2004-12-22 18:56:52 +00:00
|
|
|
/* Send the packet */
|
2009-01-20 03:44:43 +00:00
|
|
|
Packet p(PACKET_UDP_SERVER_REGISTER);
|
|
|
|
/* Packet is: WELCOME_MESSAGE, Version, server_port */
|
|
|
|
p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
|
2007-02-01 23:26:44 +00:00
|
|
|
p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
|
2008-05-29 20:21:28 +00:00
|
|
|
p.Send_uint16(_settings_client.network.server_port);
|
2009-04-05 19:43:41 +00:00
|
|
|
p.Send_uint64(_session_key);
|
2009-01-20 03:44:43 +00:00
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_udp_master.mutex);
|
|
|
|
if (_udp_master.socket != nullptr) _udp_master.socket->SendPacket(&p, &out_addr, true);
|
2004-12-22 18:56:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/**
|
|
|
|
* Register us to the master server
|
|
|
|
* This function checks if it needs to send an advertise
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void NetworkUDPAdvertise()
|
2004-12-15 20:10:34 +00:00
|
|
|
{
|
2021-02-25 19:30:16 +00:00
|
|
|
static std::chrono::steady_clock::time_point _last_advertisement = {}; ///< The last time we performed an advertisement.
|
2015-09-19 16:49:46 +00:00
|
|
|
|
2004-12-15 20:10:34 +00:00
|
|
|
/* Check if we should send an advertise */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise) return;
|
2004-12-15 20:10:34 +00:00
|
|
|
|
2021-02-25 19:30:16 +00:00
|
|
|
if (_network_need_advertise) {
|
|
|
|
/* Forced advertisement. */
|
2006-01-19 17:50:40 +00:00
|
|
|
_network_need_advertise = false;
|
2004-12-22 18:42:56 +00:00
|
|
|
_network_advertise_retries = ADVERTISE_RETRY_TIMES;
|
2006-01-19 17:50:40 +00:00
|
|
|
} else {
|
|
|
|
/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
|
|
|
|
if (_network_advertise_retries == 0) {
|
2021-02-25 19:30:16 +00:00
|
|
|
if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_NORMAL_INTERVAL) return;
|
2010-07-24 10:14:39 +00:00
|
|
|
|
2006-01-19 17:50:40 +00:00
|
|
|
_network_advertise_retries = ADVERTISE_RETRY_TIMES;
|
2015-09-19 16:49:46 +00:00
|
|
|
} else {
|
|
|
|
/* An actual retry. */
|
2021-02-25 19:30:16 +00:00
|
|
|
if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_RETRY_INTERVAL) return;
|
2006-01-19 17:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
2004-12-22 18:42:56 +00:00
|
|
|
|
|
|
|
_network_advertise_retries--;
|
2021-02-25 19:30:16 +00:00
|
|
|
_last_advertisement = std::chrono::steady_clock::now();
|
2004-12-15 20:10:34 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!StartNewThread(nullptr, "ottd:udp-advert", &NetworkUDPAdvertiseThread)) {
|
2019-03-17 00:59:46 +00:00
|
|
|
NetworkUDPAdvertiseThread();
|
2009-01-20 03:44:43 +00:00
|
|
|
}
|
2004-12-15 20:10:34 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Initialize the whole UDP bit. */
|
2007-03-07 11:47:46 +00:00
|
|
|
void NetworkUDPInitialize()
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2009-04-08 21:03:00 +00:00
|
|
|
/* If not closed, then do it. */
|
2021-04-11 09:30:44 +00:00
|
|
|
if (_udp_server.socket != nullptr) NetworkUDPClose();
|
2009-04-08 21:03:00 +00:00
|
|
|
|
2009-04-08 19:36:51 +00:00
|
|
|
DEBUG(net, 1, "[udp] initializing listeners");
|
2021-04-11 09:30:44 +00:00
|
|
|
assert(_udp_client.socket == nullptr && _udp_server.socket == nullptr && _udp_master.socket == nullptr);
|
2008-12-23 20:58:03 +00:00
|
|
|
|
2021-04-18 19:33:17 +00:00
|
|
|
// std::scoped_lock lock(_udp_client.mutex, _udp_server.mutex, _udp_master.mutex);
|
|
|
|
|
|
|
|
/* Avoid std::scoped_lock for MacOS 10.12 compatibility */
|
|
|
|
std::unique_lock<std::mutex> lock1(_udp_client.mutex, std::defer_lock);
|
|
|
|
std::unique_lock<std::mutex> lock2(_udp_server.mutex, std::defer_lock);
|
|
|
|
std::unique_lock<std::mutex> lock3(_udp_master.mutex, std::defer_lock);
|
|
|
|
std::lock(lock1, lock2, lock3);
|
2009-04-07 20:27:13 +00:00
|
|
|
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_client.socket = new ClientNetworkUDPSocketHandler();
|
2009-04-10 12:56:55 +00:00
|
|
|
|
|
|
|
NetworkAddressList server;
|
|
|
|
GetBindAddresses(&server, _settings_client.network.server_port);
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_server.socket = new ServerNetworkUDPSocketHandler(&server);
|
2009-04-08 19:36:51 +00:00
|
|
|
|
2018-09-20 22:44:14 +00:00
|
|
|
server.clear();
|
2009-04-10 12:56:55 +00:00
|
|
|
GetBindAddresses(&server, 0);
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_master.socket = new MasterNetworkUDPSocketHandler(&server);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
_network_udp_server = false;
|
|
|
|
_network_udp_broadcast = 0;
|
2021-04-11 09:22:50 +00:00
|
|
|
_network_advertise_retries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Start the listening of the UDP server component. */
|
|
|
|
void NetworkUDPServerListen()
|
|
|
|
{
|
2021-04-11 09:30:44 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_udp_server.mutex);
|
|
|
|
_network_udp_server = _udp_server.socket->Listen();
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Close all UDP related stuff. */
|
2009-04-08 19:36:51 +00:00
|
|
|
void NetworkUDPClose()
|
2007-01-12 14:30:01 +00:00
|
|
|
{
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_client.Close();
|
|
|
|
_udp_server.Close();
|
|
|
|
_udp_master.Close();
|
2009-04-08 19:36:51 +00:00
|
|
|
|
|
|
|
_network_udp_server = false;
|
|
|
|
_network_udp_broadcast = 0;
|
|
|
|
DEBUG(net, 1, "[udp] closed listeners");
|
2007-01-12 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 22:08:43 +00:00
|
|
|
/** Receive the UDP packets. */
|
|
|
|
void NetworkBackgroundUDPLoop()
|
|
|
|
{
|
|
|
|
if (_network_udp_server) {
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_server.ReceivePackets();
|
|
|
|
_udp_master.ReceivePackets();
|
2012-01-04 22:08:43 +00:00
|
|
|
} else {
|
2021-04-11 09:30:44 +00:00
|
|
|
_udp_client.ReceivePackets();
|
2012-01-04 22:08:43 +00:00
|
|
|
if (_network_udp_broadcast > 0) _network_udp_broadcast--;
|
|
|
|
}
|
|
|
|
}
|