NetServerTCP3Destination.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/NetServerTCP3Destination.h>
00022 #include <common/Logger.h>
00023 
00024 NetServerTCP3Destination::NetServerTCP3Destination(
00025         NetMessageHandler *recieveMessageHandler, 
00026         TCPsocket socket,
00027         unsigned int destinationId) :
00028         socket_(socket), destinationId_(destinationId),
00029         send_(socket, destinationId, 
00030                 getIpAddressFromSocket(socket), recieveMessageHandler),
00031         recv_(socket, destinationId, 
00032                 getIpAddressFromSocket(socket), recieveMessageHandler),
00033         running_(true)
00034 {
00035         ipAddress_ = getIpAddressFromSocket(socket_);
00036 }
00037 
00038 NetServerTCP3Destination::~NetServerTCP3Destination()
00039 {
00040 }
00041 
00042 unsigned int NetServerTCP3Destination::getIpAddressFromSocket(TCPsocket socket)
00043 {
00044         unsigned int addr = 0;
00045         IPaddress *address = SDLNet_TCP_GetPeerAddress(socket);
00046         if (address)
00047         {
00048                 addr = SDLNet_Read32(&address->host);
00049         }
00050         return addr;
00051 }
00052 
00053 void NetServerTCP3Destination::printStats()
00054 {
00055         Logger::log(S3D::formatStringBuffer("TCP3 Destination %u net stats:", 
00056                 destinationId_));
00057         Logger::log(S3D::formatStringBuffer("  %u messages sent, %u bytes out",
00058                 send_.getMessagesSent(), send_.getBytesOut()));
00059         Logger::log(S3D::formatStringBuffer("  %u messages recieved, %u bytes in",
00060                 recv_.getMessagesRecieved(), recv_.getBytesIn()));
00061 }
00062 
00063 void NetServerTCP3Destination::sendMessage(NetMessage *message) 
00064 {
00065         send_.sendMessage(message);
00066 }
00067 
00068 void NetServerTCP3Destination::close()
00069 {
00070         send_.stop();
00071         recv_.stop();
00072         running_ = false;
00073 }
00074 
00075 bool NetServerTCP3Destination::anyFinished()
00076 {
00077         if (!running_ || send_.getStopped() || recv_.getStopped())
00078         {
00079                 running_ = false;
00080                 return true;
00081         }
00082         return false;
00083 }
00084 
00085 bool NetServerTCP3Destination::allFinished()
00086 {
00087         if (send_.getStopped() && recv_.getStopped())
00088         {
00089                 send_.wait();
00090                 recv_.wait();
00091                 SDLNet_TCP_Close(socket_);
00092                 return true;
00093         }
00094         return false;
00095 }

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