123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include "network/Packet.h"
- #include "network/ENet.h"
- static_assert(sizeof(float) == sizeof(uint32),
- "sizeof(float) != sizeof(uint32)");
- InPacket::InPacket(const void* data_, int size_)
- : data(static_cast<const char*>(data_)), size(size_), index(0) {
- }
- bool InPacket::read(void* buffer, int length) {
- if(index + length > size || length <= 0) {
- return true;
- }
- memcpy(buffer, data + index, static_cast<unsigned int>(length));
- index += length;
- return false;
- }
- bool InPacket::read(uint8& u) {
- return read(&u, sizeof(u));
- }
- bool InPacket::read(uint16& u) {
- if(read(&u, sizeof(u))) {
- return true;
- }
- u = ntohs(u);
- return false;
- }
- bool InPacket::read(uint32& u) {
- if(read(&u, sizeof(u))) {
- return true;
- }
- u = ntohl(u);
- return false;
- }
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wduplicated-branches"
- // the branches are actually different but the hardware does not care
- bool InPacket::read(int8& s) {
- uint8 u;
- if(read(u)) {
- return true;
- }
- if(u < 128) {
- s = static_cast<int8>(static_cast<int>(u) - static_cast<int>(128));
- } else {
- s = static_cast<int8>(static_cast<unsigned int>(u) - 128u);
- }
- return false;
- }
- bool InPacket::read(int16& s) {
- uint16 u;
- if(read(u)) {
- return true;
- }
- if(u < 32768) {
- s = static_cast<int16>(static_cast<int16>(u) - 32768);
- } else {
- s = static_cast<int16>(static_cast<unsigned int>(u) - 32768u);
- }
- return false;
- }
- bool InPacket::read(int32& s) {
- uint32 u;
- if(read(u)) {
- return true;
- }
- if(u < 2147483648) {
- s = static_cast<int32>(static_cast<int32>(u) - 2147483648);
- } else {
- s = static_cast<int32>(u - 2147483648u);
- }
- return false;
- }
- #pragma GCC diagnostic pop
- bool InPacket::read(float& f) {
- uint32 u;
- if(read(u)) {
- return true;
- }
- memcpy(&f, &u, sizeof(float));
- return false;
- }
- OutPacket::OutPacket(int initialSize) : buffer(initialSize) {
- }
- OutPacket& OutPacket::writeU8(uint8 u) {
- buffer.add(u);
- return *this;
- }
- OutPacket& OutPacket::writeU16(uint16 u) {
- u = htons(u);
- buffer.add(u);
- return *this;
- }
- OutPacket& OutPacket::writeU32(uint32 u) {
- u = htonl(u);
- buffer.add(u);
- return *this;
- }
- OutPacket& OutPacket::writeS8(int8 s) {
- if(s < 0) {
- return writeU8(static_cast<uint8>(s + 128));
- }
- return writeU8(static_cast<uint8>(static_cast<unsigned int>(s) + 128u));
- }
- OutPacket& OutPacket::writeS16(int16 s) {
- if(s < 0) {
- return writeU16(static_cast<uint16>(s + 32768));
- }
- return writeU16(static_cast<uint16>(static_cast<unsigned int>(s) + 32768u));
- }
- OutPacket& OutPacket::writeS32(int32 s) {
- if(s < 0) {
- return writeU32(static_cast<uint32>(s + 2147483648));
- }
- return writeU32(static_cast<uint32>(s) + 2147483648u);
- }
- OutPacket& OutPacket::writeFloat(float f) {
- uint32 u;
- memcpy(&u, &f, sizeof(float));
- return writeU32(u);
- }
|