network_gamelist.cpp

Go to the documentation of this file.
00001 /* $Id: network_gamelist.cpp 17565 2009-09-18 07:09:11Z rubidium $ */
00002 
00008 #ifdef ENABLE_NETWORK
00009 
00010 #include "../stdafx.h"
00011 #include "../debug.h"
00012 #include "../core/alloc_func.hpp"
00013 #include "../thread.h"
00014 #include "../string_func.h"
00015 #include "network_internal.h"
00016 #include "network_udp.h"
00017 #include "network_gamelist.h"
00018 
00019 NetworkGameList *_network_game_list = NULL;
00020 
00021 ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
00022 NetworkGameList *_network_game_delayed_insertion_list = NULL;
00023 
00028 void NetworkGameListAddItemDelayed(NetworkGameList *item)
00029 {
00030   _network_game_list_mutex->BeginCritical();
00031   item->next = _network_game_delayed_insertion_list;
00032   _network_game_delayed_insertion_list = item;
00033   _network_game_list_mutex->EndCritical();
00034 }
00035 
00037 static void NetworkGameListHandleDelayedInsert()
00038 {
00039   _network_game_list_mutex->BeginCritical();
00040   while (_network_game_delayed_insertion_list != NULL) {
00041     NetworkGameList *ins_item = _network_game_delayed_insertion_list;
00042     _network_game_delayed_insertion_list = ins_item->next;
00043 
00044     NetworkGameList *item = NetworkGameListAddItem(ins_item->ip, ins_item->port);
00045 
00046     if (item != NULL) {
00047       ClearGRFConfigList(&item->info.grfconfig);
00048       if (StrEmpty(item->info.server_name)) {
00049         memset(&item->info, 0, sizeof(item->info));
00050         strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
00051         strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
00052         item->online = false;
00053       }
00054       item->manually |= ins_item->manually;
00055       if (item->manually) NetworkRebuildHostList();
00056       UpdateNetworkGameWindow(false);
00057     }
00058     free(ins_item);
00059   }
00060   _network_game_list_mutex->EndCritical();
00061 }
00062 
00068 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
00069 {
00070   if (ip == 0) return NULL;
00071 
00072   NetworkGameList *item, *prev_item;
00073 
00074   prev_item = NULL;
00075   for (item = _network_game_list; item != NULL; item = item->next) {
00076     if (item->ip == ip && item->port == port) return item;
00077     prev_item = item;
00078   }
00079 
00080   item = CallocT<NetworkGameList>(1);
00081   item->next = NULL;
00082   item->ip = ip;
00083   item->port = port;
00084 
00085   if (prev_item == NULL) {
00086     _network_game_list = item;
00087   } else {
00088     prev_item->next = item;
00089   }
00090   DEBUG(net, 4, "[gamelist] added server to list");
00091 
00092   UpdateNetworkGameWindow(false);
00093 
00094   return item;
00095 }
00096 
00099 void NetworkGameListRemoveItem(NetworkGameList *remove)
00100 {
00101   NetworkGameList *item, *prev_item;
00102 
00103   prev_item = NULL;
00104   for (item = _network_game_list; item != NULL; item = item->next) {
00105     if (remove == item) {
00106       if (prev_item == NULL) {
00107         _network_game_list = remove->next;
00108       } else {
00109         prev_item->next = remove->next;
00110       }
00111 
00112       /* Remove GRFConfig information */
00113       ClearGRFConfigList(&remove->info.grfconfig);
00114       free(remove);
00115       remove = NULL;
00116 
00117       DEBUG(net, 4, "[gamelist] removed server from list");
00118       NetworkRebuildHostList();
00119       UpdateNetworkGameWindow(false);
00120       return;
00121     }
00122     prev_item = item;
00123   }
00124 }
00125 
00126 enum {
00127   MAX_GAME_LIST_REQUERY_COUNT  =  5, 
00128   REQUERY_EVERY_X_GAMELOOPS    = 60, 
00129   REFRESH_GAMEINFO_X_REQUERIES = 50, 
00130 };
00131 
00133 void NetworkGameListRequery()
00134 {
00135   NetworkGameListHandleDelayedInsert();
00136 
00137   static uint8 requery_cnt = 0;
00138 
00139   if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
00140   requery_cnt = 0;
00141 
00142   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00143     item->retries++;
00144     if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
00145 
00146     /* item gets mostly zeroed by NetworkUDPQueryServer */
00147     uint8 retries = item->retries;
00148     NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
00149     item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
00150   }
00151 }
00152 
00157 void NetworkAfterNewGRFScan()
00158 {
00159   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00160     /* Reset compatability state */
00161     item->info.compatible = item->info.version_compatible;
00162 
00163     for (GRFConfig *c = item->info.grfconfig; c != NULL; c = c->next) {
00164       assert(HasBit(c->flags, GCF_COPY));
00165 
00166       const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00167       if (f == NULL) {
00168         /* Don't know the GRF, so mark game incompatible and the (possibly)
00169          * already resolved name for this GRF (another server has sent the
00170          * name of the GRF already */
00171         c->name   = FindUnknownGRFName(c->grfid, c->md5sum, true);
00172         c->status = GCS_NOT_FOUND;
00173 
00174         /* If we miss a file, we're obviously incompatible */
00175         item->info.compatible = false;
00176       } else {
00177         c->filename  = f->filename;
00178         c->name      = f->name;
00179         c->info      = f->info;
00180         c->status    = GCS_UNKNOWN;
00181       }
00182     }
00183   }
00184 }
00185 
00186 #endif /* ENABLE_NETWORK */

Generated on Thu Sep 24 19:35:03 2009 for OpenTTD by  doxygen 1.5.6