# network_udp.cpp

Go to the documentation of this file.

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