network_udp.cpp

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

Generated on Wed Dec 23 23:27:51 2009 for OpenTTD by  doxygen 1.5.6