Packet.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <utility>
  2. #include "network/Packet.h"
  3. static_assert(sizeof(float) == sizeof(uint32),
  4. "sizeof(float) != sizeof(uint32)");
  5. InPacket::InPacket(ENetPacket* packet) : packet(packet), index(0) {
  6. }
  7. bool InPacket::read(void* buffer, unsigned int length) {
  8. if(index + length > packet->dataLength) {
  9. return true;
  10. }
  11. memcpy(buffer, packet->data + index, length);
  12. index += length;
  13. return false;
  14. }
  15. bool InPacket::readU8(uint8& u) {
  16. return read(&u, sizeof(u));
  17. }
  18. bool InPacket::readU16(uint16& u) {
  19. if(read(&u, sizeof(u))) {
  20. return true;
  21. }
  22. u = ntohs(u);
  23. return false;
  24. }
  25. bool InPacket::readU32(uint32& u) {
  26. if(read(&u, sizeof(u))) {
  27. return true;
  28. }
  29. u = ntohl(u);
  30. return false;
  31. }
  32. bool InPacket::readS8(int8& s) {
  33. uint8 u;
  34. if(readU8(u)) {
  35. return true;
  36. }
  37. if(u < 128) {
  38. s = static_cast<int8>(u) - 128;
  39. } else {
  40. s = u - 128;
  41. }
  42. return false;
  43. }
  44. bool InPacket::readS16(int16& s) {
  45. uint16 u;
  46. if(readU16(u)) {
  47. return true;
  48. }
  49. if(u < 32768) {
  50. s = static_cast<int16>(u) - 32768;
  51. } else {
  52. s = u - 32768;
  53. }
  54. return false;
  55. }
  56. bool InPacket::readS32(int32& s) {
  57. uint32 u;
  58. if(readU32(u)) {
  59. return true;
  60. }
  61. if(u < 2147483648) {
  62. s = static_cast<int32>(u) - 2147483648;
  63. } else {
  64. s = u - 2147483648;
  65. }
  66. return false;
  67. }
  68. bool InPacket::readFloat(float& f) {
  69. uint32 u;
  70. if(readU32(u)) {
  71. return true;
  72. }
  73. memcpy(&f, &u, sizeof(float));
  74. return false;
  75. }
  76. OutPacket::OutPacket(unsigned int size, int flags, int channel)
  77. : packet(enet_packet_create(nullptr, size, flags)), index(0),
  78. channel(channel) {
  79. }
  80. OutPacket OutPacket::reliable(unsigned int size) {
  81. return OutPacket(size, ENET_PACKET_FLAG_RELIABLE, 0);
  82. }
  83. OutPacket OutPacket::sequenced(unsigned int size) {
  84. return OutPacket(size, 0, 1);
  85. }
  86. OutPacket OutPacket::unsequenced(unsigned int size) {
  87. return OutPacket(size, ENET_PACKET_FLAG_UNSEQUENCED, 2);
  88. }
  89. OutPacket::~OutPacket() {
  90. enet_packet_destroy(packet);
  91. }
  92. OutPacket::OutPacket(const OutPacket& other)
  93. : packet(enet_packet_copy(other.packet)), index(other.index) {
  94. }
  95. OutPacket::OutPacket(OutPacket&& other) : packet(nullptr), index(0) {
  96. std::swap(packet, other.packet);
  97. std::swap(index, other.index);
  98. }
  99. OutPacket& OutPacket::operator=(OutPacket other) {
  100. std::swap(packet, other.packet);
  101. std::swap(index, other.index);
  102. return *this;
  103. }
  104. void OutPacket::write(const void* buffer, unsigned int length) {
  105. if(packet == nullptr || index + length > packet->dataLength) {
  106. return;
  107. }
  108. memcpy(packet->data + index, buffer, length);
  109. index += length;
  110. }
  111. void OutPacket::writeU8(uint8 u) {
  112. write(&u, sizeof(u));
  113. }
  114. void OutPacket::writeU16(uint16 u) {
  115. u = htons(u);
  116. write(&u, sizeof(u));
  117. }
  118. void OutPacket::writeU32(uint32 u) {
  119. u = htonl(u);
  120. write(&u, sizeof(u));
  121. }
  122. void OutPacket::writeS8(int8 s) {
  123. if(s < 0) {
  124. writeU8(s + 128);
  125. } else {
  126. writeU8(static_cast<uint8>(s) + 128);
  127. }
  128. }
  129. void OutPacket::writeS16(int16 s) {
  130. if(s < 0) {
  131. writeU16(s + 32768);
  132. } else {
  133. writeU16(static_cast<uint16>(s) + 32768);
  134. }
  135. }
  136. void OutPacket::writeS32(int32 s) {
  137. if(s < 0) {
  138. writeU32(s + 2147483648);
  139. } else {
  140. writeU32(static_cast<uint32>(s) + 2147483648);
  141. }
  142. }
  143. void OutPacket::writeFloat(float f) {
  144. uint32 u;
  145. memcpy(&u, &f, sizeof(float));
  146. writeU32(u);
  147. }