2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cBlockingTCPLink.h"
|
|
|
|
#include "packets/cPacket.h"
|
2011-10-21 17:25:29 -04:00
|
|
|
#include "MCSocket.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-01-29 14:28:19 -05:00
|
|
|
#define MSG_NOSIGNAL (0)
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
#ifdef __MACH__
|
|
|
|
#define MSG_NOSIGNAL (0)
|
|
|
|
#endif
|
|
|
|
|
2012-02-01 07:46:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
cBlockingTCPLink::cBlockingTCPLink(void)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cBlockingTCPLink::~cBlockingTCPLink()
|
|
|
|
{
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cBlockingTCPLink::CloseSocket()
|
|
|
|
{
|
2012-02-02 11:13:49 -05:00
|
|
|
if (!m_Socket.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2011-10-21 17:25:29 -04:00
|
|
|
m_Socket.CloseSocket();
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
assert(!m_Socket.IsValid());
|
|
|
|
if (m_Socket.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
LOGWARN("WARNING: cTCPLink Connect() called while still connected.");
|
|
|
|
m_Socket.CloseSocket();
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *hp;
|
|
|
|
unsigned int addr;
|
|
|
|
struct sockaddr_in server;
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
m_Socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (!m_Socket.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
LOGERROR("cTCPLink: Cannot create a socket");
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
addr = inet_addr(iAddress);
|
|
|
|
hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
|
|
|
|
if (hp == NULL)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
//LOGWARN("cTCPLink: gethostbyaddr returned NULL");
|
2012-02-01 17:38:03 -05:00
|
|
|
hp = gethostbyname(iAddress);
|
|
|
|
if (hp == NULL)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
LOGWARN("cTCPLink: Could not resolve %s", iAddress);
|
2011-10-03 14:41:19 -04:00
|
|
|
CloseSocket();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
server.sin_addr.s_addr = *((unsigned long *)hp->h_addr);
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons( (unsigned short)iPort);
|
|
|
|
if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server)))
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-05 11:24:16 -05:00
|
|
|
LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort, cSocket::GetErrorString( cSocket::GetLastError() ).c_str() );
|
2011-10-03 14:41:19 -04:00
|
|
|
CloseSocket();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cBlockingTCPLink::Send(char * a_Data, unsigned int a_Size, int a_Flags /* = 0 */ )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
assert(m_Socket.IsValid());
|
|
|
|
if (!m_Socket.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
LOGERROR("cBlockingTCPLink: Trying to send data without a valid connection!");
|
2011-10-03 14:41:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
return m_Socket.Send(a_Data, a_Size);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
int cBlockingTCPLink::SendMessage( const char* a_Message, int a_Flags /* = 0 */ )
|
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
assert(m_Socket.IsValid());
|
|
|
|
if (!m_Socket.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
LOGWARN("cBlockingTCPLink: Trying to send message without a valid connection!");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
return m_Socket.Send(a_Message, strlen(a_Message));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cBlockingTCPLink::ReceiveData(AString & oData)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
assert(m_Socket.IsValid());
|
|
|
|
if (!m_Socket.IsValid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
int Received = 0;
|
|
|
|
char Buffer[256];
|
2012-02-01 17:38:03 -05:00
|
|
|
while ((Received = recv(m_Socket, Buffer, sizeof(Buffer), 0)) > 0)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
oData.append(Buffer, Received);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|