ConnectDialog.cpp

Go to the documentation of this file.
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 <dialogs/ConnectDialog.h>
00022 #include <dialogs/PlayerDialog.h>
00023 #include <dialogs/MsgBoxDialog.h>
00024 #include <dialogs/ProgressDialog.h>
00025 #include <client/ScorchedClient.h>
00026 #include <client/ClientParams.h>
00027 #include <client/ClientState.h>
00028 #include <coms/ComsMessageSender.h>
00029 #include <coms/ComsConnectMessage.h>
00030 #include <net/NetInterface.h>
00031 #include <common/Logger.h>
00032 
00033 ConnectDialog *ConnectDialog::instance_ = 0;
00034 
00035 ConnectDialog *ConnectDialog::instance()
00036 {
00037         if (!instance_)
00038         {
00039                 instance_ = new ConnectDialog;
00040         }
00041         return instance_;
00042 }
00043 
00044 ConnectDialog::ConnectDialog() : 
00045         GLWWindow("Connect", -100.0f, 10.0f, 20.0f, 20.0f, eNoDraw | eNoTitle,
00046                 "Connection dialog"),
00047         connectionState_(eWaiting),
00048         tryCount_(0), lastTime_(0), idStore_(0)
00049 {
00050         connectionState_ = eWaiting;
00051         tryCount_ = 0;
00052         lastTime_ = 0;
00053 }
00054 
00055 ConnectDialog::~ConnectDialog()
00056 {
00057 }
00058 
00059 UniqueIdStore &ConnectDialog::getIdStore()
00060 {
00061         if (!idStore_)
00062         {
00063                 idStore_ = new UniqueIdStore();
00064                 // Get the unique id
00065                 if (!idStore_->loadStore())
00066                 {
00067                         Logger::log("Failed to load id store");
00068                 }
00069         }
00070         return *idStore_;
00071 }
00072 
00073 void ConnectDialog::windowInit(const unsigned state)
00074 {
00075 
00076 }
00077 
00078 void ConnectDialog::simulate(float frameTime)
00079 {
00080         time_t currentTime = time(0);
00081         if (connectionState_ == eWaiting)
00082         {
00083                 if (currentTime - lastTime_ > 3)
00084                 {
00085                         ScorchedClient::instance()->getNetInterface().stop();
00086 
00087                         if (tryCount_<3)
00088                         {
00089                                 tryCount_++;
00090                                 lastTime_ = currentTime;
00091                                 tryConnection();
00092                         }
00093                         else
00094                         {
00095                                 connectionState_ = eFinished;
00096 
00097                                 LangString msg = LANG_RESOURCE_2("FAILED_TO_CONNECT_TIMEOUT",
00098                                         "Failed to connect to server \"{0}:{1}\", timeout.",
00099                                         host_,
00100                                         port_);
00101                                 MsgBoxDialog::instance()->show(msg);
00102 
00103                                 ScorchedClient::instance()->getGameState().stimulate(
00104                                         ClientState::StimOptions);
00105                         }
00106                 }
00107         }
00108 }
00109 
00110 void ConnectDialog::tryConnection()
00111 {
00112         const char *serverName = 
00113                 (ClientParams::instance()->getConnect()[0]?
00114                 ClientParams::instance()->getConnect():
00115                 "Localhost");
00116 
00117         host_ = serverName;
00118         port_ = S3D::ScorchedPort;
00119 
00120         char *colon = strchr((char *)serverName, ':');
00121         if (colon) 
00122         {
00123                 char *stop;
00124                 *colon = '\0';
00125                 colon++;
00126                 port_ = strtol(colon, &stop, 10);
00127                 host_ = serverName;
00128                 colon--;
00129                 *colon = ':';
00130         }       
00131 
00132         ProgressDialog::instance()->progressChange(
00133                 LANG_RESOURCE_3("CONNECTING_TO", 
00134                         "Connecting to \"{0}:{1}\" ({3})....", 
00135                         host_, 
00136                         S3D::formatStringBuffer("%i", port_), 
00137                         S3D::formatStringBuffer("%i", tryCount_)), 0);
00138 
00139         connectionState_ = eTryingConnection;
00140         if (ClientParams::instance()->getConnectedToServer())
00141         {
00142                 // Do in a thread so connect can block if it wants!
00143                 SDL_CreateThread(ConnectDialog::tryRemoteConnection, 0);
00144         }
00145         else
00146         {
00147                 // Or connect localy
00148                 tryLocalConnection();
00149         }
00150 }
00151 
00152 int ConnectDialog::tryRemoteConnection(void *)
00153 {
00154         char *host = (char *) instance_->host_.c_str();
00155         int port = instance_->port_;
00156 
00157         // Try to connect to the server
00158         ScorchedClient::instance()->getNetInterface().connect(host, port);
00159 
00160         // Wait for result
00161         instance_->connectionState_ = eWaiting;
00162         return 0;
00163 }
00164 
00165 void ConnectDialog::tryLocalConnection()
00166 {
00167         // Try to connect localy
00168         ScorchedClient::instance()->getNetInterface().connect("Local", 0);
00169 
00170         // Wait for result
00171         connectionState_ = eWaiting;;
00172 }
00173 
00174 void ConnectDialog::connected()
00175 {
00176         ProgressDialog::instance()->progressChange(LANG_RESOURCE("CONNECTED", "Connected"), 100);
00177 
00178         // Wait for the coms to start
00179         for (int i=0; i<10 && !ScorchedClient::instance()->getNetInterface().started(); i++)
00180         {
00181                 SDL_Delay(500);
00182         }
00183 
00184         // If we connected then send our details to the server
00185         ComsConnectMessage connectMessage;
00186         connectMessage.setVersion(S3D::ScorchedVersion.c_str());
00187         connectMessage.setProtocolVersion(S3D::ScorchedProtocolVersion.c_str());
00188 
00189         if (!ComsMessageSender::sendToServer(connectMessage))
00190         {
00191                 ScorchedClient::instance()->getNetInterface().stop();
00192 
00193                 LangString msg = LANG_RESOURCE_2("FAILED_TO_CONNECT_SEND",
00194                         "Failed to connect to server \"{0}:{1}\", send failed.",
00195                         host_,
00196                         port_);
00197                 MsgBoxDialog::instance()->show(msg);
00198 
00199                 ScorchedClient::instance()->getNetInterface().stop();
00200                 ScorchedClient::instance()->getGameState().stimulate(
00201                         ClientState::StimOptions);
00202         }
00203 
00204         connectionState_ = eFinished;
00205 }

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3