git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / net / NetServerTCP3Recv.cpp
blob0899a88def4cfbda4b61feaceee177fd47865602
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <net/NetServerTCP3Recv.h>
22 #include <net/NetServerTCP3Coms.h>
23 #include <net/NetMessagePool.h>
24 #include <common/Logger.h>
26 NetServerTCP3Recv::NetServerTCP3Recv(
27 TCPsocket socket,
28 unsigned int destinationId, unsigned int ipAddress,
29 NetMessageHandler *recieveMessageHandler) :
30 socket_(socket),
31 destinationId_(destinationId), ipAddress_(ipAddress),
32 recieveMessageHandler_(recieveMessageHandler),
33 messagesRecieved_(0), bytesIn_(0),
34 stopped_(false), running_(true)
36 socketSet_ = SDLNet_AllocSocketSet(1);
37 SDLNet_TCP_AddSocket(socketSet_, socket_);
38 recvThread_ = SDL_CreateThread(
39 NetServerTCP3Recv::recvThreadFunc, (void *) this);
40 if (recvThread_ == 0)
42 Logger::log(
43 "NetServerTCP3Recv: Failed to create recv thread");
47 NetServerTCP3Recv::~NetServerTCP3Recv()
49 SDLNet_FreeSocketSet(socketSet_);
50 socketSet_ = 0;
53 int NetServerTCP3Recv::recvThreadFunc(void *c)
55 // Call a non-static class thread to do the processing in (just for convienience)
56 NetServerTCP3Recv *th = (NetServerTCP3Recv*) c;
57 while (th->running_)
59 if (!th->actualRecvFunc()) break;
60 SDL_Delay(100);
62 th->stopped_ = true;
63 return 0;
66 bool NetServerTCP3Recv::actualRecvFunc()
68 // Check if there is anything to recieve
69 int numready = SDLNet_CheckSockets(socketSet_, 100);
70 if (numready == -1) return false;
71 if (numready == 0) return true;
73 // Receive the length of the string message
74 char lenbuf[4];
75 if (!NetServerTCP3Coms::SDLNet_TCP_Recv_Full(socket_, lenbuf, 4))
77 Logger::log(S3D::formatStringBuffer(
78 "NetServerTCP3Recv: Read failed for length"));
79 return false;
81 Uint32 len = SDLNet_Read32(lenbuf);
83 // Cannot recieve a message large than .5 MB
84 if (len > 5000000 || len == 0)
86 Logger::log(S3D::formatStringBuffer(
87 "NetServerTCP3Recv: Buffer was too large to recieve. Size %i.",
88 len));
89 return false;
92 // allocate the buffer memory
93 NetMessage *buffer = NetMessagePool::instance()->
94 getFromPool(NetMessage::BufferMessage,
95 destinationId_, ipAddress_);
96 buffer->getBuffer().allocate(len);
97 buffer->getBuffer().setBufferUsed(len);
99 // get the string buffer over the socket
100 if (!NetServerTCP3Coms::SDLNet_TCP_Recv_Full(socket_,
101 buffer->getBuffer().getBuffer(),
102 len))
104 Logger::log(S3D::formatStringBuffer(
105 "NetServerTCP3Recv: Read failed for buffer"));
106 NetMessagePool::instance()->addToPool(buffer);
107 return false;
110 // Notify that this message has been recieved
111 NetInterface::getBytesIn() += len;
112 bytesIn_ += len;
113 messagesRecieved_ ++;
114 recieveMessageHandler_->addMessage(buffer);
116 return true;
119 void NetServerTCP3Recv::wait()
121 int status;
122 SDL_WaitThread(recvThread_, &status);