diff --git a/include/frnetlib/Packet.h b/include/frnetlib/Packet.h index 0b48bcb..5919bc1 100644 --- a/include/frnetlib/Packet.h +++ b/include/frnetlib/Packet.h @@ -83,6 +83,29 @@ namespace fr return *this; } + /* + * For packing pairs + */ + template + inline Packet &operator<<(const std::pair &var) + { + *this << var.first; + *this << var.second; + return *this; + } + + /* + * For extracting pairs + */ + template + inline Packet &operator>>(std::pair &var) + { + *this >> var.first; + *this >> var.second; + return *this; + } + + /* * Adds a boolean variable to the packet */ @@ -271,6 +294,39 @@ namespace fr return *this; } + /* + * Adds an enum, or enum class to the packet. + * + * Underlying type must not be larger than 32bits. + * The enum's underlying type should be specified like so: + * + * enum Enum : type{}; + */ + template::value>::type* = nullptr> + inline Packet &operator<<(T var) + { + *this << (uint32_t)static_cast::type>(var); + return *this; + } + + /* + * Extracts en enum, or enum class from the packet. + * + * Underlying type must not be larger than 32bits. + * The enum's underlying type should be specified like so: + * + * enum Enum : type{}; + */ + template::value>::type* = nullptr> + inline Packet &operator>>(T &var) + { + uint32_t val; + *this >> val; + var = static_cast(val); + return *this; + } + + /* * Adds a float variable to the packet */ diff --git a/main.cpp b/main.cpp index 3428fbb..15a6755 100644 --- a/main.cpp +++ b/main.cpp @@ -15,53 +15,19 @@ #include "frnetlib/SSLContext.h" #include "frnetlib/SSLListener.h" -void server() +enum Enum : uint32_t { - fr::TcpListener listener; - listener.listen("9092"); - - fr::TcpSocket socket; - listener.accept(socket); - - uint64_t packet_count = 0; - auto last_print_time = std::chrono::system_clock::now(); - while(true) - { - fr::Packet packet; - if(socket.receive(packet) != fr::Socket::Success) - break; - - - std::string s1; - packet >> s1; - - packet_count++; - if(last_print_time + std::chrono::seconds(1) < std::chrono::system_clock::now()) - { - std::cout << "Packets per second: " << packet_count << std::endl; - packet_count = 0; - last_print_time = std::chrono::system_clock::now(); - } - } - -} - + E1 = 0, + E2 = 1, + E3 = 2, +}; int main() { - std::thread server_thread(server); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - - fr::TcpSocket socket; - socket.connect("127.0.0.1", "9092"); - - std::string a(32384, 'c'); fr::Packet packet; - while(true) - { - packet << a; - if(socket.send(packet) != fr::Socket::Success) - break; - packet.clear(); - } - server_thread.join(); + std::vector> bob = {{1, 2}, {3, 4}}; + packet << bob; + bob.clear(); + + packet >> bob; + std::cout << bob[0].first << ", " << bob[0].second << ", " << bob[1].first << ", " << bob[1].second << std::endl; } \ No newline at end of file