diff options
| author | 2017-07-09 15:06:02 +0200 | |
|---|---|---|
| committer | 2017-07-16 21:29:57 +0200 | |
| commit | 859be35d54fda177a237e0c24bc1eaca76f1936d (patch) | |
| tree | 87d277c722f269a1b8ca2186ec1e3ddbe8e4397a /src/network/packet.h | |
| parent | Network: Enable sending and receiving chat messages (diff) | |
| download | yuzu-859be35d54fda177a237e0c24bc1eaca76f1936d.tar.gz yuzu-859be35d54fda177a237e0c24bc1eaca76f1936d.tar.xz yuzu-859be35d54fda177a237e0c24bc1eaca76f1936d.zip | |
Network: Send the game title
Diffstat (limited to '')
| -rw-r--r-- | src/network/packet.h | 64 |
1 files changed, 12 insertions, 52 deletions
diff --git a/src/network/packet.h b/src/network/packet.h index 6d84cfbac..026271701 100644 --- a/src/network/packet.h +++ b/src/network/packet.h | |||
| @@ -10,14 +10,11 @@ | |||
| 10 | 10 | ||
| 11 | namespace Network { | 11 | namespace Network { |
| 12 | 12 | ||
| 13 | /// A class for serialize data for network transfer. It also handles endianess | 13 | /// A class that serializes data for network transfer. It also handles endianess |
| 14 | class Packet { | 14 | class Packet { |
| 15 | /// A bool-like type that cannot be converted to integer or pointer types | ||
| 16 | typedef bool (Packet::*BoolType)(std::size_t); | ||
| 17 | |||
| 18 | public: | 15 | public: |
| 19 | Packet(); | 16 | Packet() = default; |
| 20 | ~Packet(); | 17 | ~Packet() = default; |
| 21 | 18 | ||
| 22 | /** | 19 | /** |
| 23 | * Append data to the end of the packet | 20 | * Append data to the end of the packet |
| @@ -64,41 +61,8 @@ public: | |||
| 64 | * @return True if all data was read, false otherwise | 61 | * @return True if all data was read, false otherwise |
| 65 | */ | 62 | */ |
| 66 | bool EndOfPacket() const; | 63 | bool EndOfPacket() const; |
| 67 | /** | 64 | |
| 68 | * Test the validity of the packet, for reading | 65 | explicit operator bool() const; |
| 69 | * This operator allows to test the packet as a boolean | ||
| 70 | * variable, to check if a reading operation was successful. | ||
| 71 | * | ||
| 72 | * A packet will be in an invalid state if it has no more | ||
| 73 | * data to read. | ||
| 74 | * | ||
| 75 | * This behaviour is the same as standard C++ streams. | ||
| 76 | * | ||
| 77 | * Usage example: | ||
| 78 | * @code | ||
| 79 | * float x; | ||
| 80 | * packet >> x; | ||
| 81 | * if (packet) | ||
| 82 | * { | ||
| 83 | * // ok, x was extracted successfully | ||
| 84 | * } | ||
| 85 | * | ||
| 86 | * // -- or -- | ||
| 87 | * | ||
| 88 | * float x; | ||
| 89 | * if (packet >> x) | ||
| 90 | * { | ||
| 91 | * // ok, x was extracted successfully | ||
| 92 | * } | ||
| 93 | * @endcode | ||
| 94 | * | ||
| 95 | * Don't focus on the return type, it's equivalent to bool but | ||
| 96 | * it disallows unwanted implicit conversions to integer or | ||
| 97 | * pointer types. | ||
| 98 | * | ||
| 99 | * @return True if last data extraction from packet was successful | ||
| 100 | */ | ||
| 101 | operator BoolType() const; | ||
| 102 | 66 | ||
| 103 | /// Overloads of operator >> to read data from the packet | 67 | /// Overloads of operator >> to read data from the packet |
| 104 | Packet& operator>>(bool& out_data); | 68 | Packet& operator>>(bool& out_data); |
| @@ -135,10 +99,6 @@ public: | |||
| 135 | Packet& operator<<(const std::array<T, S>& data); | 99 | Packet& operator<<(const std::array<T, S>& data); |
| 136 | 100 | ||
| 137 | private: | 101 | private: |
| 138 | /// Disallow comparisons between packets | ||
| 139 | bool operator==(const Packet& right) const; | ||
| 140 | bool operator!=(const Packet& right) const; | ||
| 141 | |||
| 142 | /** | 102 | /** |
| 143 | * Check if the packet can extract a given number of bytes | 103 | * Check if the packet can extract a given number of bytes |
| 144 | * This function updates accordingly the state of the packet. | 104 | * This function updates accordingly the state of the packet. |
| @@ -148,14 +108,14 @@ private: | |||
| 148 | bool CheckSize(std::size_t size); | 108 | bool CheckSize(std::size_t size); |
| 149 | 109 | ||
| 150 | // Member data | 110 | // Member data |
| 151 | std::vector<char> data; ///< Data stored in the packet | 111 | std::vector<char> data; ///< Data stored in the packet |
| 152 | std::size_t read_pos; ///< Current reading position in the packet | 112 | std::size_t read_pos = 0; ///< Current reading position in the packet |
| 153 | bool is_valid; ///< Reading state of the packet | 113 | bool is_valid = true; ///< Reading state of the packet |
| 154 | }; | 114 | }; |
| 155 | 115 | ||
| 156 | template <typename T> | 116 | template <typename T> |
| 157 | Packet& Packet::operator>>(std::vector<T>& out_data) { | 117 | Packet& Packet::operator>>(std::vector<T>& out_data) { |
| 158 | for (u32 i = 0; i < out_data.size(); ++i) { | 118 | for (std::size_t i = 0; i < out_data.size(); ++i) { |
| 159 | T character = 0; | 119 | T character = 0; |
| 160 | *this >> character; | 120 | *this >> character; |
| 161 | out_data[i] = character; | 121 | out_data[i] = character; |
| @@ -165,7 +125,7 @@ Packet& Packet::operator>>(std::vector<T>& out_data) { | |||
| 165 | 125 | ||
| 166 | template <typename T, std::size_t S> | 126 | template <typename T, std::size_t S> |
| 167 | Packet& Packet::operator>>(std::array<T, S>& out_data) { | 127 | Packet& Packet::operator>>(std::array<T, S>& out_data) { |
| 168 | for (u32 i = 0; i < out_data.size(); ++i) { | 128 | for (std::size_t i = 0; i < out_data.size(); ++i) { |
| 169 | T character = 0; | 129 | T character = 0; |
| 170 | *this >> character; | 130 | *this >> character; |
| 171 | out_data[i] = character; | 131 | out_data[i] = character; |
| @@ -175,7 +135,7 @@ Packet& Packet::operator>>(std::array<T, S>& out_data) { | |||
| 175 | 135 | ||
| 176 | template <typename T> | 136 | template <typename T> |
| 177 | Packet& Packet::operator<<(const std::vector<T>& in_data) { | 137 | Packet& Packet::operator<<(const std::vector<T>& in_data) { |
| 178 | for (u32 i = 0; i < in_data.size(); ++i) { | 138 | for (std::size_t i = 0; i < in_data.size(); ++i) { |
| 179 | *this << in_data[i]; | 139 | *this << in_data[i]; |
| 180 | } | 140 | } |
| 181 | return *this; | 141 | return *this; |
| @@ -183,7 +143,7 @@ Packet& Packet::operator<<(const std::vector<T>& in_data) { | |||
| 183 | 143 | ||
| 184 | template <typename T, std::size_t S> | 144 | template <typename T, std::size_t S> |
| 185 | Packet& Packet::operator<<(const std::array<T, S>& in_data) { | 145 | Packet& Packet::operator<<(const std::array<T, S>& in_data) { |
| 186 | for (u32 i = 0; i < in_data.size(); ++i) { | 146 | for (std::size_t i = 0; i < in_data.size(); ++i) { |
| 187 | *this << in_data[i]; | 147 | *this << in_data[i]; |
| 188 | } | 148 | } |
| 189 | return *this; | 149 | return *this; |