summaryrefslogtreecommitdiffstats
path: root/source/packets/cPacket.cpp
blob: 8307e67faa6719d3642032dc92046228604594dd (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

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

#include "cPacket.h"
#include "../Endianness.h"





/*
// These checks cannot be done in preprocessor, since sizeof() is evaluated while compiling, so in preprocessing it's unknown.
// Check some basic type assumptions:
#if (sizeof(int) != 4)
	#error "Bad size for int, protocol won't work"
#endif

#if (sizeof(float) != 4)
	#error "Bad size for float, protocol won't work"
#endif

#if (sizeof(double) != 8)
	#error "Bad size for double, protocol won't work"
#endif
*/





void cPacket::AppendString(AString & a_Dst, const AString & a_String)
{
	AppendShort(a_Dst, (unsigned short)a_String.size());
	a_Dst.append(a_String);
}





void cPacket::AppendString16(AString & a_Dst, const AString & a_String)
{
	AppendShort(a_Dst, (unsigned short)a_String.size());
	AString UTF16;
	UTF16.resize(a_String.size() * sizeof(short));
	for( unsigned int i = 0; i < a_String.size(); ++i )
	{
		UTF16[i * sizeof( short )]     = 0x00;
		UTF16[i * sizeof( short ) + 1] = a_String[i];
	}
	a_Dst.append(UTF16.data(), a_String.size() * sizeof(short));
}





void cPacket::AppendShort(AString & a_Dst, short a_Short)
{
	short ConvertedShort = htons( a_Short );
	a_Dst.append((const char *)&ConvertedShort, sizeof(short));
}





void cPacket::AppendShort(AString & a_Dst, unsigned short a_Short)
{
	short ConvertedShort = htons( a_Short );
	a_Dst.append((const char *)&ConvertedShort, sizeof(short));
}





void cPacket::AppendInteger(AString & a_Dst, int a_Integer)
{
	int ConvertedInt = htonl( a_Integer );
	a_Dst.append((const char *)&ConvertedInt, sizeof(int));
}





void cPacket::AppendInteger(AString & a_Dst, unsigned int a_Integer)
{
	unsigned int ConvertedInt = htonl( a_Integer );
	a_Dst.append((const char *)&ConvertedInt, sizeof(int));
}





void cPacket::AppendFloat(AString & a_Dst, float a_Float)
{
	unsigned int ConvertedFloat = HostToNetwork4(&a_Float);
	a_Dst.append((const char *)&ConvertedFloat, sizeof(int));
}





void cPacket::AppendDouble(AString & a_Dst, const double & a_Double)
{
	unsigned long long ConvertedDouble = HostToNetwork8(&a_Double);
	a_Dst.append((const char *)&ConvertedDouble, 8);
}





void cPacket::AppendByte(AString & a_Dst, char a_Byte)
{
	a_Dst.append(&a_Byte, 1);
}





void cPacket::AppendLong(AString & a_Dst, const long long & a_Long)
{
	unsigned long long ConvertedLong = HostToNetwork8(&a_Long);
	a_Dst.append((const char *)&ConvertedLong, sizeof(a_Long));
}





void cPacket::AppendBool(AString & a_Dst, bool a_Bool)
{
	a_Dst.append((const char *)&a_Bool, 1);
}





void cPacket::AppendData(AString & a_Dst, const char * a_Data, unsigned int a_Size)
{
	a_Dst.append(a_Data, a_Size);
}