NetServerTCP3Recv.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/NetServerTCP3Recv.h>
00022 #include <net/NetServerTCP3Coms.h>
00023 #include <net/NetMessagePool.h>
00024 #include <common/Logger.h>
00025 
00026 NetServerTCP3Recv::NetServerTCP3Recv(
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         messagesRecieved_(0), bytesIn_(0),
00034         stopped_(false), running_(true)
00035 {
00036         socketSet_ = SDLNet_AllocSocketSet(1);
00037         SDLNet_TCP_AddSocket(socketSet_, socket_);
00038         recvThread_ = SDL_CreateThread(
00039                 NetServerTCP3Recv::recvThreadFunc, (void *) this);
00040         if (recvThread_ == 0)
00041         {
00042                 Logger::log(
00043                         "NetServerTCP3Recv: Failed to create recv thread");
00044         }
00045 }
00046 
00047 NetServerTCP3Recv::~NetServerTCP3Recv()
00048 {
00049         SDLNet_FreeSocketSet(socketSet_);
00050         socketSet_ = 0;
00051 }
00052 
00053 int NetServerTCP3Recv::recvThreadFunc(void *c)
00054 {
00055         // Call a non-static class thread to do the processing in (just for convienience)
00056         NetServerTCP3Recv *th = (NetServerTCP3Recv*) c;
00057         while (th->running_)
00058         {
00059                 if (!th->actualRecvFunc()) break;
00060                 SDL_Delay(100);
00061         }
00062         th->stopped_ = true;
00063         return 0;
00064 }
00065 
00066 bool NetServerTCP3Recv::actualRecvFunc()
00067 {
00068         // Check if there is anything to recieve
00069         int numready = SDLNet_CheckSockets(socketSet_, 100);
00070         if (numready == -1) return false;
00071         if (numready == 0) return true;
00072 
00073         // Receive the length of the string message
00074         char lenbuf[4];
00075         if (!NetServerTCP3Coms::SDLNet_TCP_Recv_Full(socket_, lenbuf, 4))
00076         {
00077                 Logger::log(S3D::formatStringBuffer(
00078                         "NetServerTCP3Recv: Read failed for length"));
00079                 return false;
00080         }
00081         Uint32 len = SDLNet_Read32(lenbuf);
00082         
00083         // Cannot recieve a message large than .5 MB
00084         if (len > 5000000 || len == 0)
00085         {
00086                 Logger::log(S3D::formatStringBuffer(
00087                         "NetServerTCP3Recv: Buffer was too large to recieve.  Size %i.",
00088                         len));
00089                 return false;
00090         }
00091 
00092         // allocate the buffer memory
00093         NetMessage *buffer = NetMessagePool::instance()->
00094                 getFromPool(NetMessage::BufferMessage, 
00095                                 destinationId_, ipAddress_);
00096         buffer->getBuffer().allocate(len);
00097         buffer->getBuffer().setBufferUsed(len);
00098 
00099         // get the string buffer over the socket
00100         if (!NetServerTCP3Coms::SDLNet_TCP_Recv_Full(socket_, 
00101                 buffer->getBuffer().getBuffer(),
00102                 len))
00103         {
00104                 Logger::log(S3D::formatStringBuffer(
00105                         "NetServerTCP3Recv: Read failed for buffer"));
00106                 NetMessagePool::instance()->addToPool(buffer);
00107                 return false;
00108         }
00109 
00110         // Notify that this message has been recieved
00111         NetInterface::getBytesIn() += len;
00112         bytesIn_ += len;
00113         messagesRecieved_ ++;
00114         recieveMessageHandler_->addMessage(buffer);
00115 
00116         return true;
00117 }
00118 
00119 void NetServerTCP3Recv::wait()
00120 {
00121         int status;
00122         SDL_WaitThread(recvThread_, &status);
00123 }

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