Packet.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "network/Packet.h"
  2. #include "network/ENet.h"
  3. static_assert(sizeof(float) == sizeof(uint32),
  4. "sizeof(float) != sizeof(uint32)");
  5. InPacket::InPacket(const void* data_, int size_)
  6. : data(static_cast<const char*>(data_)), size(size_), index(0) {
  7. }
  8. bool InPacket::read(void* buffer, int length) {
  9. if(index + length > size || length <= 0) {
  10. return true;
  11. }
  12. memcpy(buffer, data + index, static_cast<unsigned int>(length));
  13. index += length;
  14. return false;
  15. }
  16. bool InPacket::read(uint8& u) {
  17. return read(&u, sizeof(u));
  18. }
  19. bool InPacket::read(uint16& u) {
  20. if(read(&u, sizeof(u))) {
  21. return true;
  22. }
  23. u = ntohs(u);
  24. return false;
  25. }
  26. bool InPacket::read(uint32& u) {
  27. if(read(&u, sizeof(u))) {
  28. return true;
  29. }
  30. u = ntohl(u);
  31. return false;
  32. }
  33. #pragma GCC diagnostic push
  34. #pragma GCC diagnostic ignored "-Wduplicated-branches"
  35. // the branches are actually different but the hardware does not care
  36. bool InPacket::read(int8& s) {
  37. uint8 u;
  38. if(read(u)) {
  39. return true;
  40. }
  41. if(u < 128) {
  42. s = static_cast<int8>(static_cast<int>(u) - static_cast<int>(128));
  43. } else {
  44. s = static_cast<int8>(static_cast<unsigned int>(u) - 128u);
  45. }
  46. return false;
  47. }
  48. bool InPacket::read(int16& s) {
  49. uint16 u;
  50. if(read(u)) {
  51. return true;
  52. }
  53. if(u < 32768) {
  54. s = static_cast<int16>(static_cast<int16>(u) - 32768);
  55. } else {
  56. s = static_cast<int16>(static_cast<unsigned int>(u) - 32768u);
  57. }
  58. return false;
  59. }
  60. bool InPacket::read(int32& s) {
  61. uint32 u;
  62. if(read(u)) {
  63. return true;
  64. }
  65. if(u < 2147483648) {
  66. s = static_cast<int32>(static_cast<int32>(u) - 2147483648);
  67. } else {
  68. s = static_cast<int32>(u - 2147483648u);
  69. }
  70. return false;
  71. }
  72. #pragma GCC diagnostic pop
  73. bool InPacket::read(float& f) {
  74. uint32 u;
  75. if(read(u)) {
  76. return true;
  77. }
  78. memcpy(&f, &u, sizeof(float));
  79. return false;
  80. }
  81. OutPacket::OutPacket(int initialSize) : buffer(initialSize) {
  82. }
  83. OutPacket& OutPacket::writeU8(uint8 u) {
  84. buffer.add(u);
  85. return *this;
  86. }
  87. OutPacket& OutPacket::writeU16(uint16 u) {
  88. u = htons(u);
  89. buffer.add(u);
  90. return *this;
  91. }
  92. OutPacket& OutPacket::writeU32(uint32 u) {
  93. u = htonl(u);
  94. buffer.add(u);
  95. return *this;
  96. }
  97. OutPacket& OutPacket::writeS8(int8 s) {
  98. if(s < 0) {
  99. return writeU8(static_cast<uint8>(s + 128));
  100. }
  101. return writeU8(static_cast<uint8>(static_cast<unsigned int>(s) + 128u));
  102. }
  103. OutPacket& OutPacket::writeS16(int16 s) {
  104. if(s < 0) {
  105. return writeU16(static_cast<uint16>(s + 32768));
  106. }
  107. return writeU16(static_cast<uint16>(static_cast<unsigned int>(s) + 32768u));
  108. }
  109. OutPacket& OutPacket::writeS32(int32 s) {
  110. if(s < 0) {
  111. return writeU32(static_cast<uint32>(s + 2147483648));
  112. }
  113. return writeU32(static_cast<uint32>(s) + 2147483648u);
  114. }
  115. OutPacket& OutPacket::writeFloat(float f) {
  116. uint32 u;
  117. memcpy(&u, &f, sizeof(float));
  118. return writeU32(u);
  119. }