summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/BlockingTCPLink.cpp
blob: 07f48b955e21b48450d529b1c8f6b50ec8fff2a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "BlockingTCPLink.h"
#include "Errors.h"




cBlockingTCPLink::cBlockingTCPLink(void)
{
}





cBlockingTCPLink::~cBlockingTCPLink()
{
	CloseSocket();
}





void cBlockingTCPLink::CloseSocket()
{
	if (!m_Socket.IsValid())
	{
		m_Socket.CloseSocket();
	}
}





bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort)
{
	ASSERT(!m_Socket.IsValid());
	if (m_Socket.IsValid())
	{
		LOGWARN("WARNING: cTCPLink Connect() called while still connected.");
		m_Socket.CloseSocket();
	}

	struct hostent *hp;
	unsigned int addr;
	struct sockaddr_in server;

	m_Socket = socket(AF_INET, SOCK_STREAM, 0);
	if (!m_Socket.IsValid())
	{
		LOGERROR("cTCPLink: Cannot create a socket");
		return false;
	}

	addr = inet_addr(iAddress);
	hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
	if (hp == NULL)
	{
		//LOGWARN("cTCPLink: gethostbyaddr returned NULL");
		hp = gethostbyname(iAddress);
		if (hp == NULL)
		{
			LOGWARN("cTCPLink: Could not resolve %s", iAddress);
			CloseSocket();
			return false;
		}
	}

	memcpy(&server.sin_addr.s_addr,hp->h_addr, hp->h_length);
	server.sin_family = AF_INET;
	server.sin_port = htons( (unsigned short)iPort);
	if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server)))
	{
		LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort,GetOSErrorString( cSocket::GetLastError() ).c_str() );
		CloseSocket();
		return false;
	}

	return true;
}





int cBlockingTCPLink::Send(char * a_Data, unsigned int a_Size, int a_Flags /* = 0 */ )
{
	UNUSED(a_Flags);
	
	ASSERT(m_Socket.IsValid());
	if (!m_Socket.IsValid())
	{
		LOGERROR("cBlockingTCPLink: Trying to send data without a valid connection!");
		return -1;
	}
	return m_Socket.Send(a_Data, a_Size);
}





int cBlockingTCPLink::SendMessage( const char* a_Message, int a_Flags /* = 0 */ )
{
	UNUSED(a_Flags);
	
	ASSERT(m_Socket.IsValid());
	if (!m_Socket.IsValid())
	{
		LOGWARN("cBlockingTCPLink: Trying to send message without a valid connection!");
		return -1;
	}
	return m_Socket.Send(a_Message, strlen(a_Message));
}





void cBlockingTCPLink::ReceiveData(AString & oData)
{
	ASSERT(m_Socket.IsValid());
	if (!m_Socket.IsValid())
	{
		return;
	}

	int Received = 0;
	char Buffer[256];
	while ((Received = recv(m_Socket, Buffer, sizeof(Buffer), 0)) > 0)
	{
		oData.append(Buffer, Received);
	}
}