| 1 | //! @file data.hpp |
| 2 | //! @author ryftchen |
| 3 | //! @brief The declarations (data) in the application module. |
| 4 | //! @version 0.1.0 |
| 5 | //! @copyright Copyright (c) 2022-2025 ryftchen. All rights reserved. |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #ifndef _PRECOMPILED_HEADER |
| 10 | #include <netinet/in.h> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | #else |
| 14 | #include "application/pch/precompiled_header.hpp" |
| 15 | #endif // _PRECOMPILED_HEADER |
| 16 | |
| 17 | //! @brief The application module. |
| 18 | namespace application // NOLINT(modernize-concat-nested-namespaces) |
| 19 | { |
| 20 | //! @brief Data-processing-related functions in the application module. |
| 21 | namespace data |
| 22 | { |
| 23 | //! @brief Data packet. |
| 24 | class Packet |
| 25 | { |
| 26 | public: |
| 27 | //! @brief Construct a new Packet object. |
| 28 | //! @param pktBuf - packet buffer |
| 29 | //! @param pktLen - buffer length |
| 30 | Packet(char* const pktBuf, const std::size_t pktLen); |
| 31 | |
| 32 | //! @brief Write data to the packet buffer. |
| 33 | //! @tparam Data - type of data to be written |
| 34 | //! @param data - original data |
| 35 | //! @return whether it is continuously writable |
| 36 | template <typename Data> |
| 37 | requires std::is_trivially_copyable_v<Data> |
| 38 | bool write(const Data data); |
| 39 | //! @brief Write data to the packet buffer. |
| 40 | //! @param dst - data after conversion |
| 41 | //! @param offset - data offset |
| 42 | //! @return whether it is continuously writable |
| 43 | bool write(const void* const dst, const std::size_t offset); |
| 44 | //! @brief Read data to the packet buffer. |
| 45 | //! @tparam Data - type of data to be read |
| 46 | //! @param data - original data |
| 47 | //! @return whether it is continuously readable |
| 48 | template <typename Data> |
| 49 | requires std::is_trivially_copyable_v<Data> |
| 50 | bool read(Data* const data); |
| 51 | //! @brief Read data to the packet buffer. |
| 52 | //! @param dst - data after conversion |
| 53 | //! @param offset - data offset |
| 54 | //! @return whether it is continuously readable |
| 55 | bool read(void* const dst, const std::size_t offset); |
| 56 | |
| 57 | private: |
| 58 | //! @brief Pointer to the beginning of the buffer. |
| 59 | char* const head{nullptr}; |
| 60 | //! @brief Pointer to the ending of the buffer. |
| 61 | const char* const tail{nullptr}; |
| 62 | //! @brief Pointer to the current writing location. |
| 63 | char* writer{nullptr}; |
| 64 | //! @brief Pointer to the current reading location. |
| 65 | const char* reader{nullptr}; |
| 66 | }; |
| 67 | |
| 68 | template <typename Data> |
| 69 | requires std::is_trivially_copyable_v<Data> |
| 70 | bool Packet::write(const Data data) |
| 71 | { |
| 72 | Data temp{}; |
| 73 | if constexpr (sizeof(Data) == sizeof(int)) |
| 74 | { |
| 75 | temp = ::htonl(hostlong: data); |
| 76 | } |
| 77 | else if constexpr (sizeof(Data) == sizeof(short)) |
| 78 | { |
| 79 | temp = ::htons(hostshort: data); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | temp = data; |
| 84 | } |
| 85 | return write(&temp, sizeof(Data)); |
| 86 | } |
| 87 | |
| 88 | template <typename Data> |
| 89 | requires std::is_trivially_copyable_v<Data> |
| 90 | bool Packet::read(Data* const data) |
| 91 | { |
| 92 | if (!data) |
| 93 | { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | const bool isEnd = read(data, sizeof(Data)); |
| 98 | if constexpr (sizeof(Data) == sizeof(int)) |
| 99 | { |
| 100 | *data = ::ntohl(netlong: *data); |
| 101 | } |
| 102 | else if constexpr (sizeof(Data) == sizeof(short)) |
| 103 | { |
| 104 | *data = ::ntohs(netshort: *data); |
| 105 | } |
| 106 | return isEnd; |
| 107 | } |
| 108 | |
| 109 | extern void encryptMessage(char* const buffer, const std::size_t length); |
| 110 | extern void decryptMessage(char* const buffer, const std::size_t length); |
| 111 | |
| 112 | extern void compressData(std::vector<char>& cache); |
| 113 | extern void decompressData(std::vector<char>& cache); |
| 114 | |
| 115 | extern std::string toHexString(const char* const buffer, const std::size_t length); |
| 116 | } // namespace data |
| 117 | } // namespace application |
| 118 | |