ClientKeepAliveSender.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/ClientKeepAliveSender.h>
00022 #include <client/ScorchedClient.h>
00023 #include <coms/ComsKeepAliveMessage.h>
00024 #include <coms/ComsMessageSender.h>
00025 #include <net/NetInterface.h>
00026 #include <common/OptionsScorched.h>
00027 #include <common/Logger.h>
00028 #include <time.h>
00029 
00030 ClientKeepAliveSender *ClientKeepAliveSender::instance_ = 0;
00031 
00032 ClientKeepAliveSender *ClientKeepAliveSender::instance()
00033 {
00034         if (!instance_)
00035         {
00036                 instance_ = new ClientKeepAliveSender;
00037         }
00038         return instance_;
00039 }
00040 
00041 ClientKeepAliveSender::ClientKeepAliveSender() : 
00042         lastSendTime_(0), recvMessage_(true), sendThread_(0)
00043 {
00044         ComsKeepAliveMessage message;
00045         message.writeTypeMessage(buffer_);
00046         message.writeMessage(buffer_);
00047         buffer_.compressBuffer();
00048 }
00049 
00050 ClientKeepAliveSender::~ClientKeepAliveSender()
00051 {
00052 }
00053 
00054 void ClientKeepAliveSender::sendKeepAlive()
00055 {
00056         if (ScorchedClient::instance()->getOptionsGame().getKeepAliveTimeoutTime() == 0)
00057         {
00058                 return;
00059         }
00060 
00061         if (sendThread_)
00062         {
00063                 return;
00064         }
00065 
00066         lastSendTime_ = (unsigned int) time(0);
00067 
00068         // Create the processing thread
00069         sendThread_ = SDL_CreateThread(ClientKeepAliveSender::sendThreadFunc, 0);
00070         if (sendThread_ == 0)
00071         {
00072                 Logger::log(S3D::formatStringBuffer("ClientKeepAliveSender: Failed to create send thread"));
00073                 return;
00074         }       
00075 }
00076 
00077 int ClientKeepAliveSender::sendThreadFunc(void *)
00078 {
00079         while (true)
00080         {
00081                 SDL_Delay(500);
00082                 instance_->send();
00083         }
00084         return 1;
00085 }
00086 
00087 void ClientKeepAliveSender::send()
00088 {
00089         unsigned int sendTime = (unsigned int)
00090                 ScorchedClient::instance()->getOptionsGame().getKeepAliveTime();
00091         unsigned int theTime = (unsigned int) time(0);
00092 
00093         if (theTime - lastSendTime_ >= sendTime)
00094         {
00095                 if (theTime - lastSendTime_ >= sendTime + 5)
00096                 {
00097                         Logger::log("Warning: Keepalive sender falling behind");
00098                 }
00099 
00100                 // Use this directly as it is thread safe
00101                 if (ScorchedClient::instance()->getNetInterface().started())
00102                 {
00103                         ScorchedClient::instance()->getNetInterface().sendMessageServer(
00104                                 buffer_, NetInterfaceFlags::fAsync);
00105                 }
00106 
00107                 lastSendTime_ = theTime;
00108         }
00109 }

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