summaryrefslogtreecommitdiff
path: root/src/SFML/Network/Packet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/SFML/Network/Packet.cpp')
-rw-r--r--src/SFML/Network/Packet.cpp39
1 files changed, 26 insertions, 13 deletions
diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp
index 0dc2bbc..9263628 100644
--- a/src/SFML/Network/Packet.cpp
+++ b/src/SFML/Network/Packet.cpp
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2018 Laurent Gomila (laurent@sfml-dev.org)
+// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
@@ -64,6 +64,13 @@ void Packet::append(const void* data, std::size_t sizeInBytes)
////////////////////////////////////////////////////////////
+std::size_t Packet::getReadPosition() const
+{
+ return m_readPos;
+}
+
+
+////////////////////////////////////////////////////////////
void Packet::clear()
{
m_data.clear();
@@ -116,7 +123,7 @@ Packet& Packet::operator >>(Int8& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const Int8*>(&m_data[m_readPos]);
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
m_readPos += sizeof(data);
}
@@ -129,7 +136,7 @@ Packet& Packet::operator >>(Uint8& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const Uint8*>(&m_data[m_readPos]);
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
m_readPos += sizeof(data);
}
@@ -142,7 +149,8 @@ Packet& Packet::operator >>(Int16& data)
{
if (checkSize(sizeof(data)))
{
- data = ntohs(*reinterpret_cast<const Int16*>(&m_data[m_readPos]));
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
+ data = static_cast<Int16>(ntohs(static_cast<Uint16>(data)));
m_readPos += sizeof(data);
}
@@ -155,7 +163,8 @@ Packet& Packet::operator >>(Uint16& data)
{
if (checkSize(sizeof(data)))
{
- data = ntohs(*reinterpret_cast<const Uint16*>(&m_data[m_readPos]));
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
+ data = ntohs(data);
m_readPos += sizeof(data);
}
@@ -168,7 +177,8 @@ Packet& Packet::operator >>(Int32& data)
{
if (checkSize(sizeof(data)))
{
- data = ntohl(*reinterpret_cast<const Int32*>(&m_data[m_readPos]));
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
+ data = static_cast<Int32>(ntohl(static_cast<Uint32>(data)));
m_readPos += sizeof(data);
}
@@ -181,7 +191,8 @@ Packet& Packet::operator >>(Uint32& data)
{
if (checkSize(sizeof(data)))
{
- data = ntohl(*reinterpret_cast<const Uint32*>(&m_data[m_readPos]));
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
+ data = ntohl(data);
m_readPos += sizeof(data);
}
@@ -196,7 +207,8 @@ Packet& Packet::operator >>(Int64& data)
{
// Since ntohll is not available everywhere, we have to convert
// to network byte order (big endian) manually
- const Uint8* bytes = reinterpret_cast<const Uint8*>(&m_data[m_readPos]);
+ Uint8 bytes[sizeof(data)];
+ std::memcpy(bytes, &m_data[m_readPos], sizeof(data));
data = (static_cast<Int64>(bytes[0]) << 56) |
(static_cast<Int64>(bytes[1]) << 48) |
(static_cast<Int64>(bytes[2]) << 40) |
@@ -219,7 +231,8 @@ Packet& Packet::operator >>(Uint64& data)
{
// Since ntohll is not available everywhere, we have to convert
// to network byte order (big endian) manually
- const Uint8* bytes = reinterpret_cast<const Uint8*>(&m_data[m_readPos]);
+ Uint8 bytes[sizeof(data)];
+ std::memcpy(bytes, &m_data[m_readPos], sizeof(data));
data = (static_cast<Uint64>(bytes[0]) << 56) |
(static_cast<Uint64>(bytes[1]) << 48) |
(static_cast<Uint64>(bytes[2]) << 40) |
@@ -240,7 +253,7 @@ Packet& Packet::operator >>(float& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const float*>(&m_data[m_readPos]);
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
m_readPos += sizeof(data);
}
@@ -253,7 +266,7 @@ Packet& Packet::operator >>(double& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const double*>(&m_data[m_readPos]);
+ std::memcpy(&data, &m_data[m_readPos], sizeof(data));
m_readPos += sizeof(data);
}
@@ -399,7 +412,7 @@ Packet& Packet::operator <<(Uint8 data)
////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Int16 data)
{
- Int16 toWrite = htons(data);
+ Int16 toWrite = static_cast<Int16>(htons(static_cast<Uint16>(data)));
append(&toWrite, sizeof(toWrite));
return *this;
}
@@ -417,7 +430,7 @@ Packet& Packet::operator <<(Uint16 data)
////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Int32 data)
{
- Int32 toWrite = htonl(data);
+ Int32 toWrite = static_cast<Int32>(htonl(static_cast<Uint32>(data)));
append(&toWrite, sizeof(toWrite));
return *this;
}