# network_udp.cpp

Go to the documentation of this file.

    00001 /* $Id: network_udp.cpp 19094 2010-02-11 14:58:51Z yexo $ */
    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((!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, (!StrEmpty(in_reply[i]->name)) ? in_reply[i]->name : in_reply[i]->filename, lastof(name));
    00210     this->Send_GRFIdentifier(&packet, in_reply[i]);
    00211     packet.Send_string(name);
    00212   }
    00213
    00214   this->SendPacket(&packet, client_addr);
    00215 }
    00216
    00218
    00219 class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
    00220 protected:
    00221   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
    00222   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
    00223   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
    00224   virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
    00225 public:
    00226   virtual ~ClientNetworkUDPSocketHandler() {}
    00227 };
    00228
    00229 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
    00230 {
    00231   NetworkGameList *item;
    00232
    00233   /* Just a fail-safe.. should never happen */
    00234   if (_network_udp_server) return;
    00235
    00236   DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString());
    00237
    00238   /* Find next item */
    00239   item = NetworkGameListAddItem(*client_addr);
    00240
    00241   ClearGRFConfigList(&item->info.grfconfig);
    00242   this->Recv_NetworkGameInfo(p, &item->info);
    00243
    00244   item->info.compatible = true;
    00245   {
    00246     /* Checks whether there needs to be a request for names of GRFs and makes
    00247      * the request if necessary. GRFs that need to be requested are the GRFs
    00248      * that do not exist on the clients system and we do not have the name
    00249      * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
    00250      * The in_request array and in_request_count are used so there is no need
    00251      * to do a second loop over the GRF list, which can be relatively expensive
    00252      * due to the string comparisons. */
    00253     const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
    00254     const GRFConfig *c;
    00255     uint in_request_count = 0;
    00256
    00257     for (c = item->info.grfconfig; c != NULL; c = c->next) {
    00258       if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
    00259       if (c->status != GCS_NOT_FOUND || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
    00260       in_request[in_request_count] = c;
    00261       in_request_count++;
    00262     }
    00263
    00264     if (in_request_count > 0) {
    00265       /* There are 'unknown' GRFs, now send a request for them */
    00266       uint i;
    00267       Packet packet(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00268
    00269       packet.Send_uint8(in_request_count);
    00270       for (i = 0; i < in_request_count; i++) {
    00271         this->Send_GRFIdentifier(&packet, in_request[i]);
    00272       }
    00273
    00274       this->SendPacket(&packet, &item->address);
    00275     }
    00276   }
    00277
    00278   if (item->info.hostname[0] == '\0') {
    00279     snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", client_addr->GetHostname());
    00280   }
    00281
    00282   if (client_addr->GetAddress()->ss_family == AF_INET6) {
    00283     strecat(item->info.server_name, " (IPv6)", lastof(item->info.server_name));
    00284   }
    00285
    00286   /* Check if we are allowed on this server based on the revision-match */
    00287   item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision);
    00288   item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
    00289
    00290   item->online = true;
    00291
    00292   UpdateNetworkGameWindow(false);
    00293 }
    00294
    00295 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_MASTER_RESPONSE_LIST)
    00296 {
    00297   /* packet begins with the protocol version (uint8)
    00298    * then an uint16 which indicates how many
    00299    * ip:port pairs are in this packet, after that
    00300    * an uint32 (ip) and an uint16 (port) for each pair.
    00301    */
    00302
    00303   ServerListType type = (ServerListType)(p->Recv_uint8() - 1);
    00304
    00305   if (type < SLT_END) {
    00306     for (int i = p->Recv_uint16(); i != 0 ; i--) {
    00307       sockaddr_storage addr_storage;
    00308       memset(&addr_storage, 0, sizeof(addr_storage));
    00309
    00310       if (type == SLT_IPv4) {
    00311         addr_storage.ss_family = AF_INET;
    00312         ((sockaddr_in*)&addr_storage)->sin_addr.s_addr = TO_LE32(p->Recv_uint32());
    00313       } else {
    00314         assert(type == SLT_IPv6);
    00315         addr_storage.ss_family = AF_INET6;
    00316         byte *addr = (byte*)&((sockaddr_in6*)&addr_storage)->sin6_addr;
    00317         for (uint i = 0; i < sizeof(in6_addr); i++) *addr++ = p->Recv_uint8();
    00318       }
    00319       NetworkAddress addr(addr_storage, type == SLT_IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
    00320       addr.SetPort(p->Recv_uint16());
    00321
    00322       /* Somehow we reached the end of the packet */
    00323       if (this->HasClientQuit()) return;
    00324
    00325       NetworkUDPQueryServer(addr);
    00326     }
    00327   }
    00328 }
    00329
    00331 DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
    00332 {
    00333   uint8 num_grfs;
    00334   uint i;
    00335
    00336   DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString());
    00337
    00338   num_grfs = p->Recv_uint8 ();
    00339   if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
    00340
    00341   for (i = 0; i < num_grfs; i++) {
    00342     char *unknown_name;
    00343     char name[NETWORK_GRF_NAME_LENGTH];
    00344     GRFConfig c;
    00345
    00346     this->Recv_GRFIdentifier(p, &c);
    00347     p->Recv_string(name, sizeof(name));
    00348
    00349     /* An empty name is not possible under normal circumstances
    00350      * and causes problems when showing the NewGRF list. */
    00351     if (StrEmpty(name)) continue;
    00352
    00353     /* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
    00354      * If it exists and not resolved yet, then name of the fake GRF is
    00355      * overwritten with the name from the reply. */
    00356     unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
    00357     if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
    00358       ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
    00359     }
    00360   }
    00361 }
    00362
    00363 void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
    00364 {
    00365   /* Find the matching GRF file */
    00366   const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
    00367   if (f == NULL) {
    00368     /* Don't know the GRF, so mark game incompatible and the (possibly)
    00369      * already resolved name for this GRF (another server has sent the
    00370      * name of the GRF already */
    00371     config->name   = FindUnknownGRFName(config->grfid, config->md5sum, true);
    00372     config->status = GCS_NOT_FOUND;
    00373   } else {
    00374     config->filename  = f->filename;
    00375     config->name      = f->name;
    00376     config->info      = f->info;
    00377   }
    00378   SetBit(config->flags, GCF_COPY);
    00379 }
    00380
    00381 /* Broadcast to all ips */
    00382 static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
    00383 {
    00384   for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) {
    00385     Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
    00386
    00387     DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname());
    00388
    00389     socket->SendPacket(&p, addr, true, true);
    00390   }
    00391 }
    00392
    00393
    00394 /* Request the the server-list from the master server */
    00395 void NetworkUDPQueryMasterServer()
    00396 {
    00397   Packet p(PACKET_UDP_CLIENT_GET_LIST);
    00398   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00399
    00400   /* packet only contains protocol version */
    00401   p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
    00402   p.Send_uint8(SLT_AUTODETECT);
    00403
    00404   _udp_client_socket->SendPacket(&p, &out_addr, true);
    00405
    00406   DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString());
    00407 }
    00408
    00409 /* Find all servers */
    00410 void NetworkUDPSearchGame()
    00411 {
    00412   /* We are still searching.. */
    00413   if (_network_udp_broadcast > 0) return;
    00414
    00415   DEBUG(net, 0, "[udp] searching server");
    00416
    00417   NetworkUDPBroadCast(_udp_client_socket);
    00418   _network_udp_broadcast = 300; // Stay searching for 300 ticks
    00419 }
    00420
    00422 struct NetworkUDPQueryServerInfo : NetworkAddress {
    00423   bool manually;
    00424   NetworkUDPQueryServerInfo(const NetworkAddress &address, bool manually) :
    00425     NetworkAddress(address),
    00426     manually(manually)
    00427   {
    00428   }
    00429 };
    00430
    00435 static void NetworkUDPQueryServerThread(void *pntr)
    00436 {
    00437   NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
    00438
    00439   /* Clear item in gamelist */
    00440   NetworkGameList *item = CallocT<NetworkGameList>(1);
    00441   item->address = *info;
    00442   info->GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
    00443   strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname));
    00444   item->manually = info->manually;
    00445   NetworkGameListAddItemDelayed(item);
    00446
    00447   _network_udp_mutex->BeginCritical();
    00448   /* Init the packet */
    00449   Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
    00450   if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, info);
    00451   _network_udp_mutex->EndCritical();
    00452
    00453   delete info;
    00454 }
    00455
    00456 void NetworkUDPQueryServer(NetworkAddress address, bool manually)
    00457 {
    00458   NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
    00459   if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info)) {
    00460     NetworkUDPQueryServerThread(info);
    00461   }
    00462 }
    00463
    00464 static void NetworkUDPRemoveAdvertiseThread(void *pntr)
    00465 {
    00466   DEBUG(net, 1, "[udp] removing advertise from master server");
    00467
    00468   /* Find somewhere to send */
    00469   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00470
    00471   /* Send the packet */
    00472   Packet p(PACKET_UDP_SERVER_UNREGISTER);
    00473   /* Packet is: Version, server_port */
    00474   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
    00475   p.Send_uint16(_settings_client.network.server_port);
    00476
    00477   _network_udp_mutex->BeginCritical();
    00478   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
    00479   _network_udp_mutex->EndCritical();
    00480 }
    00481
    00486 void NetworkUDPRemoveAdvertise(bool blocking)
    00487 {
    00488   /* Check if we are advertising */
    00489   if (!_networking || !_network_server || !_network_udp_server) return;
    00490
    00491   if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL)) {
    00492     NetworkUDPRemoveAdvertiseThread(NULL);
    00493   }
    00494 }
    00495
    00496 static void NetworkUDPAdvertiseThread(void *pntr)
    00497 {
    00498   /* Find somewhere to send */
    00499   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
    00500
    00501   DEBUG(net, 1, "[udp] advertising to master server");
    00502
    00503   /* Add a bit more messaging when we cannot get a session key */
    00504   static byte session_key_retries = 0;
    00505   if (_session_key == 0 && session_key_retries++ == 2) {
    00506     DEBUG(net, 0, "[udp] advertising to the master server is failing");
    00507     DEBUG(net, 0, "[udp]   we are not receiving the session key from the server");
    00508     DEBUG(net, 0, "[udp]   please allow udp packets from %s to you to be delivered", out_addr.GetAddressAsString(false));
    00509     DEBUG(net, 0, "[udp]   please allow udp packets from you to %s to be delivered", out_addr.GetAddressAsString(false));
    00510   }
    00511   if (_session_key != 0 && _network_advertise_retries == 0) {
    00512     DEBUG(net, 0, "[udp] advertising to the master server is failing");
    00513     DEBUG(net, 0, "[udp]   we are not receiving the acknowledgement from the server");
    00514     DEBUG(net, 0, "[udp]   this usually means that the master server cannot reach us");
    00515     DEBUG(net, 0, "[udp]   please allow udp and tcp packets to port %u to be delivered", _settings_client.network.server_port);
    00516     DEBUG(net, 0, "[udp]   please allow udp and tcp packets from port %u to be delivered", _settings_client.network.server_port);
    00517   }
    00518
    00519   /* Send the packet */
    00520   Packet p(PACKET_UDP_SERVER_REGISTER);
    00521   /* Packet is: WELCOME_MESSAGE, Version, server_port */
    00522   p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
    00523   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
    00524   p.Send_uint16(_settings_client.network.server_port);
    00525   p.Send_uint64(_session_key);
    00526
    00527   _network_udp_mutex->BeginCritical();
    00528   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
    00529   _network_udp_mutex->EndCritical();
    00530 }
    00531
    00532 /* Register us to the master server
    00533  *   This function checks if it needs to send an advertise */
    00534 void NetworkUDPAdvertise()
    00535 {
    00536   /* Check if we should send an advertise */
    00537   if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise)
    00538     return;
    00539
    00540   if (_network_need_advertise) {
    00541     _network_need_advertise = false;
    00542     _network_advertise_retries = ADVERTISE_RETRY_TIMES;
    00543   } else {
    00544     /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
    00545     if (_network_advertise_retries == 0) {
    00546       if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter)
    00547         return;
    00548       _network_advertise_retries = ADVERTISE_RETRY_TIMES;
    00549     }
    00550
    00551     if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter)
    00552       return;
    00553   }
    00554
    00555   _network_advertise_retries--;
    00556   _network_last_advertise_frame = _frame_counter;
    00557
    00558   if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) {
    00559     NetworkUDPAdvertiseThread(NULL);
    00560   }
    00561 }
    00562
    00563 void NetworkUDPInitialize()
    00564 {
    00565   /* If not closed, then do it. */
    00566   if (_udp_server_socket != NULL) NetworkUDPClose();
    00567
    00568   DEBUG(net, 1, "[udp] initializing listeners");
    00569   assert(_udp_client_socket == NULL && _udp_server_socket == NULL && _udp_master_socket == NULL);
    00570
    00571   _network_udp_mutex->BeginCritical();
    00572
    00573   _udp_client_socket = new ClientNetworkUDPSocketHandler();
    00574
    00575   NetworkAddressList server;
    00576   GetBindAddresses(&server, _settings_client.network.server_port);
    00577   _udp_server_socket = new ServerNetworkUDPSocketHandler(&server);
    00578
    00579   server.Clear();
    00580   GetBindAddresses(&server, 0);
    00581   _udp_master_socket = new MasterNetworkUDPSocketHandler(&server);
    00582
    00583   _network_udp_server = false;
    00584   _network_udp_broadcast = 0;
    00585   _network_udp_mutex->EndCritical();
    00586 }
    00587
    00588 void NetworkUDPClose()
    00589 {
    00590   _network_udp_mutex->BeginCritical();
    00591   _udp_server_socket->Close();
    00592   _udp_master_socket->Close();
    00593   _udp_client_socket->Close();
    00594   delete _udp_client_socket;
    00595   delete _udp_server_socket;
    00596   delete _udp_master_socket;
    00597   _udp_client_socket = NULL;
    00598   _udp_server_socket = NULL;
    00599   _udp_master_socket = NULL;
    00600   _network_udp_mutex->EndCritical();
    00601
    00602   _network_udp_server = false;
    00603   _network_udp_broadcast = 0;
    00604   DEBUG(net, 1, "[udp] closed listeners");
    00605 }
    00606
    00607 #endif /* ENABLE_NETWORK */