Added support for 64-bit integers in sf::Packet

This commit is contained in:
Laurent Gomila 2014-10-02 23:11:44 +02:00 committed by Lukas Dürrenberger
parent f24ca9a840
commit f99035bea1
2 changed files with 92 additions and 0 deletions

View File

@ -181,6 +181,8 @@ public:
Packet& operator >>(Uint16& data); Packet& operator >>(Uint16& data);
Packet& operator >>(Int32& data); Packet& operator >>(Int32& data);
Packet& operator >>(Uint32& data); Packet& operator >>(Uint32& data);
Packet& operator >>(Int64& data);
Packet& operator >>(Uint64& data);
Packet& operator >>(float& data); Packet& operator >>(float& data);
Packet& operator >>(double& data); Packet& operator >>(double& data);
Packet& operator >>(char* data); Packet& operator >>(char* data);
@ -200,6 +202,8 @@ public:
Packet& operator <<(Uint16 data); Packet& operator <<(Uint16 data);
Packet& operator <<(Int32 data); Packet& operator <<(Int32 data);
Packet& operator <<(Uint32 data); Packet& operator <<(Uint32 data);
Packet& operator <<(Int64 data);
Packet& operator <<(Uint64 data);
Packet& operator <<(float data); Packet& operator <<(float data);
Packet& operator <<(double data); Packet& operator <<(double data);
Packet& operator <<(const char* data); Packet& operator <<(const char* data);

View File

@ -188,6 +188,52 @@ Packet& Packet::operator >>(Uint32& data)
} }
////////////////////////////////////////////////////////////
Packet& Packet::operator >>(Int64& data)
{
if (checkSize(sizeof(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]);
data = (static_cast<Int64>(bytes[0]) << 56) |
(static_cast<Int64>(bytes[1]) << 48) |
(static_cast<Int64>(bytes[2]) << 40) |
(static_cast<Int64>(bytes[3]) << 32) |
(static_cast<Int64>(bytes[4]) << 24) |
(static_cast<Int64>(bytes[5]) << 16) |
(static_cast<Int64>(bytes[6]) << 8) |
(static_cast<Int64>(bytes[7]) );
m_readPos += sizeof(data);
}
return *this;
}
////////////////////////////////////////////////////////////
Packet& Packet::operator >>(Uint64& data)
{
if (checkSize(sizeof(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]);
data = (static_cast<Uint64>(bytes[0]) << 56) |
(static_cast<Uint64>(bytes[1]) << 48) |
(static_cast<Uint64>(bytes[2]) << 40) |
(static_cast<Uint64>(bytes[3]) << 32) |
(static_cast<Uint64>(bytes[4]) << 24) |
(static_cast<Uint64>(bytes[5]) << 16) |
(static_cast<Uint64>(bytes[6]) << 8) |
(static_cast<Uint64>(bytes[7]) );
m_readPos += sizeof(data);
}
return *this;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Packet& Packet::operator >>(float& data) Packet& Packet::operator >>(float& data)
{ {
@ -385,6 +431,48 @@ Packet& Packet::operator <<(Uint32 data)
} }
////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Int64 data)
{
// Since htonll is not available everywhere, we have to convert
// to network byte order (big endian) manually
Uint8 toWrite[] =
{
(data >> 56) & 0xFF,
(data >> 48) & 0xFF,
(data >> 40) & 0xFF,
(data >> 32) & 0xFF,
(data >> 24) & 0xFF,
(data >> 16) & 0xFF,
(data >> 8) & 0xFF,
(data ) & 0xFF
};
append(&toWrite, sizeof(toWrite));
return *this;
}
////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Uint64 data)
{
// Since htonll is not available everywhere, we have to convert
// to network byte order (big endian) manually
Uint8 toWrite[] =
{
(data >> 56) & 0xFF,
(data >> 48) & 0xFF,
(data >> 40) & 0xFF,
(data >> 32) & 0xFF,
(data >> 24) & 0xFF,
(data >> 16) & 0xFF,
(data >> 8) & 0xFF,
(data ) & 0xFF
};
append(&toWrite, sizeof(toWrite));
return *this;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Packet& Packet::operator <<(float data) Packet& Packet::operator <<(float data)
{ {