00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #include <serverbrowser/ServerBrowser.h> 00022 #include <common/OptionsMasterListServer.h> 00023 00024 ServerBrowser *ServerBrowser::instance_ = 0; 00025 00026 ServerBrowser *ServerBrowser::instance() 00027 { 00028 if (!instance_) 00029 { 00030 instance_ = new ServerBrowser; 00031 } 00032 return instance_; 00033 } 00034 00035 ServerBrowser::ServerBrowser() : 00036 refreshing_(false), serverList_(), 00037 serverRefresh_(serverList_), 00038 serverCollector_(serverList_) 00039 { 00040 refreshingMutex_ = SDL_CreateMutex(); 00041 } 00042 00043 ServerBrowser::~ServerBrowser() 00044 { 00045 SDL_DestroyMutex(refreshingMutex_); 00046 } 00047 00048 void ServerBrowser::cancel() 00049 { 00050 SDL_LockMutex(refreshingMutex_); 00051 bool complete = true; 00052 if (refreshing_) 00053 { 00054 complete = false; 00055 serverCollector_.setCancel(true); 00056 serverRefresh_.setCancel(true); 00057 } 00058 SDL_UnlockMutex(refreshingMutex_); 00059 00060 while (!complete) 00061 { 00062 SDL_Delay(100); 00063 SDL_LockMutex(refreshingMutex_); 00064 if (!refreshing_) complete = true; 00065 SDL_UnlockMutex(refreshingMutex_); 00066 } 00067 } 00068 00069 void ServerBrowser::refreshList(RefreshType t) 00070 { 00071 bool alreadyRefreshing = false; 00072 SDL_LockMutex(refreshingMutex_); 00073 alreadyRefreshing = refreshing_; 00074 refreshing_ = true; 00075 SDL_UnlockMutex(refreshingMutex_); 00076 if (alreadyRefreshing) return; 00077 00078 SDL_CreateThread(ServerBrowser::threadFunc, (void *) int(t)); 00079 } 00080 00081 int ServerBrowser::threadFunc(void *var) 00082 { 00083 RefreshType typ = (RefreshType) long(var); 00084 00085 bool result = false; 00086 switch (typ) 00087 { 00088 case RefreshNone: 00089 // No new list 00090 break; 00091 case RefreshLan: 00092 result = instance_->serverCollector_.fetchLANList(); 00093 break; 00094 case RefreshNet: 00095 result = 00096 instance_->serverCollector_.fetchServerList( 00097 OptionsMasterListServer::instance()->getMasterListServer(), 00098 OptionsMasterListServer::instance()->getMasterListServerURI()) 00099 || 00100 instance_->serverCollector_.fetchServerList( 00101 OptionsMasterListServer::instance()->getMasterListBackupServer(), 00102 OptionsMasterListServer::instance()->getMasterListBackupServerURI()); 00103 00104 break; 00105 case RefreshFavourites: 00106 result = instance_->serverCollector_.fetchFavoritesList(); 00107 break; 00108 } 00109 if (result) 00110 { 00111 instance_->serverRefresh_.refreshList(); 00112 } 00113 00114 SDL_LockMutex(instance_->refreshingMutex_); 00115 instance_->serverCollector_.setCancel(false); 00116 instance_->serverRefresh_.setCancel(false); 00117 instance_->refreshing_ = false; 00118 SDL_UnlockMutex(instance_->refreshingMutex_); 00119 return 0; 00120 }
1.5.3