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