28 #include <SFML/Network/Packet.hpp>
29 #include <SFML/Network/SocketHelper.hpp>
60 if (Data && (SizeInBytes > 0))
62 std::size_t Start = myData.size();
63 myData.resize(Start + SizeInBytes);
64 memcpy(&myData[Start], Data, SizeInBytes);
87 return !myData.empty() ? &myData[0] : NULL;
105 return myReadPos >= myData.size();
112 Packet::operator bool()
const
131 if (CheckSize(
sizeof(Data)))
133 Data = *
reinterpret_cast<const Int8*
>(
GetData() + myReadPos);
134 myReadPos +=
sizeof(Data);
141 if (CheckSize(
sizeof(Data)))
143 Data = *
reinterpret_cast<const Uint8*
>(
GetData() + myReadPos);
144 myReadPos +=
sizeof(Data);
151 if (CheckSize(
sizeof(Data)))
153 Data = ntohs(*reinterpret_cast<const Int16*>(
GetData() + myReadPos));
154 myReadPos +=
sizeof(Data);
161 if (CheckSize(
sizeof(Data)))
163 Data = ntohs(*reinterpret_cast<const Uint16*>(
GetData() + myReadPos));
164 myReadPos +=
sizeof(Data);
171 if (CheckSize(
sizeof(Data)))
173 Data = ntohl(*reinterpret_cast<const Int32*>(
GetData() + myReadPos));
174 myReadPos +=
sizeof(Data);
181 if (CheckSize(
sizeof(Data)))
183 Data = ntohl(*reinterpret_cast<const Uint32*>(
GetData() + myReadPos));
184 myReadPos +=
sizeof(Data);
191 if (CheckSize(
sizeof(Data)))
193 Data = *
reinterpret_cast<const float*
>(
GetData() + myReadPos);
194 myReadPos +=
sizeof(Data);
201 if (CheckSize(
sizeof(Data)))
203 Data = *
reinterpret_cast<const double*
>(
GetData() + myReadPos);
204 myReadPos +=
sizeof(Data);
215 if ((Length > 0) && CheckSize(Length))
218 memcpy(Data,
GetData() + myReadPos, Length);
234 if ((Length > 0) && CheckSize(Length))
237 Data.assign(
GetData() + myReadPos, Length);
251 if ((Length > 0) && CheckSize(Length *
sizeof(Int32)))
254 for (Uint32 i = 0; i < Length; ++i)
258 Data[i] =
static_cast<wchar_t>(c);
260 Data[Length] = L
'\0';
272 if ((Length > 0) && CheckSize(Length *
sizeof(Int32)))
275 for (Uint32 i = 0; i < Length; ++i)
279 Data +=
static_cast<wchar_t>(c);
292 *this << static_cast<Uint8>(Data);
297 Append(&Data,
sizeof(Data));
302 Append(&Data,
sizeof(Data));
307 Int16 ToWrite = htons(Data);
308 Append(&ToWrite,
sizeof(ToWrite));
313 Uint16 ToWrite = htons(Data);
314 Append(&ToWrite,
sizeof(ToWrite));
319 Int32 ToWrite = htonl(Data);
320 Append(&ToWrite,
sizeof(ToWrite));
325 Uint32 ToWrite = htonl(Data);
326 Append(&ToWrite,
sizeof(ToWrite));
331 Append(&Data,
sizeof(Data));
336 Append(&Data,
sizeof(Data));
343 for (
const char* c = Data; *c !=
'\0'; ++c)
348 Append(Data, Length *
sizeof(
char));
355 Uint32 Length =
static_cast<Uint32
>(Data.size());
361 Append(Data.c_str(), Length *
sizeof(std::string::value_type));
370 for (
const wchar_t* c = Data; *c != L
'\0'; ++c)
375 for (
const wchar_t* c = Data; *c != L
'\0'; ++c)
376 *
this << static_cast<Int32>(*c);
383 Uint32 Length =
static_cast<Uint32
>(Data.size());
389 for (std::wstring::const_iterator c = Data.begin(); c != Data.end(); ++c)
390 *
this << static_cast<Int32>(*c);
400 bool Packet::CheckSize(std::size_t Size)
402 myIsValid = myIsValid && (myReadPos + Size <= myData.size());
411 const char* Packet::OnSend(std::size_t& DataSize)
421 void Packet::OnReceive(
const char* Data, std::size_t DataSize)
void Append(const void *Data, std::size_t SizeInBytes)
Append data to the end of the packet.
Packet wraps data to send / to receive through the network.
bool EndOfPacket() const
Tell if the reading position has reached the end of the packet.
virtual ~Packet()
Virtual destructor.
std::size_t GetDataSize() const
Get the size of the data contained in the packet.
Packet & operator>>(bool &Data)
Operator >> overloads to extract data from the packet.
Packet & operator<<(bool Data)
Operator << overloads to put data into the packet.
void Clear()
Clear the packet data.
const char * GetData() const
Get a pointer to the data contained in the packet Warning : the returned pointer may be invalid after...
Packet()
Default constructor.