6e8fbca745
match the genesis editor version 1.3.0.653.
236 lines
6.3 KiB
C++
236 lines
6.3 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2006, Radon Labs GmbH
|
|
Copyright (c) 2011-2013,WebJet Business Division,CYOU
|
|
|
|
http://www.genesis-3d.com.cn
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "stdneb.h"
|
|
#include "net/tcp/stdtcpclientconnection.h"
|
|
#include "io/memorystream.h"
|
|
|
|
namespace Net
|
|
{
|
|
__ImplementClass(Net::StdTcpClientConnection, 'STCC', Core::RefCounted);
|
|
|
|
using namespace Util;
|
|
using namespace IO;
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
StdTcpClientConnection::StdTcpClientConnection()
|
|
{
|
|
// empty
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
StdTcpClientConnection::~StdTcpClientConnection()
|
|
{
|
|
this->Shutdown();
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
bool
|
|
StdTcpClientConnection::Connect(const GPtr<Socket>& sock)
|
|
{
|
|
n_assert(!this->socket.isvalid());
|
|
n_assert(sock.isvalid());
|
|
n_assert(sock->IsOpen());
|
|
|
|
// check if socket is really actually connected
|
|
if (sock->IsConnected())
|
|
{
|
|
this->socket = sock;
|
|
this->socket->SetBlocking(true);
|
|
this->socket->SetNoDelay(true);
|
|
this->sendStream = MemoryStream::Create();
|
|
this->recvStream = MemoryStream::Create();
|
|
n_printf("Client from addr=%s connected!\n",
|
|
this->socket->GetAddress().GetHostAddr().AsCharPtr());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
bool
|
|
StdTcpClientConnection::IsConnected() const
|
|
{
|
|
if (this->socket.isvalid())
|
|
{
|
|
return this->socket->IsConnected();
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
void
|
|
StdTcpClientConnection::Shutdown()
|
|
{
|
|
if (this->socket.isvalid())
|
|
{
|
|
this->socket->Close();
|
|
this->socket = 0;
|
|
}
|
|
this->sendStream = 0;
|
|
this->recvStream = 0;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
const IpAddress&
|
|
StdTcpClientConnection::GetClientAddress() const
|
|
{
|
|
n_assert(this->socket.isvalid());
|
|
return this->socket->GetAddress();
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
Socket::Result
|
|
StdTcpClientConnection::Send(const GPtr<Stream>& stream)
|
|
{
|
|
n_assert(stream.isvalid());
|
|
if (stream->GetSize() == 0)
|
|
{
|
|
// nothing to send
|
|
return Socket::Success;
|
|
}
|
|
|
|
Socket::Result res = Socket::Success;
|
|
stream->SetAccessMode(Stream::ReadAccess);
|
|
if (stream->Open())
|
|
{
|
|
// we may not exceed the maximum message size...
|
|
// so we may have to split the send data into
|
|
// multiple packets
|
|
SizeT maxMsgSize = this->socket->GetMaxMsgSize();
|
|
SizeT sendSize = stream->GetSize();
|
|
uchar* ptr = (uchar*) stream->Map();
|
|
SizeT overallBytesSent = 0;
|
|
while ((Socket::Success == res) && (overallBytesSent < sendSize))
|
|
{
|
|
SizeT bytesToSend = sendSize - overallBytesSent;
|
|
if (bytesToSend > maxMsgSize)
|
|
{
|
|
bytesToSend = maxMsgSize;
|
|
}
|
|
SizeT bytesSent = 0;
|
|
res = this->socket->Send(ptr, bytesToSend, bytesSent);
|
|
ptr += bytesSent;
|
|
overallBytesSent += bytesSent;
|
|
}
|
|
stream->Unmap();
|
|
stream->Close();
|
|
}
|
|
return res;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
Socket::Result
|
|
StdTcpClientConnection::Send()
|
|
{
|
|
n_assert(this->sendStream.isvalid());
|
|
Socket::Result res = this->Send(this->sendStream);
|
|
this->sendStream->SetSize(0);
|
|
return res;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
Socket::Result
|
|
StdTcpClientConnection::Recv()
|
|
{
|
|
n_assert(this->recvStream.isvalid());
|
|
this->recvStream->SetAccessMode(Stream::WriteAccess);
|
|
this->recvStream->SetSize(0);
|
|
Socket::Result res = Socket::Success;
|
|
if (this->recvStream->Open())
|
|
{
|
|
// NOTE: the following loop will make sure that Recv()
|
|
// never blocks
|
|
uchar buf[1024];
|
|
while ((Socket::Success == res) && (this->socket->HasRecvData()))
|
|
{
|
|
SizeT bytesReceived = 0;
|
|
res = this->socket->Recv(&buf, sizeof(buf), bytesReceived);
|
|
if ((bytesReceived > 0) && (Socket::Success == res))
|
|
{
|
|
this->recvStream->Write(buf, bytesReceived);
|
|
}
|
|
}
|
|
this->recvStream->Close();
|
|
}
|
|
this->recvStream->SetAccessMode(Stream::ReadAccess);
|
|
if (Socket::Success == res)
|
|
{
|
|
if (this->recvStream->GetSize() > 0)
|
|
{
|
|
return Socket::Success;
|
|
}
|
|
else
|
|
{
|
|
return Socket::WouldBlock;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
const GPtr<Stream>&
|
|
StdTcpClientConnection::GetSendStream()
|
|
{
|
|
return this->sendStream;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
/**
|
|
*/
|
|
const GPtr<Stream>&
|
|
StdTcpClientConnection::GetRecvStream()
|
|
{
|
|
return this->recvStream;
|
|
}
|
|
|
|
} // namespace Net
|