ServerKeepAliveHandler.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 <server/ServerKeepAliveHandler.h>
00022 #include <server/ServerChannelManager.h>
00023 #include <server/ServerCommon.h>
00024 #include <server/ScorchedServer.h>
00025 #include <common/Logger.h>
00026 #include <common/OptionsScorched.h>
00027 #include <tank/TankContainer.h>
00028 #include <coms/ComsKeepAliveMessage.h>
00029 #include <time.h>
00030 
00031 ServerKeepAliveHandler *ServerKeepAliveHandler::instance_ = 0;
00032 
00033 ServerKeepAliveHandler *ServerKeepAliveHandler::instance()
00034 {
00035         if (!instance_)
00036         {
00037                 instance_ = new ServerKeepAliveHandler;
00038         }
00039         return instance_;
00040 }
00041 
00042 ServerKeepAliveHandler::ServerKeepAliveHandler()
00043 {
00044         ScorchedServer::instance()->getComsMessageHandler().addHandler(
00045                 "ComsKeepAliveMessage",
00046                 this);
00047 }
00048 
00049 ServerKeepAliveHandler::~ServerKeepAliveHandler()
00050 {
00051 }
00052 
00053 bool ServerKeepAliveHandler::processMessage(NetMessage &message,
00054         const char *messageType, NetBufferReader &reader)
00055 {
00056         return true;
00057 }
00058 
00059 void ServerKeepAliveHandler::keepAlive(unsigned int destinationId)
00060 {
00061         unsigned int theTime = (unsigned int) time(0);
00062 
00063         std::map<unsigned int, Tank *> &tanks =
00064                 ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00065         std::map<unsigned int, Tank *>::iterator itor;
00066         for (itor = tanks.begin();
00067                 itor != tanks.end();
00068                 itor++)
00069         {
00070                 Tank *current = (*itor).second;
00071                 if (current->getDestinationId() == destinationId)
00072                 {
00073                         current->setKeepAlive(theTime);
00074                 }
00075         }
00076 }
00077 
00078 void ServerKeepAliveHandler::checkKeepAlives()
00079 {
00080         unsigned int allowedTime = (unsigned int)
00081                 ScorchedServer::instance()->getOptionsGame().getKeepAliveTimeoutTime();
00082         if (allowedTime == 0) return;
00083 
00084         unsigned int theTime = (unsigned int) time(0);
00085 
00086         std::map<unsigned int, Tank *> &tanks =
00087                 ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00088         std::map<unsigned int, Tank *>::iterator itor;
00089         for (itor = tanks.begin();
00090                 itor != tanks.end();
00091                 itor++)
00092         {
00093                 Tank *current = (*itor).second;
00094                 if (current->getDestinationId() != 0)
00095                 {
00096                         if (current->getKeepAlive() != 0 &&
00097                                 theTime - current->getKeepAlive()  > allowedTime)
00098                         {
00099                                 ServerChannelManager::instance()->sendText(
00100                                         ChannelText("info",
00101                                                 "KEEPALIVE_KICK",
00102                                                 "\"{0}\" Kicked for exceeding keep alive timeout ({1} seconds)",
00103                                                 current->getTargetName(),
00104                                                 theTime - current->getKeepAlive()),
00105                                         true);
00106 
00107                                 ServerCommon::kickDestination(current->getDestinationId());
00108 
00109                                 // To give more time until we repeat this message
00110                                 current->setKeepAlive(0); 
00111                                 break; // As now the tank container may be out of date
00112                         }
00113                 }
00114         }
00115 }

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