00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00110 current->setKeepAlive(0);
00111 break;
00112 }
00113 }
00114 }
00115 }