network_gamelist.cpp

Go to the documentation of this file.
00001 /* $Id: network_gamelist.cpp 24075 2012-03-26 20:41:21Z 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 
00015 #ifdef ENABLE_NETWORK
00016 
00017 #include "../stdafx.h"
00018 #include "../debug.h"
00019 #include "../window_func.h"
00020 #include "../thread/thread.h"
00021 #include "network_internal.h"
00022 #include "network_udp.h"
00023 #include "network_gamelist.h"
00024 
00025 NetworkGameList *_network_game_list = NULL;
00026 
00028 static ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
00030 static NetworkGameList *_network_game_delayed_insertion_list = NULL;
00031 
00037 void NetworkGameListAddItemDelayed(NetworkGameList *item)
00038 {
00039   _network_game_list_mutex->BeginCritical();
00040   item->next = _network_game_delayed_insertion_list;
00041   _network_game_delayed_insertion_list = item;
00042   _network_game_list_mutex->EndCritical();
00043 }
00044 
00046 static void NetworkGameListHandleDelayedInsert()
00047 {
00048   _network_game_list_mutex->BeginCritical();
00049   while (_network_game_delayed_insertion_list != NULL) {
00050     NetworkGameList *ins_item = _network_game_delayed_insertion_list;
00051     _network_game_delayed_insertion_list = ins_item->next;
00052 
00053     NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
00054 
00055     if (item != NULL) {
00056       if (StrEmpty(item->info.server_name)) {
00057         ClearGRFConfigList(&item->info.grfconfig);
00058         memset(&item->info, 0, sizeof(item->info));
00059         strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
00060         strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
00061         item->online = false;
00062       }
00063       item->manually |= ins_item->manually;
00064       if (item->manually) NetworkRebuildHostList();
00065       UpdateNetworkGameWindow();
00066     }
00067     free(ins_item);
00068   }
00069   _network_game_list_mutex->EndCritical();
00070 }
00071 
00078 NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
00079 {
00080   const char *hostname = address.GetHostname();
00081 
00082   /* Do not query the 'any' address. */
00083   if (StrEmpty(hostname) ||
00084       strcmp(hostname, "0.0.0.0") == 0 ||
00085       strcmp(hostname, "::") == 0) {
00086     return NULL;
00087   }
00088 
00089   NetworkGameList *item, *prev_item;
00090 
00091   prev_item = NULL;
00092   for (item = _network_game_list; item != NULL; item = item->next) {
00093     if (item->address == address) return item;
00094     prev_item = item;
00095   }
00096 
00097   item = CallocT<NetworkGameList>(1);
00098   item->next = NULL;
00099   item->address = address;
00100 
00101   if (prev_item == NULL) {
00102     _network_game_list = item;
00103   } else {
00104     prev_item->next = item;
00105   }
00106   DEBUG(net, 4, "[gamelist] added server to list");
00107 
00108   UpdateNetworkGameWindow();
00109 
00110   return item;
00111 }
00112 
00117 void NetworkGameListRemoveItem(NetworkGameList *remove)
00118 {
00119   NetworkGameList *prev_item = NULL;
00120   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00121     if (remove == item) {
00122       if (prev_item == NULL) {
00123         _network_game_list = remove->next;
00124       } else {
00125         prev_item->next = remove->next;
00126       }
00127 
00128       /* Remove GRFConfig information */
00129       ClearGRFConfigList(&remove->info.grfconfig);
00130       free(remove);
00131       remove = NULL;
00132 
00133       DEBUG(net, 4, "[gamelist] removed server from list");
00134       NetworkRebuildHostList();
00135       UpdateNetworkGameWindow();
00136       return;
00137     }
00138     prev_item = item;
00139   }
00140 }
00141 
00142 static const uint MAX_GAME_LIST_REQUERY_COUNT  = 10; 
00143 static const uint REQUERY_EVERY_X_GAMELOOPS    = 60; 
00144 static const uint REFRESH_GAMEINFO_X_REQUERIES = 50; 
00145 
00147 void NetworkGameListRequery()
00148 {
00149   NetworkGameListHandleDelayedInsert();
00150 
00151   static uint8 requery_cnt = 0;
00152 
00153   if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
00154   requery_cnt = 0;
00155 
00156   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00157     item->retries++;
00158     if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
00159 
00160     /* item gets mostly zeroed by NetworkUDPQueryServer */
00161     uint8 retries = item->retries;
00162     NetworkUDPQueryServer(NetworkAddress(item->address));
00163     item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
00164   }
00165 }
00166 
00171 void NetworkAfterNewGRFScan()
00172 {
00173   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00174     /* Reset compatability state */
00175     item->info.compatible = item->info.version_compatible;
00176 
00177     for (GRFConfig *c = item->info.grfconfig; c != NULL; c = c->next) {
00178       assert(HasBit(c->flags, GCF_COPY));
00179 
00180       const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
00181       if (f == NULL) {
00182         /* Don't know the GRF, so mark game incompatible and the (possibly)
00183          * already resolved name for this GRF (another server has sent the
00184          * name of the GRF already */
00185         c->name->Release();
00186         c->name = FindUnknownGRFName(c->ident.grfid, c->ident.md5sum, true);
00187         c->name->AddRef();
00188         c->status = GCS_NOT_FOUND;
00189 
00190         /* If we miss a file, we're obviously incompatible */
00191         item->info.compatible = false;
00192       } else {
00193         c->filename = f->filename;
00194         c->name->Release();
00195         c->name = f->name;
00196         c->name->AddRef();
00197         c->info->Release();
00198         c->info = f->info;
00199         c->info->AddRef();
00200         c->status = GCS_UNKNOWN;
00201       }
00202     }
00203   }
00204 
00205   InvalidateWindowClassesData(WC_NETWORK_WINDOW);
00206 }
00207 
00208 #endif /* ENABLE_NETWORK */