ClientConnectionAcceptHandler.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/ClientConnectionAcceptHandler.h>
00022 #include <client/ScorchedClient.h>
00023 #include <dialogs/RulesDialog.h>
00024 #include <dialogs/ConnectDialog.h>
00025 #include <dialogs/ProgressDialog.h>
00026 #include <engine/ModFiles.h>
00027 #include <image/ImageFactory.h>
00028 #include <image/ImagePng.h>
00029 #include <tank/TankContainer.h>
00030 #include <net/NetInterface.h>
00031 #include <coms/ComsConnectAcceptMessage.h>
00032 #include <coms/ComsHaveModFilesMessage.h>
00033 #include <coms/ComsMessageSender.h>
00034 #include <common/Logger.h>
00035 #include <common/OptionsScorched.h>
00036 #include <client/ClientParams.h>
00037 #include <common/Defines.h>
00038 
00039 ClientConnectionAcceptHandler *ClientConnectionAcceptHandler::instance_ = 0;
00040 
00041 ClientConnectionAcceptHandler *ClientConnectionAcceptHandler::instance()
00042 {
00043         if (!instance_)
00044         {
00045           instance_ = new ClientConnectionAcceptHandler();
00046         }
00047 
00048         return instance_;
00049 }
00050 
00051 ClientConnectionAcceptHandler::ClientConnectionAcceptHandler()
00052 {
00053         ScorchedClient::instance()->getComsMessageHandler().addHandler(
00054                 "ComsConnectAcceptMessage",
00055                 this);
00056 }
00057 
00058 ClientConnectionAcceptHandler::~ClientConnectionAcceptHandler()
00059 {
00060 
00061 }
00062 
00063 bool ClientConnectionAcceptHandler::processMessage(
00064         NetMessage &netMessage,
00065         const char *messageType,
00066         NetBufferReader &reader)
00067 {
00068         ComsConnectAcceptMessage message;
00069         if (!message.readMessage(reader)) return false;
00070 
00071         if (ClientParams::instance()->getConnectedToServer())
00072         {
00073                 unsigned int ip = netMessage.getIpAddress();
00074                 if (!ConnectDialog::instance()->getIdStore().saveUniqueId(
00075                         ip, message.getUniqueId(), message.getPublishAddress()))
00076                 {
00077                         Logger::log( "Server failed ip security check!");
00078                         return false;
00079                 }
00080         }
00081 
00082         // The server tells us what our id is.
00083         // Set this id so we know what our players are
00084         ScorchedClient::instance()->getTankContainer().
00085                 setCurrentDestinationId(message.getDestinationId());
00086 
00087         // Tell the user to wait
00088         Logger::log(S3D::formatStringBuffer(
00089                 "Connection accepted by \"%s\".\nPlease wait...",
00090                 message.getServerName()));
00091 
00092         // Set the server specific png for the current server
00093         {
00094                 GLTexture *texture = 0;
00095                 if (message.getServerPng().getBufferUsed() > 0)
00096                 {
00097                         // Use a custom icon
00098                         ImagePng map;
00099                         map.loadFromBuffer(message.getServerPng());
00100 
00101                         // Set the texture
00102                         texture = new GLTexture;
00103                         texture->create(map, false);
00104                 }
00105                 else
00106                 {
00107                         // Use the default icon
00108                         std::string file1(S3D::getDataFile("data/windows/scorched.png"));
00109                         std::string file2(S3D::getDataFile("data/windows/scorcheda.png"));
00110                         ImageHandle map = ImageFactory::loadImageHandle(
00111                                 file1.c_str(), file2.c_str(), false);
00112 
00113                         // Set the texture
00114                         texture = new GLTexture;
00115                         texture->create(map, false);
00116                 }
00117                 
00118                 RulesDialog::instance()->addIcon(texture);
00119         }
00120 
00121         // Set the mod
00122         S3D::setDataFileMod(
00123                 ScorchedClient::instance()->getOptionsGame().getMod());
00124 
00125         // Load any mod files we currently have for the mod
00126         // the server is using.
00127         if (ClientParams::instance()->getConnectedToServer())
00128         {
00129                 if (!ScorchedClient::instance()->getModFiles().loadModFiles(
00130                         ScorchedClient::instance()->getOptionsGame().getMod(), true,
00131                         ProgressDialogSync::instance()))
00132                 {
00133                         S3D::dialogMessage("ModFiles", 
00134                                 S3D::formatStringBuffer("Failed to load mod \"%s\"",
00135                                 ScorchedClient::instance()->getOptionsGame().getMod()));
00136                         return false;
00137                 }
00138         }
00139 
00140         // Tell the server what mod files we actually have
00141         ComsHaveModFilesMessage comsFileMessage;
00142         std::map<std::string, ModFileEntry *> &files = 
00143                 ScorchedClient::instance()->getModFiles().getFiles();
00144         std::map<std::string, ModFileEntry *>::iterator itor;
00145         for (itor = files.begin();
00146                 itor != files.end();
00147                 itor++)
00148         {
00149                 const std::string &name = (*itor).first;
00150                 ModFileEntry *file = (*itor).second;
00151 
00152                 comsFileMessage.getFiles().push_back(
00153                         ModIdentifierEntry(
00154                                 name.c_str(),
00155                                 file->getCompressedSize(),
00156                                 file->getCompressedCrc()));
00157         }
00158         if (!ComsMessageSender::sendToServer(comsFileMessage)) return false;
00159 
00160         return true;
00161 }

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