network_gamelist.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 
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 #include "../newgrf_text.h"
00025 
00026 NetworkGameList *_network_game_list = NULL;
00027 
00028 static ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
00029 static NetworkGameList *_network_game_delayed_insertion_list = NULL;
00030 
00036 void NetworkGameListAddItemDelayed(NetworkGameList *item)
00037 {
00038   _network_game_list_mutex->BeginCritical();
00039   item->next = _network_game_delayed_insertion_list;
00040   _network_game_delayed_insertion_list = item;
00041   _network_game_list_mutex->EndCritical();
00042 }
00043 
00045 static void NetworkGameListHandleDelayedInsert()
00046 {
00047   _network_game_list_mutex->BeginCritical();
00048   while (_network_game_delayed_insertion_list != NULL) {
00049     NetworkGameList *ins_item = _network_game_delayed_insertion_list;
00050     _network_game_delayed_insertion_list = ins_item->next;
00051 
00052     NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
00053 
00054     if (item != NULL) {
00055       if (StrEmpty(item->info.server_name)) {
00056         ClearGRFConfigList(&item->info.grfconfig);
00057         memset(&item->info, 0, sizeof(item->info));
00058         strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
00059         strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
00060         item->online = false;
00061       }
00062       item->manually |= ins_item->manually;
00063       if (item->manually) NetworkRebuildHostList();
00064       UpdateNetworkGameWindow(false);
00065     }
00066     free(ins_item);
00067   }
00068   _network_game_list_mutex->EndCritical();
00069 }
00070 
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(false);
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(false);
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 */

Generated on Thu Apr 14 00:48:15 2011 for OpenTTD by  doxygen 1.6.1