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 *unknown_name;
00373     char name[NETWORK_GRF_NAME_LENGTH];
00374     GRFIdentifier c;
00375 
00376     this->ReceiveGRFIdentifier(p, &c);
00377     p->Recv_string(name, sizeof(name));
00378 
00379     /* An empty name is not possible under normal circumstances
00380      * and causes problems when showing the NewGRF list. */
00381     if (StrEmpty(name)) continue;
00382 
00383     /* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
00384      * If it exists and not resolved yet, then name of the fake GRF is
00385      * overwritten with the name from the reply. */
00386     unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
00387     if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
00388       ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
00389     }
00390   }
00391 }
00392 
00393 void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
00394 {
00395   /* Find the matching GRF file */
00396   const GRFConfig *f = FindGRFConfig(config->ident.grfid, FGCM_EXACT, config->ident.md5sum);
00397   if (f == NULL) {
00398     /* Don't know the GRF, so mark game incompatible and the (possibly)
00399      * already resolved name for this GRF (another server has sent the
00400      * name of the GRF already */
00401     AddGRFTextToList(&config->name, FindUnknownGRFName(config->ident.grfid, config->ident.md5sum, true));
00402     config->status = GCS_NOT_FOUND;
00403   } else {
00404     config->filename  = f->filename;
00405     config->name      = DuplicateGRFText(f->name);
00406     config->info      = f->info;
00407   }
00408   SetBit(config->flags, GCF_COPY);
00409 }
00410 
00411 /* Broadcast to all ips */
00412 static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
00413 {
00414   for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) {
00415     Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
00416 
00417     DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname());
00418 
00419     socket->SendPacket(&p, addr, true, true);
00420   }
00421 }
00422 
00423 
00424 /* Request the the server-list from the master server */
00425 void NetworkUDPQueryMasterServer()
00426 {
00427   Packet p(PACKET_UDP_CLIENT_GET_LIST);
00428   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
00429 
00430   /* packet only contains protocol version */
00431   p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
00432   p.Send_uint8(SLT_AUTODETECT);
00433 
00434   _udp_client_socket->SendPacket(&p, &out_addr, true);
00435 
00436   DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString());
00437 }
00438 
00439 /* Find all servers */
00440 void NetworkUDPSearchGame()
00441 {
00442   /* We are still searching.. */
00443   if (_network_udp_broadcast > 0) return;
00444 
00445   DEBUG(net, 0, "[udp] searching server");
00446 
00447   NetworkUDPBroadCast(_udp_client_socket);
00448   _network_udp_broadcast = 300; // Stay searching for 300 ticks
00449 }
00450 
00452 struct NetworkUDPQueryServerInfo : NetworkAddress {
00453   bool manually; 
00454   NetworkUDPQueryServerInfo(const NetworkAddress &address, bool manually) :
00455     NetworkAddress(address),
00456     manually(manually)
00457   {
00458   }
00459 };
00460 
00465 static void NetworkUDPQueryServerThread(void *pntr)
00466 {
00467   NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
00468 
00469   /* Clear item in gamelist */
00470   NetworkGameList *item = CallocT<NetworkGameList>(1);
00471   item->address = *info;
00472   info->GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
00473   strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname));
00474   item->manually = info->manually;
00475   NetworkGameListAddItemDelayed(item);
00476 
00477   _network_udp_mutex->BeginCritical();
00478   /* Init the packet */
00479   Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
00480   if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, info);
00481   _network_udp_mutex->EndCritical();
00482 
00483   delete info;
00484 }
00485 
00486 void NetworkUDPQueryServer(NetworkAddress address, bool manually)
00487 {
00488   NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
00489   if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info)) {
00490     NetworkUDPQueryServerThread(info);
00491   }
00492 }
00493 
00494 static void NetworkUDPRemoveAdvertiseThread(void *pntr)
00495 {
00496   DEBUG(net, 1, "[udp] removing advertise from master server");
00497 
00498   /* Find somewhere to send */
00499   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
00500 
00501   /* Send the packet */
00502   Packet p(PACKET_UDP_SERVER_UNREGISTER);
00503   /* Packet is: Version, server_port */
00504   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
00505   p.Send_uint16(_settings_client.network.server_port);
00506 
00507   _network_udp_mutex->BeginCritical();
00508   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
00509   _network_udp_mutex->EndCritical();
00510 }
00511 
00516 void NetworkUDPRemoveAdvertise(bool blocking)
00517 {
00518   /* Check if we are advertising */
00519   if (!_networking || !_network_server || !_network_udp_server) return;
00520 
00521   if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL)) {
00522     NetworkUDPRemoveAdvertiseThread(NULL);
00523   }
00524 }
00525 
00526 static void NetworkUDPAdvertiseThread(void *pntr)
00527 {
00528   /* Find somewhere to send */
00529   NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
00530 
00531   DEBUG(net, 1, "[udp] advertising to master server");
00532 
00533   /* Add a bit more messaging when we cannot get a session key */
00534   static byte session_key_retries = 0;
00535   if (_session_key == 0 && session_key_retries++ == 2) {
00536     DEBUG(net, 0, "[udp] advertising to the master server is failing");
00537     DEBUG(net, 0, "[udp]   we are not receiving the session key from the server");
00538     DEBUG(net, 0, "[udp]   please allow udp packets from %s to you to be delivered", out_addr.GetAddressAsString(false));
00539     DEBUG(net, 0, "[udp]   please allow udp packets from you to %s to be delivered", out_addr.GetAddressAsString(false));
00540   }
00541   if (_session_key != 0 && _network_advertise_retries == 0) {
00542     DEBUG(net, 0, "[udp] advertising to the master server is failing");
00543     DEBUG(net, 0, "[udp]   we are not receiving the acknowledgement from the server");
00544     DEBUG(net, 0, "[udp]   this usually means that the master server cannot reach us");
00545     DEBUG(net, 0, "[udp]   please allow udp and tcp packets to port %u to be delivered", _settings_client.network.server_port);
00546     DEBUG(net, 0, "[udp]   please allow udp and tcp packets from port %u to be delivered", _settings_client.network.server_port);
00547   }
00548 
00549   /* Send the packet */
00550   Packet p(PACKET_UDP_SERVER_REGISTER);
00551   /* Packet is: WELCOME_MESSAGE, Version, server_port */
00552   p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
00553   p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
00554   p.Send_uint16(_settings_client.network.server_port);
00555   p.Send_uint64(_session_key);
00556 
00557   _network_udp_mutex->BeginCritical();
00558   if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
00559   _network_udp_mutex->EndCritical();
00560 }
00561 
00562 /* Register us to the master server
00563  *   This function checks if it needs to send an advertise */
00564 void NetworkUDPAdvertise()
00565 {
00566   /* Check if we should send an advertise */
00567   if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise) return;
00568 
00569   if (_network_need_advertise) {
00570     _network_need_advertise = false;
00571     _network_advertise_retries = ADVERTISE_RETRY_TIMES;
00572   } else {
00573     /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
00574     if (_network_advertise_retries == 0) {
00575       if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) return;
00576 
00577       _network_advertise_retries = ADVERTISE_RETRY_TIMES;
00578     }
00579 
00580     if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) return;
00581   }
00582 
00583   _network_advertise_retries--;
00584   _network_last_advertise_frame = _frame_counter;
00585 
00586   if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) {
00587     NetworkUDPAdvertiseThread(NULL);
00588   }
00589 }
00590 
00591 void NetworkUDPInitialize()
00592 {
00593   /* If not closed, then do it. */
00594   if (_udp_server_socket != NULL) NetworkUDPClose();
00595 
00596   DEBUG(net, 1, "[udp] initializing listeners");
00597   assert(_udp_client_socket == NULL && _udp_server_socket == NULL && _udp_master_socket == NULL);
00598 
00599   _network_udp_mutex->BeginCritical();
00600 
00601   _udp_client_socket = new ClientNetworkUDPSocketHandler();
00602 
00603   NetworkAddressList server;
00604   GetBindAddresses(&server, _settings_client.network.server_port);
00605   _udp_server_socket = new ServerNetworkUDPSocketHandler(&server);
00606 
00607   server.Clear();
00608   GetBindAddresses(&server, 0);
00609   _udp_master_socket = new MasterNetworkUDPSocketHandler(&server);
00610 
00611   _network_udp_server = false;
00612   _network_udp_broadcast = 0;
00613   _network_udp_mutex->EndCritical();
00614 }
00615 
00616 void NetworkUDPClose()
00617 {
00618   _network_udp_mutex->BeginCritical();
00619   _udp_server_socket->Close();
00620   _udp_master_socket->Close();
00621   _udp_client_socket->Close();
00622   delete _udp_client_socket;
00623   delete _udp_server_socket;
00624   delete _udp_master_socket;
00625   _udp_client_socket = NULL;
00626   _udp_server_socket = NULL;
00627   _udp_master_socket = NULL;
00628   _network_udp_mutex->EndCritical();
00629 
00630   _network_udp_server = false;
00631   _network_udp_broadcast = 0;
00632   DEBUG(net, 1, "[udp] closed listeners");
00633 }
00634 
00635 #endif /* ENABLE_NETWORK */

Generated on Sun Jan 23 01:49:04 2011 for OpenTTD by  doxygen 1.6.1