# network_udp.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00017 #ifdef ENABLE_NETWORK
    00018
    00019 #include "../stdafx.h"
    00020 #include "../date_func.h"
    00021 #include "../map_func.h"
    00022 #include "../debug.h"
    00023 #include "network_gamelist.h"
    00024 #include "network_internal.h"
    00025 #include "network_udp.h"
    00026 #include "network.h"
    00027 #include "../core/endian_func.hpp"
    00028 #include "../company_base.h"
    00029 #include "../thread/thread.h"
    00030 #include "../rev.h"
    00031 #include "../newgrf_text.h"
    00032 #include "../strings_func.h"
    00033 #include "table/strings.h"
    00034
    00035 #include "core/udp.h"
    00036
    00037 static ThreadMutex *_network_udp_mutex = ThreadMutex::New();
    00038
    00040 static uint64 _session_key = 0;
    00041
    00042 static const uint ADVERTISE_NORMAL_INTERVAL = 30000;
    00043 static const uint ADVERTISE_RETRY_INTERVAL  =   300;
    00044 static const uint ADVERTISE_RETRY_TIMES     =     3;
    00045
    00046 NetworkUDPSocketHandler *_udp_client_socket = NULL;
    00047 NetworkUDPSocketHandler *_udp_server_socket = NULL;
    00048 NetworkUDPSocketHandler *_udp_master_socket = NULL;
    00049
    00051
    00052 class MasterNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
    00053 protected:
    00054   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
    00055   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_SESSION_KEY);
    00056 public:
    00057   MasterNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
    00058   virtual ~MasterNetworkUDPSocketHandler() {}
    00059 };
    00060
    00061 DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_ACK_REGISTER)
    00062 {
    00063   _network_advertise_retries = 0;
    00064   DEBUG(net, 2, "[udp] advertising on master server successful (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
    00065
    00066   /* We are advertised, but we don't want to! */
    00067   if (!_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(false);
    00068 }
    00069
    00070 DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_SESSION_KEY)
    00071 {
    00072   _session_key = p->Recv_uint64();
    00073   DEBUG(net, 2, "[udp] received new session key from master server (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
    00074 }
    00075
    00077
    00078 class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
    00079 protected:
    00080   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
    00081   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
    00082   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00083 public:
    00084   ServerNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
    00085   virtual ~ServerNetworkUDPSocketHandler() {}
    00086 };
    00087
    00088 DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER)
    00089 {
    00090   /* Just a fail-safe.. should never happen */
    00091   if (!_network_udp_server) {
    00092     return;
    00093   }
    00094
    00095   NetworkGameInfo ngi;
    00096
    00097   /* Update some game_info */
    00098   ngi.clients_on     = _network_game_info.clients_on;
    00099   ngi.start_date     = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
    00100
    00101   ngi.server_lang    = _settings_client.network.server_lang;
    00102   ngi.use_password   = !StrEmpty(_settings_client.network.server_password);
    00103   ngi.clients_max    = _settings_client.network.max_clients;
    00104   ngi.companies_on   = (byte)Company::GetNumItems();
    00105   ngi.companies_max  = _settings_client.network.max_companies;
    00106   ngi.spectators_on  = NetworkSpectatorCount();
    00107   ngi.spectators_max = _settings_client.network.max_spectators;
    00108   ngi.game_date      = _date;
    00109   ngi.map_width      = MapSizeX();
    00110   ngi.map_height     = MapSizeY();
    00111   ngi.map_set        = _settings_game.game_creation.landscape;
    00112   ngi.dedicated      = _network_dedicated;
    00113   ngi.grfconfig      = _grfconfig;
    00114
    00115   strecpy(ngi.map_name, _network_game_info.map_name, lastof(ngi.map_name));
    00116   strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
    00117   strecpy(ngi.server_revision, _openttd_revision, lastof(ngi.server_revision));
    00118
    00119   Packet packet(PACKET_UDP_SERVER_RESPONSE);
    00120   this->SendNetworkGameInfo(&packet, &ngi);
    00121
    00122   /* Let the client know that we are here */
    00123   this->SendPacket(&packet, client_addr);
    00124
    00125   DEBUG(net, 2, "[udp] queried from %s", client_addr->GetHostname());
    00126 }
    00127
    00128 DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
    00129 {
    00130   /* Just a fail-safe.. should never happen */
    00131   if (!_network_udp_server) return;
    00132
    00133   Packet packet(PACKET_UDP_SERVER_DETAIL_INFO);
    00134
    00135   /* Send the amount of active companies */
    00136   packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
    00137   packet.Send_uint8 ((uint8)Company::GetNumItems());
    00138
    00139   /* Fetch the latest version of the stats */
    00140   NetworkCompanyStats company_stats[MAX_COMPANIES];
    00141   NetworkPopulateCompanyStats(company_stats);
    00142
    00143   /* The minimum company information "blob" size. */
    00144   static const uint MIN_CI_SIZE = 54;
    00145   uint max_cname_length = NETWORK_COMPANY_NAME_LENGTH;
    00146
    00147   if (Company::GetNumItems() * (MIN_CI_SIZE + NETWORK_COMPANY_NAME_LENGTH) >= (uint)SEND_MTU - packet.size) {
    00148     /* Assume we can at least put the company information in the packets. */
    00149     assert(Company::GetNumItems() * MIN_CI_SIZE < (uint)SEND_MTU - packet.size);
    00150
    00151     /* At this moment the company names might not fit in the
    00152      * packet. Check whether that is really the case. */
    00153
    00154     for (;;) {
    00155       int free = SEND_MTU - packet.size;
    00156       Company *company;
    00157       FOR_ALL_COMPANIES(company) {
    00158         char company_name[NETWORK_COMPANY_NAME_LENGTH];
    00159         SetDParam(0, company->index);
    00160         GetString(company_name, STR_COMPANY_NAME, company_name + max_cname_length - 1);
    00161         free -= MIN_CI_SIZE;
    00162         free -= (int)strlen(company_name);
    00163       }
    00164       if (free >= 0) break;
    00165
    00166       /* Try again, with slightly shorter strings. */
    00167       assert(max_cname_length > 0);
    00168       max_cname_length--;
    00169     }
    00170   }
    00171
    00172   Company *company;
    00173   /* Go through all the companies */
    00174   FOR_ALL_COMPANIES(company) {
    00175     /* Send the information */
    00176     this->SendCompanyInformation(&packet, company, &company_stats[company->index], max_cname_length);
    00177   }
    00178
    00179   this->SendPacket(&packet, client_addr);
    00180 }
    00181
    00195 DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
    00196 {
    00197   uint8 num_grfs;
    00198   uint i;
    00199
    00200   const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
    00201   uint8 in_reply_count = 0;
    00202   size_t packet_len = 0;
    00203
    00204   DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString());
    00205
    00206   num_grfs = p->Recv_uint8 ();
    00207   if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
    00208
    00209   for (i = 0; i < num_grfs; i++) {
    00210     GRFIdentifier c;
    00211     const GRFConfig *f;
    00212
    00213     this->ReceiveGRFIdentifier(p, &c);
    00214
    00215     /* Find the matching GRF file */
    00216     f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
    00217     if (f == NULL) continue; // The GRF is unknown to this server
    00218
    00219     /* If the reply might exceed the size of the packet, only reply
    00220      * the current list and do not send the other data.
    00221      * The name could be an empty string, if so take the filename. */
    00222     packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
    00223         min(strlen(f->GetName()) + 1, (size_t)NETWORK_GRF_NAME_LENGTH);
    00224     if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
    00225       break;
    00226     }
    00227     in_reply[in_reply_count] = f;
    00228     in_reply_count++;
    00229   }
    00230
    00231   if (in_reply_count == 0) return;
    00232
    00233   Packet packet(PACKET_UDP_SERVER_NEWGRFS);
    00234   packet.Send_uint8(in_reply_count);
    00235   for (i = 0; i < in_reply_count; i++) {
    00236     char name[NETWORK_GRF_NAME_LENGTH];
    00237
    00238     /* The name could be an empty string, if so take the filename */
    00239     strecpy(name, in_reply[i]->GetName(), lastof(name));
    00240     this->SendGRFIdentifier(&packet, &in_reply[i]->ident);
    00241     packet.Send_string(name);
    00242   }
    00243
    00244   this->SendPacket(&packet, client_addr);
    00245 }
    00246
    00248
    00249 class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
    00250 protected:
    00251   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
    00252   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
    00253   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
    00254   virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
    00255 public:
    00256   virtual ~ClientNetworkUDPSocketHandler() {}
    00257 };
    00258
    00259 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
    00260 {
    00261   NetworkGameList *item;
    00262
    00263   /* Just a fail-safe.. should never happen */
    00264   if (_network_udp_server) return;
    00265
    00266   DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString());
    00267
    00268   /* Find next item */
    00269   item = NetworkGameListAddItem(*client_addr);
    00270
    00271   ClearGRFConfigList(&item->info.grfconfig);
    00272   this->ReceiveNetworkGameInfo(p, &item->info);
    00273
    00274   item->info.compatible = true;
    00275   {
    00276     /* Checks whether there needs to be a request for names of GRFs and makes
    00277      * the request if necessary. GRFs that need to be requested are the GRFs
    00278      * that do not exist on the clients system and we do not have the name
    00279      * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
    00280      * The in_request array and in_request_count are used so there is no need
    00281      * to do a second loop over the GRF list, which can be relatively expensive
    00282      * due to the string comparisons. */
    00283     const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
    00284     const GRFConfig *c;
    00285     uint in_request_count = 0;
    00286
    00287     for (c = item->info.grfconfig; c != NULL; c = c->next) {
    00288       if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
    00289       if (c->status != GCS_NOT_FOUND || strcmp(c->GetName(), UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
    00290       in_request[in_request_count] = c;
    00291       in_request_count++;
    00292     }
    00293
    00294     if (in_request_count > 0) {
    00295       /* There are 'unknown' GRFs, now send a request for them */
    00296       uint i;
    00297       Packet packet(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00298
    00299       packet.Send_uint8(in_request_count);
    00300       for (i = 0; i < in_request_count; i++) {
    00301         this->SendGRFIdentifier(&packet, &in_request[i]->ident);
    00302       }
    00303
    00304       this->SendPacket(&packet, &item->address);
    00305     }
    00306   }
    00307
    00308   if (item->info.hostname[0] == '\0') {
    00309     snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", client_addr->GetHostname());
    00310   }
    00311
    00312   if (client_addr->GetAddress()->ss_family == AF_INET6) {
    00313     strecat(item->info.server_name, " (IPv6)", lastof(item->info.server_name));
    00314   }
    00315
    00316   /* Check if we are allowed on this server based on the revision-match */
    00317   item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision);
    00318   item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
    00319
    00320   item->online = true;
    00321
    00322   UpdateNetworkGameWindow(false);
    00323 }
    00324
    00325 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_MASTER_RESPONSE_LIST)
    00326 {
    00327   /* packet begins with the protocol version (uint8)
    00328    * then an uint16 which indicates how many
    00329    * ip:port pairs are in this packet, after that
    00330    * an uint32 (ip) and an uint16 (port) for each pair.
    00331    */
    00332
    00333   ServerListType type = (ServerListType)(p->Recv_uint8() - 1);
    00334
    00335   if (type < SLT_END) {
    00336     for (int i = p->Recv_uint16(); i != 0 ; i--) {
    00337       sockaddr_storage addr_storage;
    00338       memset(&addr_storage, 0, sizeof(addr_storage));
    00339
    00340       if (type == SLT_IPv4) {
    00341         addr_storage.ss_family = AF_INET;
    00342         ((sockaddr_in*)&addr_storage)->sin_addr.s_addr = TO_LE32(p->Recv_uint32());
    00343       } else {
    00344         assert(type == SLT_IPv6);
    00345         addr_storage.ss_family = AF_INET6;
    00346         byte *addr = (byte*)&((sockaddr_in6*)&addr_storage)->sin6_addr;
    00347         for (uint i = 0; i < sizeof(in6_addr); i++) *addr++ = p->Recv_uint8();
    00348       }
    00349       NetworkAddress addr(addr_storage, type == SLT_IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
    00350       addr.SetPort(p->Recv_uint16());
    00351
    00352       /* Somehow we reached the end of the packet */
    00353       if (this->HasClientQuit()) return;
    00354
    00355       NetworkUDPQueryServer(addr);
    00356     }
    00357   }
    00358 }
    00359
    00361 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
    00362 {
    00363   uint8 num_grfs;
    00364   uint i;
    00365
    00366   DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString());
    00367
    00368   num_grfs = p->Recv_uint8 ();
    00369   if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
    00370
    00371   for (i = 0; i < num_grfs; i++) {
    00372     char name[NETWORK_GRF_NAME_LENGTH];
    00373     GRFIdentifier c;
    00374
    00375     this->ReceiveGRFIdentifier(p, &c);
    00376     p->Recv_string(name, sizeof(name));
    00377
    00378     /* An empty name is not possible under normal circumstances
    00379      * and causes problems when showing the NewGRF list. */
    00380     if (StrEmpty(name)) continue;
    00381
    00382     /* Try to find the GRFTextWrapper for the name of this GRF ID and MD5sum tuple.
    00383      * If it exists and not resolved yet, then name of the fake GRF is
    00384      * overwritten with the name from the reply. */
    00385     GRFTextWrapper *unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
    00386     if (unknown_name != NULL && strcmp(GetGRFStringFromGRFText(unknown_name->text), UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
    00387       AddGRFTextToList(&unknown_name->text, name);
    00388     }
    00389   }
    00390 }
    00391
    00392 void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
    00393 {
    00394   /* Find the matching GRF file */
    00395   const GRFConfig *f = FindGRFConfig(config->ident.grfid, FGCM_EXACT, config->ident.md5sum);
    00396   if (f == NULL) {
    00397     /* Don't know the GRF, so mark game incompatible and the (possibly)
    00398      * already resolved name for this GRF (another server has sent the
    00399      * name of the GRF already */
    00400     config->name->Release();
    00401     config->name = FindUnknownGRFName(config->ident.grfid, config->ident.md5sum, true);
    00402     config->name->AddRef();
    00403     config->status = GCS_NOT_FOUND;
    00404   } else {
    00405     config->filename = f->filename;
    00406     config->name->Release();
    00407     config->name = f->name;
    00408     config->name->AddRef();
    00409     config->info->Release();
    00410     config->info = f->info;
    00411     config->info->AddRef();
    00412   }
    00413   SetBit(config->flags, GCF_COPY);
    00414 }
    00415
    00416 /* Broadcast to all ips */
    00417 static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
    00418 {
    00419   for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) {
    00420     Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
    00421
    00422     DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname());
    00423
    00424     socket->SendPacket(&p, addr, true, true);
    00425   }
    00426 }
    00427
    00428
    00429 /* Request the the server-list from the master server */
    00430 void NetworkUDPQueryMasterServer()
    00431 {
    00432   Packet p(PACKET_UDP_CLIENT_GET_LIST);
    00433   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00434
    00435   /* packet only contains protocol version */
    00436   p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
    00437   p.Send_uint8(SLT_AUTODETECT);
    00438
    00439   _udp_client_socket->SendPacket(&p, &out_addr, true);
    00440
    00441   DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString());
    00442 }
    00443
    00444 /* Find all servers */
    00445 void NetworkUDPSearchGame()
    00446 {
    00447   /* We are still searching.. */
    00448   if (_network_udp_broadcast > 0) return;
    00449
    00450   DEBUG(net, 0, "[udp] searching server");
    00451
    00452   NetworkUDPBroadCast(_udp_client_socket);
    00453   _network_udp_broadcast = 300; // Stay searching for 300 ticks
    00454 }
    00455
    00457 struct NetworkUDPQueryServerInfo : NetworkAddress {
    00458   bool manually;
    00459   NetworkUDPQueryServerInfo(const NetworkAddress &address, bool manually) :
    00460     NetworkAddress(address),
    00461     manually(manually)
    00462   {
    00463   }
    00464 };
    00465
    00470 static void NetworkUDPQueryServerThread(void *pntr)
    00471 {
    00472   NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
    00473
    00474   /* Clear item in gamelist */
    00475   NetworkGameList *item = CallocT<NetworkGameList>(1);
    00476   item->address = *info;
    00477   info->GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
    00478   strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname));
    00479   item->manually = info->manually;
    00480   NetworkGameListAddItemDelayed(item);
    00481
    00482   _network_udp_mutex->BeginCritical();
    00483   /* Init the packet */
    00484   Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
    00485   if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, info);
    00486   _network_udp_mutex->EndCritical();
    00487
    00488   delete info;
    00489 }
    00490
    00491 void NetworkUDPQueryServer(NetworkAddress address, bool manually)
    00492 {
    00493   NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
    00494   if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info)) {
    00495     NetworkUDPQueryServerThread(info);
    00496   }
    00497 }
    00498
    00499 static void NetworkUDPRemoveAdvertiseThread(void *pntr)
    00500 {
    00501   DEBUG(net, 1, "[udp] removing advertise from master server");
    00502
    00503   /* Find somewhere to send */
    00504   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00505
    00506   /* Send the packet */
    00507   Packet p(PACKET_UDP_SERVER_UNREGISTER);
    00508   /* Packet is: Version, server_port */
    00509   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
    00510   p.Send_uint16(_settings_client.network.server_port);
    00511
    00512   _network_udp_mutex->BeginCritical();
    00513   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
    00514   _network_udp_mutex->EndCritical();
    00515 }
    00516
    00521 void NetworkUDPRemoveAdvertise(bool blocking)
    00522 {
    00523   /* Check if we are advertising */
    00524   if (!_networking || !_network_server || !_network_udp_server) return;
    00525
    00526   if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL)) {
    00527     NetworkUDPRemoveAdvertiseThread(NULL);
    00528   }
    00529 }
    00530
    00531 static void NetworkUDPAdvertiseThread(void *pntr)
    00532 {
    00533   /* Find somewhere to send */
    00534   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00535
    00536   DEBUG(net, 1, "[udp] advertising to master server");
    00537
    00538   /* Add a bit more messaging when we cannot get a session key */
    00539   static byte session_key_retries = 0;
    00540   if (_session_key == 0 && session_key_retries++ == 2) {
    00541     DEBUG(net, 0, "[udp] advertising to the master server is failing");
    00542     DEBUG(net, 0, "[udp]   we are not receiving the session key from the server");
    00543     DEBUG(net, 0, "[udp]   please allow udp packets from %s to you to be delivered", out_addr.GetAddressAsString(false));
    00544     DEBUG(net, 0, "[udp]   please allow udp packets from you to %s to be delivered", out_addr.GetAddressAsString(false));
    00545   }
    00546   if (_session_key != 0 && _network_advertise_retries == 0) {
    00547     DEBUG(net, 0, "[udp] advertising to the master server is failing");
    00548     DEBUG(net, 0, "[udp]   we are not receiving the acknowledgement from the server");
    00549     DEBUG(net, 0, "[udp]   this usually means that the master server cannot reach us");
    00550     DEBUG(net, 0, "[udp]   please allow udp and tcp packets to port %u to be delivered", _settings_client.network.server_port);
    00551     DEBUG(net, 0, "[udp]   please allow udp and tcp packets from port %u to be delivered", _settings_client.network.server_port);
    00552   }
    00553
    00554   /* Send the packet */
    00555   Packet p(PACKET_UDP_SERVER_REGISTER);
    00556   /* Packet is: WELCOME_MESSAGE, Version, server_port */
    00557   p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
    00558   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
    00559   p.Send_uint16(_settings_client.network.server_port);
    00560   p.Send_uint64(_session_key);
    00561
    00562   _network_udp_mutex->BeginCritical();
    00563   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
    00564   _network_udp_mutex->EndCritical();
    00565 }
    00566
    00567 /* Register us to the master server
    00568  *   This function checks if it needs to send an advertise */
    00569 void NetworkUDPAdvertise()
    00570 {
    00571   /* Check if we should send an advertise */
    00572   if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise) return;
    00573
    00574   if (_network_need_advertise) {
    00575     _network_need_advertise = false;
    00576     _network_advertise_retries = ADVERTISE_RETRY_TIMES;
    00577   } else {
    00578     /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
    00579     if (_network_advertise_retries == 0) {
    00580       if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) return;
    00581
    00582       _network_advertise_retries = ADVERTISE_RETRY_TIMES;
    00583     }
    00584
    00585     if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) return;
    00586   }
    00587
    00588   _network_advertise_retries--;
    00589   _network_last_advertise_frame = _frame_counter;
    00590
    00591   if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) {
    00592     NetworkUDPAdvertiseThread(NULL);
    00593   }
    00594 }
    00595
    00596 void NetworkUDPInitialize()
    00597 {
    00598   /* If not closed, then do it. */
    00599   if (_udp_server_socket != NULL) NetworkUDPClose();
    00600
    00601   DEBUG(net, 1, "[udp] initializing listeners");
    00602   assert(_udp_client_socket == NULL && _udp_server_socket == NULL && _udp_master_socket == NULL);
    00603
    00604   _network_udp_mutex->BeginCritical();
    00605
    00606   _udp_client_socket = new ClientNetworkUDPSocketHandler();
    00607
    00608   NetworkAddressList server;
    00609   GetBindAddresses(&server, _settings_client.network.server_port);
    00610   _udp_server_socket = new ServerNetworkUDPSocketHandler(&server);
    00611
    00612   server.Clear();
    00613   GetBindAddresses(&server, 0);
    00614   _udp_master_socket = new MasterNetworkUDPSocketHandler(&server);
    00615
    00616   _network_udp_server = false;
    00617   _network_udp_broadcast = 0;
    00618   _network_udp_mutex->EndCritical();
    00619 }
    00620
    00621 void NetworkUDPClose()
    00622 {
    00623   _network_udp_mutex->BeginCritical();
    00624   _udp_server_socket->Close();
    00625   _udp_master_socket->Close();
    00626   _udp_client_socket->Close();
    00627   delete _udp_client_socket;
    00628   delete _udp_server_socket;
    00629   delete _udp_master_socket;
    00630   _udp_client_socket = NULL;
    00631   _udp_server_socket = NULL;
    00632   _udp_master_socket = NULL;
    00633   _network_udp_mutex->EndCritical();
    00634
    00635   _network_udp_server = false;
    00636   _network_udp_broadcast = 0;
    00637   DEBUG(net, 1, "[udp] closed listeners");
    00638 }
    00639
    00640 #endif /* ENABLE_NETWORK */