Packet.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "libs/enet/include/enet.h"
  2. #include "network/Packet.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) {
  10. return true;
  11. }
  12. memcpy(buffer, data + index, length);
  13. index += length;
  14. return false;
  15. }
  16. bool InPacket::readU8(uint8& u) {
  17. return read(&u, sizeof(u));
  18. }
  19. bool InPacket::readU16(uint16& u) {
  20. if(read(&u, sizeof(u))) {
  21. return true;
  22. }
  23. u = ntohs(u);
  24. return false;
  25. }
  26. bool InPacket::readU32(uint32& u) {
  27. if(read(&u, sizeof(u))) {
  28. return true;
  29. }
  30. u = ntohl(u);
  31. return false;
  32. }
  33. bool InPacket::readS8(int8& s) {
  34. uint8 u;
  35. if(readU8(u)) {
  36. return true;
  37. }
  38. if(u < 128) {
  39. s = static_cast<int8>(u) - 128;
  40. } else {
  41. s = u - 128;
  42. }
  43. return false;
  44. }
  45. bool InPacket::readS16(int16& s) {
  46. uint16 u;
  47. if(readU16(u)) {
  48. return true;
  49. }
  50. if(u < 32768) {
  51. s = static_cast<int16>(u) - 32768;
  52. } else {
  53. s = u - 32768;
  54. }
  55. return false;
  56. }
  57. bool InPacket::readS32(int32& s) {
  58. uint32 u;
  59. if(readU32(u)) {
  60. return true;
  61. }
  62. if(u < 2147483648) {
  63. s = static_cast<int32>(u) - 2147483648;
  64. } else {
  65. s = u - 2147483648;
  66. }
  67. return false;
  68. }
  69. bool InPacket::readFloat(float& f) {
  70. uint32 u;
  71. if(readU32(u)) {
  72. return true;
  73. }
  74. memcpy(&f, &u, sizeof(float));
  75. return false;
  76. }
  77. OutPacket::OutPacket(int initialSize) : buffer(initialSize) {
  78. }
  79. OutPacket& OutPacket::writeU8(uint8 u) {
  80. buffer.add(u);
  81. return *this;
  82. }
  83. OutPacket& OutPacket::writeU16(uint16 u) {
  84. u = htons(u);
  85. buffer.add(u);
  86. return *this;
  87. }
  88. OutPacket& OutPacket::writeU32(uint32 u) {
  89. u = htonl(u);
  90. buffer.add(u);
  91. return *this;
  92. }
  93. OutPacket& OutPacket::writeS8(int8 s) {
  94. if(s < 0) {
  95. return writeU8(s + 128);
  96. }
  97. return writeU8(static_cast<uint8>(s) + 128u);
  98. }
  99. OutPacket& OutPacket::writeS16(int16 s) {
  100. if(s < 0) {
  101. return writeU16(s + 32768);
  102. }
  103. return writeU16(static_cast<uint16>(s) + 32768u);
  104. }
  105. OutPacket& OutPacket::writeS32(int32 s) {
  106. if(s < 0) {
  107. return writeU32(s + 2147483648);
  108. }
  109. return writeU32(static_cast<uint32>(s) + 2147483648u);
  110. }
  111. OutPacket& OutPacket::writeFloat(float f) {
  112. uint32 u;
  113. memcpy(&u, &f, sizeof(float));
  114. return writeU32(u);
  115. }