43 #ifndef _networkpacket_cpp
44 #define _networkpacket_cpp
46 #include "Network/packet.h"
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
70 bool Packet::VerifySize(
const size_t& Bytes)
const
72 return RawData.size() >= (ReadPos+Bytes);
75 void Packet::Append(
const void* Data,
size_t Bytes)
77 if(!Data || (Bytes <= 0))
79 size_t First = RawData.size();
80 RawData.resize(First + Bytes);
81 std::memcpy(&RawData[First],Data,Bytes);
89 size_t Packet::GetPacketSize()
const
91 return RawData.size();
94 bool Packet::EndOfPacket()
const
96 return (ReadPos >= RawData.size());
99 const std::vector<char>& Packet::GetRawData()
const
104 Packet& Packet::operator >>(
bool& Data)
109 Packet& Packet::operator >>(
Int8& Data)
111 if(!VerifySize(
sizeof(Data)))
114 Data = *
reinterpret_cast<const Int8*
>(RawData[ReadPos]);
115 ReadPos+=
sizeof(Data);
119 Packet& Packet::operator >>(
UInt8& Data)
121 if(!VerifySize(
sizeof(Data)))
124 Data = *
reinterpret_cast<const UInt8*
>(RawData[ReadPos]);
125 ReadPos+=
sizeof(Data);
129 Packet& Packet::operator >>(
Int16& Data)
131 if(!VerifySize(
sizeof(Data)))
134 Data = *
reinterpret_cast<const Int16*
>(RawData[ReadPos]);
135 ReadPos+=
sizeof(Data);
139 Packet& Packet::operator >>(
UInt16& Data)
141 if(!VerifySize(
sizeof(Data)))
144 Data = *
reinterpret_cast<const UInt16*
>(RawData[ReadPos]);
145 ReadPos+=
sizeof(Data);
149 Packet& Packet::operator >>(
Int32& Data)
151 if(!VerifySize(
sizeof(Data)))
154 Data = *
reinterpret_cast<const Int32*
>(RawData[ReadPos]);
155 ReadPos+=
sizeof(Data);
159 Packet& Packet::operator >>(
UInt32& Data)
161 if(!VerifySize(
sizeof(Data)))
164 Data = *
reinterpret_cast<const UInt32*
>(RawData[ReadPos]);
165 ReadPos+=
sizeof(Data);
169 Packet& Packet::operator >>(
float& Data)
171 if(!VerifySize(
sizeof(Data)))
174 Data = *
reinterpret_cast<const float*
>(RawData[ReadPos]);
175 ReadPos+=
sizeof(Data);
179 Packet& Packet::operator >>(
double& Data)
181 if(!VerifySize(
sizeof(Data)))
184 Data = *
reinterpret_cast<const double*
>(RawData[ReadPos]);
185 ReadPos+=
sizeof(Data);
189 Packet& Packet::operator >>(
char* Data)
194 if(Length && VerifySize(
sizeof(
char)*Length))
196 Data =
new char[Length+1];
197 std::memcpy(Data,&RawData[ReadPos],
sizeof(
char)*Length);
199 ReadPos+=(
sizeof(char)*Length);
205 Packet& Packet::operator >>(
String& Data)
210 if(Length && VerifySize(
sizeof(
char)*Length))
213 for(
Whole X = 0 ; X < Length ; ++X )
215 Data.push_back(static_cast<char>(RawData[ReadPos+X]));
217 ReadPos+=(
sizeof(char)*Length);
223 Packet& Packet::operator >>(
wchar_t* Data)
228 if(Length && VerifySize(
sizeof(
wchar_t)*Length))
230 Data =
new wchar_t[Length+1];
231 std::memcpy(Data,&RawData[ReadPos],
sizeof(
wchar_t)*Length);
233 ReadPos+=(
sizeof(wchar_t)*Length);
244 if(Length && VerifySize(
sizeof(
wchar_t)*Length))
247 wchar_t* Str =
new wchar_t[Length+1];
248 std::memcpy(Str,&RawData[ReadPos],
sizeof(
wchar_t)*Length);
249 Str[Length+1] = wchar_t(
'\0');
251 ReadPos+=(
sizeof(wchar_t)*Length);
267 Packet& Packet::operator <<(
bool Data)
269 *this << static_cast<UInt8>(Data);
273 Packet& Packet::operator <<(
const Int8 Data)
275 Append(&Data,
sizeof(Data));
279 Packet& Packet::operator <<(
const UInt8 Data)
281 Append(&Data,
sizeof(Data));
285 Packet& Packet::operator <<(
Int16 Data)
287 Int16 Converted = htons(Data);
288 Append(&Converted,
sizeof(Converted));
292 Packet& Packet::operator <<(
UInt16 Data)
294 UInt16 Converted = htons(Data);
295 Append(&Converted,
sizeof(Converted));
299 Packet& Packet::operator <<(
Int32 Data)
301 Int32 Converted = htonl(Data);
302 Append(&Converted,
sizeof(Converted));
306 Packet& Packet::operator <<(
UInt32 Data)
308 UInt32 Converted = htonl(Data);
309 Append(&Converted,
sizeof(Converted));
313 Packet& Packet::operator <<(
const float Data)
315 Append(&Data,
sizeof(Data));
319 Packet& Packet::operator <<(
const double Data)
321 Append(&Data,
sizeof(Data));
325 Packet& Packet::operator <<(
const char* Data)
332 Packet& Packet::operator <<(
const String& Data)
334 UInt32 Length = Data.length();
339 Append(Data.c_str(),
sizeof(char)*Length);
345 Packet& Packet::operator <<(
const wchar_t* Data)
352 Packet& Packet::operator <<(
const WideString& Data)
354 UInt32 Length = Data.length();
359 Append(Data.c_str(),
sizeof(char)*Length);