summaryrefslogtreecommitdiffstats
path: root/source/packets/cPacket.cpp
blob: e49c3e016526dc7736ccbc703b972afad741136e (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "cPacket.h"
#include "cMCLogger.h"
#include "Endianness.h"

#ifdef _WIN32
#define MSG_NOSIGNAL (0)
#endif

#ifdef __MAC_NA
#define MSG_NOSIGNAL (0)
#endif

//*****************************************************************************
// Blocking receive all function
//*****************************************************************************
int cPacket::RecvAll( SOCKET a_Socket, char* a_Data, unsigned int a_Size, int a_Options )
{
	unsigned int RequestSize = a_Size;
	while(a_Size != 0)
	{
		int Num = recv(a_Socket, a_Data, a_Size, a_Options);
		if( cSocket::IsSocketError( Num ) )
			return Num;
		a_Size -= Num;
		a_Data += Num;
	}
	return RequestSize - a_Size;
}

//*****************************************************************************
// Own implementation of send()
//*****************************************************************************
int cPacket::SendData( SOCKET a_Socket, const char* a_Message, unsigned int a_Size, int a_Options )
{
	return send(a_Socket, a_Message, a_Size, a_Options | MSG_NOSIGNAL );
}


//*****************************************************************************
// New packets
//*****************************************************************************

bool cPacket::ReadString( std::string & a_OutString )
{
	short StrLen;
	if(!ReadShort( StrLen )) return false;

	if( StrLen == 0 )
	{
		a_OutString.clear();
		return true;
	}

	char* cString = new char[StrLen];
	if( cSocket::IsSocketError( RecvAll( m_Socket, cString, StrLen, 0 ) ) ) return false;

	a_OutString.assign( cString, StrLen );

	//printf("Discoved string: %s size: %i\n", a_OutString.c_str(), a_OutString.size() );
	delete [] cString;
	return true;
}

bool cPacket::ReadString16( std::string & a_OutString )
{
	short StrLen;
	if(!ReadShort( StrLen )) return false;

	a_OutString.clear();
	if( StrLen == 0 )
	{
		return true;
	}

	char* UTF16 = new char[StrLen*sizeof( short )];
	if( cSocket::IsSocketError( RecvAll( m_Socket, UTF16, StrLen * sizeof( short ), 0 ) ) ) return false;

	for( int i = 0; i < StrLen; ++i )
		a_OutString.push_back( (char)UTF16[i*sizeof( short )+1] );

	//printf("Discoved string: %s size: %i\n", a_OutString.c_str(), a_OutString.size() );
	delete [] UTF16;
	return true;
}

bool cPacket::ReadShort( short & a_OutShort )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutShort, sizeof(short), 0 ) ) ) return false;
	a_OutShort = ntohs(a_OutShort);
	return true;
}

bool cPacket::ReadInteger( int & a_OutInteger )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutInteger, sizeof(int), 0 ) ) ) return false;
	a_OutInteger = ntohl(a_OutInteger);
	return true;
}

bool cPacket::ReadInteger( unsigned int & a_OutInteger )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutInteger, sizeof(unsigned int), 0 ) ) ) return false;
	a_OutInteger = ntohl(a_OutInteger);
	return true;
}

bool cPacket::ReadFloat( float & a_OutFloat )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutFloat, sizeof(float), 0 ) ) ) return false;
	a_OutFloat = NetworkToHostFloat4( &a_OutFloat );
	return true;
}

bool cPacket::ReadDouble( double & a_OutDouble )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutDouble, sizeof(double), 0 ) ) ) return false;
	a_OutDouble = NetworkToHostDouble8( &a_OutDouble );
	return true;
}

bool cPacket::ReadByte( char & a_OutByte )
{
	return !cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutByte, sizeof(char), 0 ) );
}

bool cPacket::ReadByte( unsigned char & a_OutByte )
{
	return !cSocket::IsSocketError(RecvAll( m_Socket, (char*)&a_OutByte, sizeof(char), 0 ) );
}

bool cPacket::ReadLong( long long & a_OutLong )
{
	if( cSocket::IsSocketError( RecvAll( m_Socket, (char*)&a_OutLong, sizeof(long long), 0 ) ) ) return false;
	a_OutLong = NetworkToHostLong8( &a_OutLong );
	return true;
}

bool cPacket::ReadBool( bool & a_OutBool )
{
	if( cSocket::IsSocketError(RecvAll( m_Socket, (char*)&a_OutBool, sizeof(bool), 0 ) ) ) return false;
	return true;
}

//*****************************************************************************
// Append variables to a c-String
//*****************************************************************************
void cPacket::AppendString( std::string & a_String, char* a_Dst, unsigned int & a_Iterator )
{
	AppendShort( (unsigned short)a_String.size(), a_Dst, a_Iterator );
	memcpy( a_Dst + a_Iterator, a_String.c_str(), a_String.size() ); a_Iterator += a_String.size();
}

void cPacket::AppendString16( std::string & a_String, char* a_Dst, unsigned int & a_Iterator )
{
	AppendShort( (unsigned short)a_String.size(), a_Dst, a_Iterator );
	char* UTF16 = new char[ a_String.size() * sizeof( short ) ];
	for( unsigned int i = 0; i < a_String.size(); ++i )
	{
		UTF16[i*sizeof( short )]	= 0x00;//a_String[i];
		UTF16[i*sizeof( short )+1]	= a_String[i];
	}
	memcpy( a_Dst + a_Iterator, UTF16, a_String.size() * sizeof( short ) ); a_Iterator += a_String.size() * sizeof( short );
	delete [] UTF16;
}

void cPacket::AppendShort( short a_Short, char *a_Dst, unsigned int &a_Iterator )
{
	short ConvertedShort = htons( a_Short );
	memcpy( a_Dst + a_Iterator, &ConvertedShort, sizeof( short ) ); a_Iterator+=sizeof( short );
}

void cPacket::AppendShort( unsigned short a_Short, char *a_Dst, unsigned int &a_Iterator )
{
	short ConvertedShort = htons( a_Short );
	memcpy( a_Dst + a_Iterator, &ConvertedShort, sizeof( unsigned short ) ); a_Iterator+=sizeof( unsigned short );
}


void cPacket::AppendInteger( int a_Integer, char* a_Dst, unsigned int & a_Iterator )
{
	int ConvertedInt = htonl( a_Integer );
	memcpy( a_Dst + a_Iterator, &ConvertedInt, sizeof( int ) ); a_Iterator+=sizeof( int );
}

void cPacket::AppendInteger( unsigned int a_Integer, char* a_Dst, unsigned int & a_Iterator )
{
	unsigned int ConvertedInt = htonl( a_Integer );
	memcpy( a_Dst + a_Iterator, &ConvertedInt, sizeof( unsigned int ) ); a_Iterator+=sizeof( unsigned int );
}

void cPacket::AppendFloat( float a_Float, char* a_Dst, unsigned int & a_Iterator )
{
	unsigned int ConvertedFloat = HostToNetwork4(&a_Float);
	memcpy( a_Dst + a_Iterator, &ConvertedFloat, sizeof(float) ); a_Iterator += sizeof(float);
}

void cPacket::AppendDouble( double & a_Double, char* a_Dst, unsigned int & a_Iterator )
{
	unsigned long long ConvertedDouble = HostToNetwork8(&a_Double);
	memcpy( a_Dst + a_Iterator, &ConvertedDouble, sizeof(double) ); a_Iterator += sizeof(double);
}

void cPacket::AppendByte( char a_Byte, char* a_Dst, unsigned int & a_Iterator )
{
	a_Dst[a_Iterator] = a_Byte; a_Iterator+=sizeof(char);
}

void cPacket::AppendLong( long long & a_Long, char* a_Dst, unsigned int & a_Iterator )
{
	unsigned long long ConvertedLong = HostToNetwork8(&a_Long);
	memcpy( a_Dst + a_Iterator, &ConvertedLong, sizeof(long long) );
	a_Iterator += sizeof( long long );
}

void cPacket::AppendBool( bool a_Bool, char* a_Dst, unsigned int & a_Iterator )
{
	a_Dst[a_Iterator] = (char)a_Bool; a_Iterator+=sizeof(bool);
}

void cPacket::AppendData( char* a_Data, unsigned int a_Size, char* a_Dst, unsigned int & a_Iterator )
{
	memcpy( a_Dst + a_Iterator, a_Data, a_Size ); a_Iterator += a_Size;
}