ClientConnectionAuthHandler.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 <client/ClientConnectionAuthHandler.h>
00022 #include <client/ScorchedClient.h>
00023 #include <client/ClientParams.h>
00024 #include <client/ClientState.h>
00025 #include <common/OptionsScorched.h>
00026 #include <common/Logger.h>
00027 #include <server/ScorchedServer.h>
00028 #include <dialogs/ConnectDialog.h>
00029 #include <dialogs/MsgBoxDialog.h>
00030 #include <dialogs/ProgressDialog.h>
00031 #include <dialogs/AuthDialog.h>
00032 #include <GLW/GLWWindowManager.h>
00033 #include <coms/ComsConnectAuthMessage.h>
00034 #include <coms/ComsMessageSender.h>
00035 #include <graph/OptionsDisplay.h>
00036 #include <tank/TankContainer.h>
00037 #include <client/SecureID.h>
00038 #include <net/NetInterface.h>
00039 
00040 ClientConnectionAuthHandler *ClientConnectionAuthHandler::instance_ = 0;
00041 
00042 ClientConnectionAuthHandler *ClientConnectionAuthHandler::instance()
00043 {
00044         if (!instance_)
00045         {
00046           instance_ = new ClientConnectionAuthHandler();
00047         }
00048 
00049         return instance_;
00050 }
00051 
00052 ClientConnectionAuthHandler::ClientConnectionAuthHandler()
00053 {
00054         ScorchedClient::instance()->getComsMessageHandler().addHandler(
00055                 "ComsConnectAuthMessage",
00056                 this);
00057 }
00058 
00059 ClientConnectionAuthHandler::~ClientConnectionAuthHandler()
00060 {
00061 
00062 }
00063 
00064 bool ClientConnectionAuthHandler::processMessage(
00065         NetMessage &netMessage,
00066         const char *messageType,
00067         NetBufferReader &reader)
00068 {
00069         ComsConnectAuthMessage message;
00070         if (!message.readMessage(reader)) return false;
00071 
00072         bool nameRequired = (0 == strcmp(message.getUserName(), "required"));
00073         bool passwordRequired = (0 == strcmp(message.getPassword(), "required"));
00074         if ((passwordRequired && !ClientParams::instance()->getPassword()[0]) ||
00075                 (nameRequired && !ClientParams::instance()->getUserName()[0]))
00076         {
00077                 AuthDialog::instance()->setRequiredAuth(
00078                         (nameRequired?AuthDialog::eNameRequired:0) |
00079                         (passwordRequired?AuthDialog::ePasswordRequired:0));
00080                 GLWWindowManager::instance()->showWindow(
00081                         AuthDialog::instance()->getId());
00082         }
00083         else
00084         {
00085                 sendAuth();
00086         }
00087 
00088         return true;
00089 }
00090 
00091 void ClientConnectionAuthHandler::sendAuth()
00092 {
00093         ProgressDialog::instance()->progressChange(LANG_RESOURCE("AUTHENTICATING", "Authenticating"), 100);
00094 
00095         const char *hostName = ConnectDialog::instance()->getHost();
00096         int portNumber = ConnectDialog::instance()->getPort();
00097 
00098         // Update unique id store
00099         std::string uniqueId, SUI;
00100         if (ClientParams::instance()->getConnectedToServer())
00101         {
00102                 IPaddress address;
00103                 if (SDLNet_ResolveHost(&address, (char *) hostName, 0) == 0)
00104                 {
00105                         unsigned int ipAddress = SDLNet_Read32(&address.host);
00106                         uniqueId = ConnectDialog::instance()->getIdStore().getUniqueId(ipAddress);
00107 
00108                         SecureID MakeKey;
00109                         SUI = MakeKey.getSecureID(ipAddress);
00110                 }
00111         }
00112 
00113         // Check the number of players that are connecting
00114         unsigned int noPlayers = 1;
00115         if (!ClientParams::instance()->getConnectedToServer())
00116         {
00117                 noPlayers = ScorchedServer::instance()->getOptionsGame().getNoMaxPlayers() -
00118                         ScorchedServer::instance()->getTankContainer().getNoOfTanks();
00119         }
00120 
00121         ComsConnectAuthMessage connectMessage;
00122 
00123         connectMessage.setUserName(ClientParams::instance()->getUserName());
00124         connectMessage.setPassword(ClientParams::instance()->getPassword());
00125         connectMessage.setUniqueId(uniqueId.c_str());
00126         connectMessage.setSUI(SUI.c_str());
00127         connectMessage.setHostDesc(OptionsDisplay::instance()->getHostDescription());
00128         connectMessage.setNoPlayers(noPlayers);
00129         connectMessage.setCompatabilityVer((unsigned int) OptionsDisplay::instance()->getOptions().size());
00130 
00131         if (!ComsMessageSender::sendToServer(connectMessage))
00132         {
00133                 LangString msg = LANG_RESOURCE_2("FAILED_TO_SEND_AUTH",
00134                         "Failed to send auth to server \"{0}:{1}\", send failed.",
00135                         hostName, portNumber);
00136                 MsgBoxDialog::instance()->show(msg);
00137 
00138                 cancelAuth();
00139         }
00140 }
00141 
00142 void ClientConnectionAuthHandler::cancelAuth()
00143 {
00144         ScorchedClient::instance()->getNetInterface().stop();
00145         ScorchedClient::instance()->getGameState().stimulate(
00146                 ClientState::StimOptions);
00147 }
00148 

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