packet.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. namespace Network {
  9. /// A class that serializes data for network transfer. It also handles endianess
  10. class Packet {
  11. public:
  12. Packet() = default;
  13. ~Packet() = default;
  14. /**
  15. * Append data to the end of the packet
  16. * @param data Pointer to the sequence of bytes to append
  17. * @param size_in_bytes Number of bytes to append
  18. */
  19. void Append(const void* data, std::size_t size_in_bytes);
  20. /**
  21. * Reads data from the current read position of the packet
  22. * @param out_data Pointer where the data should get written to
  23. * @param size_in_bytes Number of bytes to read
  24. */
  25. void Read(void* out_data, std::size_t size_in_bytes);
  26. /**
  27. * Clear the packet
  28. * After calling Clear, the packet is empty.
  29. */
  30. void Clear();
  31. /**
  32. * Ignores bytes while reading
  33. * @param length THe number of bytes to ignore
  34. */
  35. void IgnoreBytes(u32 length);
  36. /**
  37. * Get a pointer to the data contained in the packet
  38. * @return Pointer to the data
  39. */
  40. const void* GetData() const;
  41. /**
  42. * This function returns the number of bytes pointed to by
  43. * what getData returns.
  44. * @return Data size, in bytes
  45. */
  46. std::size_t GetDataSize() const;
  47. /**
  48. * This function is useful to know if there is some data
  49. * left to be read, without actually reading it.
  50. * @return True if all data was read, false otherwise
  51. */
  52. bool EndOfPacket() const;
  53. explicit operator bool() const;
  54. /// Overloads of operator >> to read data from the packet
  55. Packet& operator>>(bool& out_data);
  56. Packet& operator>>(s8& out_data);
  57. Packet& operator>>(u8& out_data);
  58. Packet& operator>>(s16& out_data);
  59. Packet& operator>>(u16& out_data);
  60. Packet& operator>>(s32& out_data);
  61. Packet& operator>>(u32& out_data);
  62. Packet& operator>>(float& out_data);
  63. Packet& operator>>(double& out_data);
  64. Packet& operator>>(char* out_data);
  65. Packet& operator>>(std::string& out_data);
  66. template <typename T>
  67. Packet& operator>>(std::vector<T>& out_data);
  68. template <typename T, std::size_t S>
  69. Packet& operator>>(std::array<T, S>& out_data);
  70. /// Overloads of operator << to write data into the packet
  71. Packet& operator<<(bool in_data);
  72. Packet& operator<<(s8 in_data);
  73. Packet& operator<<(u8 in_data);
  74. Packet& operator<<(s16 in_data);
  75. Packet& operator<<(u16 in_data);
  76. Packet& operator<<(s32 in_data);
  77. Packet& operator<<(u32 in_data);
  78. Packet& operator<<(float in_data);
  79. Packet& operator<<(double in_data);
  80. Packet& operator<<(const char* in_data);
  81. Packet& operator<<(const std::string& in_data);
  82. template <typename T>
  83. Packet& operator<<(const std::vector<T>& in_data);
  84. template <typename T, std::size_t S>
  85. Packet& operator<<(const std::array<T, S>& data);
  86. private:
  87. /**
  88. * Check if the packet can extract a given number of bytes
  89. * This function updates accordingly the state of the packet.
  90. * @param size Size to check
  91. * @return True if size bytes can be read from the packet
  92. */
  93. bool CheckSize(std::size_t size);
  94. // Member data
  95. std::vector<char> data; ///< Data stored in the packet
  96. std::size_t read_pos = 0; ///< Current reading position in the packet
  97. bool is_valid = true; ///< Reading state of the packet
  98. };
  99. template <typename T>
  100. Packet& Packet::operator>>(std::vector<T>& out_data) {
  101. for (std::size_t i = 0; i < out_data.size(); ++i) {
  102. T character = 0;
  103. *this >> character;
  104. out_data[i] = character;
  105. }
  106. return *this;
  107. }
  108. template <typename T, std::size_t S>
  109. Packet& Packet::operator>>(std::array<T, S>& out_data) {
  110. for (std::size_t i = 0; i < out_data.size(); ++i) {
  111. T character = 0;
  112. *this >> character;
  113. out_data[i] = character;
  114. }
  115. return *this;
  116. }
  117. template <typename T>
  118. Packet& Packet::operator<<(const std::vector<T>& in_data) {
  119. for (std::size_t i = 0; i < in_data.size(); ++i) {
  120. *this << in_data[i];
  121. }
  122. return *this;
  123. }
  124. template <typename T, std::size_t S>
  125. Packet& Packet::operator<<(const std::array<T, S>& in_data) {
  126. for (std::size_t i = 0; i < in_data.size(); ++i) {
  127. *this << in_data[i];
  128. }
  129. return *this;
  130. }
  131. } // namespace Network