Packet.cpp 3.1 KB

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