NetServerTCP3Send.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 <net/NetServerTCP3Send.h>
00022 #include <net/NetServerTCP3Coms.h>
00023 #include <net/NetMessagePool.h>
00024 #include <common/Logger.h>
00025 
00026 NetServerTCP3Send::NetServerTCP3Send(
00027         TCPsocket socket, 
00028         unsigned int destinationId, unsigned int ipAddress,
00029         NetMessageHandler *recieveMessageHandler) :
00030         socket_(socket), 
00031         destinationId_(destinationId), ipAddress_(ipAddress),
00032         recieveMessageHandler_(recieveMessageHandler),
00033         messagesSent_(0), bytesOut_(0),
00034         stopped_(false), running_(true)
00035 {
00036         sendMessageHandler_.setMessageHandler(this);
00037         sendThread_ = SDL_CreateThread(
00038                 NetServerTCP3Send::sendThreadFunc, (void *) this);
00039         if (sendThread_ == 0)
00040         {
00041                 Logger::log(
00042                         "NetServerTCP3Send: Failed to create send thread");
00043         }
00044 }
00045 
00046 NetServerTCP3Send::~NetServerTCP3Send()
00047 {
00048         std::list<NetMessage *>::iterator itor;
00049         for (itor = outgoingMessages_.begin();
00050                 itor != outgoingMessages_.end();
00051                 itor++)
00052         {
00053                 NetMessagePool::instance()->addToPool(*itor);
00054         }
00055 }
00056 
00057 void NetServerTCP3Send::sendMessage(NetMessage *message) 
00058 {
00059         sendMessageHandler_.addMessage(message);
00060 }
00061 
00062 int NetServerTCP3Send::sendThreadFunc(void *c)
00063 {
00064         // Call a non-static class thread to do the processing in (just for convienience)
00065         NetServerTCP3Send *th = (NetServerTCP3Send*) c;
00066         while (th->running_)
00067         {
00068                 if (!th->actualSendFunc()) break;
00069                 SDL_Delay(100);
00070         }
00071         th->stopped_ = true;
00072         return 0;
00073 }
00074 
00075 bool NetServerTCP3Send::actualSendFunc()
00076 {
00077         // Get any new messages
00078         sendMessageHandler_.processMessages();
00079         if (outgoingMessages_.empty()) return true;
00080 
00081         // Send first message
00082         NetMessage *message = outgoingMessages_.front();
00083 
00084         // Send the length of the string
00085         Uint32 len = message->getBuffer().getBufferUsed();
00086         Uint32 netlen = 0;
00087         SDLNet_Write32(len, &netlen);
00088 
00089         int result = NetServerTCP3Coms::SDLNet_TCP_Send_Wrapper(
00090                 socket_, &netlen, sizeof(netlen));
00091         if(result < (int) sizeof(netlen))
00092         {
00093                 Logger::log(S3D::formatStringBuffer(
00094                         "NetServerTCP3Send: Failed to send buffer length. Sent %i of %i.",
00095                         result, sizeof(netlen)));
00096                 return false;
00097         }
00098         
00099         // Send the buffer
00100         result = NetServerTCP3Coms::SDLNet_TCP_Send_Wrapper(
00101                 socket_, message->getBuffer().getBuffer(), len);
00102         if(result<int(len))
00103         {
00104                 Logger::log(S3D::formatStringBuffer(
00105                         "NetServerTCP3Send: Failed to send buffer. Sent %i of %i.",
00106                         result, int(len)));
00107                 return false;
00108         }
00109         
00110         // Notify that this message has been sent
00111         outgoingMessages_.pop_front();
00112         recieveMessageHandler_->addMessage(message);
00113         NetInterface::getBytesOut() += len;
00114         bytesOut_ += len;
00115         messagesSent_++;
00116 
00117         return true;
00118 }
00119 
00120 void NetServerTCP3Send::processMessage(NetMessage &oldmessage)
00121 {
00122         // Get a new buffer from the pool
00123         NetMessage *message = NetMessagePool::instance()->
00124                 getFromPool(NetMessage::SentMessage, 
00125                         oldmessage.getDestinationId(), 
00126                         ipAddress_);
00127 
00128         // Copy old buffer into new buffer
00129         NetBuffer &buffer = oldmessage.getBuffer();
00130         message->getBuffer().allocate(buffer.getBufferUsed());
00131         memcpy(message->getBuffer().getBuffer(), 
00132                 buffer.getBuffer(), buffer.getBufferUsed());
00133         message->getBuffer().setBufferUsed(buffer.getBufferUsed());
00134 
00135         // Add to list of outgoing
00136         outgoingMessages_.push_back(message);
00137 }
00138 
00139 void NetServerTCP3Send::wait()
00140 {
00141         int status;
00142         SDL_WaitThread(sendThread_, &status);
00143 }

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