From 859be35d54fda177a237e0c24bc1eaca76f1936d Mon Sep 17 00:00:00 2001 From: B3n30 Date: Sun, 9 Jul 2017 15:06:02 +0200 Subject: Network: Send the game title --- src/network/packet.h | 64 ++++++++++------------------------------------------ 1 file changed, 12 insertions(+), 52 deletions(-) (limited to 'src/network/packet.h') 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 @@ namespace Network { -/// A class for serialize data for network transfer. It also handles endianess +/// A class that serializes data for network transfer. It also handles endianess class Packet { - /// A bool-like type that cannot be converted to integer or pointer types - typedef bool (Packet::*BoolType)(std::size_t); - public: - Packet(); - ~Packet(); + Packet() = default; + ~Packet() = default; /** * Append data to the end of the packet @@ -64,41 +61,8 @@ public: * @return True if all data was read, false otherwise */ bool EndOfPacket() const; - /** - * Test the validity of the packet, for reading - * This operator allows to test the packet as a boolean - * variable, to check if a reading operation was successful. - * - * A packet will be in an invalid state if it has no more - * data to read. - * - * This behaviour is the same as standard C++ streams. - * - * Usage example: - * @code - * float x; - * packet >> x; - * if (packet) - * { - * // ok, x was extracted successfully - * } - * - * // -- or -- - * - * float x; - * if (packet >> x) - * { - * // ok, x was extracted successfully - * } - * @endcode - * - * Don't focus on the return type, it's equivalent to bool but - * it disallows unwanted implicit conversions to integer or - * pointer types. - * - * @return True if last data extraction from packet was successful - */ - operator BoolType() const; + + explicit operator bool() const; /// Overloads of operator >> to read data from the packet Packet& operator>>(bool& out_data); @@ -135,10 +99,6 @@ public: Packet& operator<<(const std::array& data); private: - /// Disallow comparisons between packets - bool operator==(const Packet& right) const; - bool operator!=(const Packet& right) const; - /** * Check if the packet can extract a given number of bytes * This function updates accordingly the state of the packet. @@ -148,14 +108,14 @@ private: bool CheckSize(std::size_t size); // Member data - std::vector data; ///< Data stored in the packet - std::size_t read_pos; ///< Current reading position in the packet - bool is_valid; ///< Reading state of the packet + std::vector data; ///< Data stored in the packet + std::size_t read_pos = 0; ///< Current reading position in the packet + bool is_valid = true; ///< Reading state of the packet }; template Packet& Packet::operator>>(std::vector& out_data) { - for (u32 i = 0; i < out_data.size(); ++i) { + for (std::size_t i = 0; i < out_data.size(); ++i) { T character = 0; *this >> character; out_data[i] = character; @@ -165,7 +125,7 @@ Packet& Packet::operator>>(std::vector& out_data) { template Packet& Packet::operator>>(std::array& out_data) { - for (u32 i = 0; i < out_data.size(); ++i) { + for (std::size_t i = 0; i < out_data.size(); ++i) { T character = 0; *this >> character; out_data[i] = character; @@ -175,7 +135,7 @@ Packet& Packet::operator>>(std::array& out_data) { template Packet& Packet::operator<<(const std::vector& in_data) { - for (u32 i = 0; i < in_data.size(); ++i) { + for (std::size_t i = 0; i < in_data.size(); ++i) { *this << in_data[i]; } return *this; @@ -183,7 +143,7 @@ Packet& Packet::operator<<(const std::vector& in_data) { template Packet& Packet::operator<<(const std::array& in_data) { - for (u32 i = 0; i < in_data.size(); ++i) { + for (std::size_t i = 0; i < in_data.size(); ++i) { *this << in_data[i]; } return *this; -- cgit v1.2.3